Package: timeplus
Timeplus Python ClientTimeplusis a real-time streaming data analytic platform.  Timeplus python client provides basic functionalities to interact with Timeplus cloud, to manage the streaming analytic work loads.InstallationpipinstalltimeplusDB APITimeplus python client support DB API defined inPython Database API Specification v2.0.fromtimeplus.dbapiimportconnectapi_key="your_timeplus_apikey"api_address="us.timeplus.cloud"workspace="your_timeplus_workspace_id"# create a connection using host/password/workspaceconn=connect(host=api_address,password=api_key,path=workspace)# run a streaming querycursor=conn.execute("select * from car_live_data")# get first result from the qeurynext_result=cusor.next()# get next one result from the qeuryrow1=cursor.fetchone()# get next three resultrows=cursor.fetchmany(3)Note, as the streaming query result is unbounded, the cursor will not end, fetch will be blocked is there is no new query result.SQLAchamyTimeplus python client has implemeted aSQLAlchemydialect to run queries, so user can use it with SQLAlchemy API.importosfromsqlalchemyimportcreate_engine,text,select,MetaData,Tablefromsqlalchemy.dialectsimportregistry# register timeplus dialectregistry.register("timeplus","timeplus.sqlalchemy","TimeplusDialect")api_key=os.environ.get("TIMEPLUS_API_KEY")api_address="dev.timeplus.cloud"port=443workspace=os.environ.get("TIMEPLUS_WORKSPACE")or"tp-demo"# create a new engineengine=create_engine(f"timeplus://:{api_key}@{api_address}:{port}/{workspace}")# execute streaming sqlwithengine.connect()asconnection:result=connection.execute(text("select * from car_live_data"))count=0max=10forrowinresult:print(f"got one row :{row}")count+=1ifcount>=max:break# execute statement using table from metadatametadata_obj=MetaData()car_table=Table(table_name,metadata_obj,autoload_with=engine)print(f"reflected table is{car_table}")print(f"cols is{[(c.name,c.type)forcincar_table.columns]}")stmt=select(car_table).where(car_table.c.cid=="c00001")print(stmt)withengine.connect()asconn:count=0max=3forrowinconn.execute(stmt):print(f"got one row from query{row}")count+=1ifcount>=max:breakREST APITimeplus python client also provides resources wrapper which can be used to call theTimeplus REST APIthrough python object.here is a list of all supported resourcesResourceSupported MethodsStreamcreate,list,get,delete,ingest,existQuerycreate,list,get,delete,cancel,analyzeSourcecreate,list,get,delete,start,stopSinkcreate,list,get,delete,start,stopViewcreate,list,get,delete,existUDFlistAlertlistDashboardlistqueryRun streaming query and fetch the query result with query metrics.importosimporttracebackimportjsonfrompprintimportpprintfromtimeplusimportQuery,Environmentapi_key=os.environ.get("TIMEPLUS_API_KEY")api_address=os.environ.get("TIMEPLUS_HOST")workspace=os.environ.get("TIMEPLUS_WORKSPACE")# Configure API key and addressenv=Environment().address(api_address).workspace(workspace).apikey(api_key)try:# list all qeuriesquery_list=Query(env=env).list()pprint(f"there are{len(query_list)}queries ")# create a new queryquery=(Query(env=env).sql(query="SELECT * FROM car_live_data")# .batching_policy(1000, 1000).create())pprint(f"query with metadata{json.dumps(query.metadata())}")# query header is the colume definitions of query result table# it is a list of name/value pair# for example : [{'name': 'in_use', 'type': 'bool'}, {'name': 'speed', 'type': 'float32'}]query_header=query.header()pprint(f"query with header{query.header()}")# iterate query resultlimit=3count=0# query.result() is an iterator which will pull all the query result in small batches# the iterator will continously pulling query result# for streaming query, the iterator will not end until user cancel the queryforeventinquery.result():# metric event return result time query metrics# a sample metrics event:# {'count': 117, 'eps': 75, 'processing_time': 1560,# 'last_event_time': 1686237113265, 'response_time': 861,# 'scanned_rows': 117, 'scanned_bytes': 7605}ifevent.event=="metrics":pprint(json.loads(event.data))# message event contains query result which is an array of arrays# representing multiple query result rows# a sample message event:# [[True,-73.857],[False, 84.1]]ifevent.event=="message":pprint(json.loads(event.data))count+=1ifcount>=limit:breakquery.cancel()query.delete()exceptExceptionase:pprint(e)traceback.print_exc()streamCreate/list/get/delete of streamsimportosimporttracebackimportjsonfrompprintimportpprintfromtimeplusimportStream,Environmentapi_key=os.environ.get("TIMEPLUS_API_KEY")api_address=os.environ.get("TIMEPLUS_HOST")worksapce=os.environ.get("TIMEPLUS_WORKSAPCE")# Configure API key and addressenv=Environment().address(api_address).apikey(api_key).workspace(worksapce)try:# list all streamsstream_list=Stream(env=env).list()pprint(f"there are{len(stream_list)}streams ")# create a new streamstream=(Stream(env=env).name("test").column("time","datetime64(3)").column("data","string").create())stream_list=Stream(env=env).list()pprint(f"there are{len(stream_list)}streams after create")pprint(f"created stream is{stream.metadata()}; type is{type(stream.metadata())}")a_stream=Stream(env=env).name("test").get()pprint(f"get stream is{a_stream.metadata()}; type is{type(a_stream.metadata())}")stream.delete()stream_list=Stream(env=env).list()pprint(f"there are{len(stream_list)}streams after delete")exceptExceptionase:pprint(e)traceback.print_exc()ingestIngest data into streamsdefault ingeststream=(Stream(env=env).name("test_ingest").column("time","datetime64(3)").column("data","string").create())stream.ingest(["time","data"],[[datetime.datetime.now(),"abcd"]])ingest json streamsstream=(Stream(env=env).name("test_ingest").column("a","integer").column("b","string").create())payload="""{"a":2,"b":"hello"}{"a":1,"b":"world"}"""stream.ingest(payload=payload,format="streaming")ingest one raw event with multiple linesstream=Stream(env=env).name("test_ingest_raw").column("raw","string").create()payload="""first linesecond line"""stream.ingest(payload=payload,format="raw")ingest multiple lines jsonstream=Stream(env=env).name("test_ingest_lines").column("raw","string").create()payload='{"a":1,"b":"world"}\n{"a":2,"b":"hello"}'stream.ingest(payload=payload,format="lines")ExamplesMore sample code can be foundhere