tree: 9302c99e774e1ab91caab3cdc1a032cff1066bb8 [search]
  1. table/
  2. tree/
  3. README.md
example/subscription/README.md

Subscription Examples

These programs are part of the main github.com/apache/iotdb-client-go/v2 module. They demonstrate the Go subscription client with Pull and Push consumers, manual commit, progress, Seek, and processors.

Run the commands from the example/ directory with Go 1.25 or later and a subscription-enabled IoTDB server.

Programs

ProgramScenarioAPIs demonstrated
treeTree path/time filtering and topic managementSession management, Pull/Push record consumption, automatic commit
tableTable database/table/time filtering and schema metadataSession management, Pull/Push record consumption, automatic commit
tree/incrementalTree incremental records with checkpoint recoveryManual commit, progress, SeekAfter, optional processors
table/incrementalTable incremental records with checkpoint recoveryManual commit, progress, SeekAfter, optional processors

Imports

import (
    "github.com/apache/iotdb-client-go/v2/subscription"
    subconsumer "github.com/apache/iotdb-client-go/v2/subscription/consumer"
    subpayload "github.com/apache/iotdb-client-go/v2/subscription/payload"
    subprocessor "github.com/apache/iotdb-client-go/v2/subscription/processor"
    subsession "github.com/apache/iotdb-client-go/v2/subscription/session"
)

The root subscription package contains shared model constants, connection configuration, and public errors. Management uses subsession, consumption uses subconsumer, record values use subpayload, and processors use subprocessor.

Ordinary Record Subscriptions

The ordinary programs create sample data and a SubscriptionRecordHandler topic, subscribe, consume records, and print topic/subscription metadata.

cd example
go run ./subscription/tree
go run ./subscription/table

Pull is the default consumer mode and uses automatic commit. Process a delivered batch according to application requirements; automatic commit confirms it after the configured interval.

Push delivery runs on the consumer background loop and invokes a sequential callback:

go run ./subscription/tree -consumer=push -ack=after -duration=10s
go run ./subscription/table -consumer=push -ack=before -duration=10s
  • -ack=after acknowledges successful callbacks and nacks failed callbacks.
  • -ack=before acknowledges the batch before invoking callbacks.
  • Push handlers must not release the message.
  • Pull callers should release messages after processing and commit.

Incremental Subscriptions

The incremental programs create a bootstrap record before subscribing, then write new records through the incremental path. They use PollWithInfo, manual commit, Positions, CommittedPositions, and SeekAfter.

go run ./subscription/tree/incremental
go run ./subscription/table/incremental

The checkpoint is kept in memory for the current run; the programs do not persist it to disk.

Enable the optional processor chain with -processors:

go run ./subscription/tree/incremental -processors
go run ./subscription/table/incremental -processors

The chain installs WatermarkProcessor and the model-specific ColumnAlignProcessor. It demonstrates message-level watermark ordering and in-place column alignment.

Connection Settings

The programs accept -host, -port, and -username, defaulting to 127.0.0.1:6667 and root. IOTDB_PASSWORD supplies the password and defaults to root when unset.

IOTDB_PASSWORD='your-password' go run ./subscription/table \
  -host=127.0.0.1 \
  -port=6667 \
  -username=root

Resources and Cleanup

Each run uses unique resource names. The programs:

  • Open the subscription session and consumer with bounded contexts.
  • Create and close their own consumer subscriptions.
  • Drop the generated topic after the consumer closes.
  • Leave the sample database intact for inspection.
  • Report processing, acknowledgment, and cleanup errors.

Limitations

  • The examples use record payloads only.
  • TsFile payload decoding returns subscription.ErrUnsupportedTsFile.
  • The subscription client does not provide a TsFile Reader or file-management API.
  • TLS connections are not currently supported.

See the subscription client guide for configuration defaults, message ownership, errors, and the complete public API.