Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions iotdb-client/client-py/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,57 @@ zone = session.get_time_zone()
session.close()
```

### Export TsFiles with Subscription

`SubscriptionSession` manages topics and creates pull consumers. A pull consumer downloads a
Subscription TsFile to `file_save_dir/group_id/consumer_id/topic_name/` and returns a
`SubscriptionMessage`. Call `commit(message)` after processing the file so IoTDB can advance the
consumer's progress.

```python
import uuid

from iotdb.subscription import SubscriptionSession

topic_name = "python_export_{}".format(uuid.uuid4().hex)
topic_attributes = {
"mode": "snapshot",
"format": "SubscriptionTsFileHandler",
"strict": "false",
"loose-range": "",
"start-time": "2025-01-01T00:00:00Z",
"end-time": "2025-02-01T00:00:00Z",
}

with SubscriptionSession(
node_urls=["127.0.0.1:6667"],
username="root",
password="root",
sql_dialect="table",
) as session:
session.create_topic(topic_name, topic_attributes)
try:
consumer = session.create_pull_consumer(
file_save_dir="./exported-tsfiles",
heartbeat_interval_ms=30000,
)
with consumer:
consumer.subscribe(topic_name)
while not consumer.all_topic_messages_have_been_consumed():
for message in consumer.poll(timeout_ms=1000):
print(message.file_path)
consumer.commit(message)
finally:
session.drop_topic(topic_name, if_exists=True)
```

`get_subscriptions(topic_name)` returns the server-reported Subscription identifiers and consumer
groups for that topic. Use `drop_subscription_if_exists(subscription.subscription_id)` to remove a
stale Subscription before dropping its Topic.

Set `sql_dialect="tree"` for tree-model topics. Tree topics can be filtered with the `path`
attribute, while table topics can use `database` and `table` attributes.

### Initialization

* Initialize a Session
Expand Down
17 changes: 17 additions & 0 deletions iotdb-client/client-py/iotdb/Session.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,23 @@ def close(self):
finally:
self.__is_close = True

def _pipe_subscribe(self, request):
"""Send a Subscription RPC on this session's Thrift connection."""
if self.__is_close or self.__client is None:
raise IoTDBConnectionException("Session is not open.")
return self.__client.pipeSubscribe(request)

def _get_server_thrift_max_frame_size(self, fallback):
"""Return the negotiated frame limit when the server exposes it."""
if self.__is_close or self.__client is None:
return fallback
try:
properties = self.__client.getProperties()
except TTransport.TException:
return fallback
server_limit = getattr(properties, "thriftMaxFrameSize", None)
return server_limit if server_limit and server_limit > 0 else fallback

def set_storage_group(self, group_name):
"""
create one database
Expand Down
Loading
Loading