add python example
This commit is contained in:
parent
709c0b9743
commit
6f828bfe4f
|
@ -15,8 +15,7 @@ try:
|
||||||
assert rowsAffected == 0
|
assert rowsAffected == 0
|
||||||
|
|
||||||
# change database. same as execute "USE db"
|
# change database. same as execute "USE db"
|
||||||
rowsAffected = conn.select_db(db)
|
conn.select_db(db)
|
||||||
assert rowsAffected == 0
|
|
||||||
|
|
||||||
# create super table
|
# create super table
|
||||||
rowsAffected = conn.execute(
|
rowsAffected = conn.execute(
|
||||||
|
|
|
@ -1,47 +1,72 @@
|
||||||
import taosws
|
import taosws
|
||||||
|
|
||||||
conn = None
|
db = "power"
|
||||||
|
def prepare():
|
||||||
lineDemo = [
|
conn = None
|
||||||
"meters,groupid=2,location=California.SanFrancisco current=10.3000002f64,voltage=219i32,phase=0.31f64 1626006833639"
|
try:
|
||||||
]
|
|
||||||
|
|
||||||
telnetDemo = ["metric_telnet 1707095283260 4 host=host0 interface=eth0"]
|
|
||||||
|
|
||||||
jsonDemo = [
|
|
||||||
'{"metric": "metric_json","timestamp": 1626846400,"value": 10.3, "tags": {"groupid": 2, "location": "California.SanFrancisco", "id": "d1001"}}'
|
|
||||||
]
|
|
||||||
|
|
||||||
try:
|
|
||||||
conn = taosws.connect(user="root",
|
conn = taosws.connect(user="root",
|
||||||
password="taosdata",
|
password="taosdata",
|
||||||
host="localhost",
|
host="localhost",
|
||||||
port=6041)
|
port=6041)
|
||||||
|
|
||||||
conn.execute("CREATE DATABASE IF NOT EXISTS power")
|
# create database
|
||||||
conn = conn.execute("USE power")
|
rowsAffected = conn.execute(f"CREATE DATABASE IF NOT EXISTS {db}")
|
||||||
|
assert rowsAffected == 0
|
||||||
|
|
||||||
|
except Exception as err:
|
||||||
|
print(f"Failed to create db and table, err:{err}")
|
||||||
|
finally:
|
||||||
|
if conn:
|
||||||
|
conn.close()
|
||||||
|
|
||||||
|
def schemaless_insert():
|
||||||
|
|
||||||
|
conn = None
|
||||||
|
|
||||||
|
lineDemo = [
|
||||||
|
"meters,groupid=2,location=California.SanFrancisco current=10.3000002f64,voltage=219i32,phase=0.31f64 1626006833639"
|
||||||
|
]
|
||||||
|
|
||||||
|
telnetDemo = ["metric_telnet 1707095283260 4 host=host0 interface=eth0"]
|
||||||
|
|
||||||
|
jsonDemo = [
|
||||||
|
'{"metric": "metric_json","timestamp": 1626846400,"value": 10.3, "tags": {"groupid": 2, "location": "California.SanFrancisco", "id": "d1001"}}'
|
||||||
|
]
|
||||||
|
|
||||||
|
try:
|
||||||
|
conn = taosws.connect(user="root",
|
||||||
|
password="taosdata",
|
||||||
|
host="localhost",
|
||||||
|
port=6041,
|
||||||
|
database=db)
|
||||||
|
|
||||||
conn.schemaless_insert(
|
conn.schemaless_insert(
|
||||||
lines=lineDemo,
|
lines=lineDemo,
|
||||||
protocol=taosws.PySchemalessProtocol.Line,
|
protocol=taosws.PySchemalessProtocol.Line,
|
||||||
precision=taosws.PySchemalessPrecision.Millisecond
|
precision=taosws.PySchemalessPrecision.Millisecond,
|
||||||
|
ttl=1,
|
||||||
|
req_id=1,
|
||||||
)
|
)
|
||||||
|
|
||||||
conn.schemaless_insert(
|
conn.schemaless_insert(
|
||||||
lines=telnetDemo,
|
lines=telnetDemo,
|
||||||
protocol=taosws.PySchemalessProtocol.Telnet,
|
protocol=taosws.PySchemalessProtocol.Telnet,
|
||||||
precision=taosws.PySchemalessPrecision.Microsecond
|
precision=taosws.PySchemalessPrecision.Microsecond,
|
||||||
|
ttl=1,
|
||||||
|
req_id=2,
|
||||||
)
|
)
|
||||||
|
|
||||||
conn.schemaless_insert(
|
conn.schemaless_insert(
|
||||||
lines=jsonDemo,
|
lines=jsonDemo,
|
||||||
protocol=taosws.PySchemalessProtocol.Json,
|
protocol=taosws.PySchemalessProtocol.Json,
|
||||||
precision=taosws.PySchemalessPrecision.Millisecond
|
precision=taosws.PySchemalessPrecision.Millisecond,
|
||||||
|
ttl=1,
|
||||||
|
req_id=3,
|
||||||
)
|
)
|
||||||
except Exception as err:
|
|
||||||
|
except Exception as err:
|
||||||
print(f"Failed to insert data with schemaless, err:{err}")
|
print(f"Failed to insert data with schemaless, err:{err}")
|
||||||
|
|
||||||
finally:
|
finally:
|
||||||
if conn:
|
if conn:
|
||||||
conn.close()
|
conn.close()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue