OpenTSDB JSON Protocol

Similar to OpenTSDB line protocol, will be used as the STable name, timestamp is the timestamp to be used, value represents the metric collected, tags are the tag sets.

Please refer to for more details.

note
  • In JSON protocol, strings will be converted to NCHAR type and numeric values will be converted to double type.
  • The child table name is created automatically in a rule to guarantee its uniqueness. But you can configure smlChildTableName in taos.cfg to specify a tag value as the table names if the tag value is unique globally. For example, if a tag is called tname and you set smlChildTableName=tname in taos.cfg, when you insert st,tname=cpu1,t1=4 c1=3 1626006833639000000, the child table cpu1 will be automatically created. Note that if multiple rows have the same tname but different tag_set values, the tag_set of the first row is used to create the table and the others are ignored.
  • Java
  • Python
  • Go
  • Node.js
  • C#
  • C
  1. package com.taos.example;
  2. import com.taosdata.jdbc.SchemalessWriter;
  3. import com.taosdata.jdbc.enums.SchemalessProtocolType;
  4. import com.taosdata.jdbc.enums.SchemalessTimestampType;
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9. public class JSONProtocolExample {
  10. private static Connection getConnection() throws SQLException {
  11. String jdbcUrl = "jdbc:TAOS://localhost:6030?user=root&password=taosdata";
  12. return DriverManager.getConnection(jdbcUrl);
  13. }
  14. private static void createDatabase(Connection conn) throws SQLException {
  15. try (Statement stmt = conn.createStatement()) {
  16. stmt.execute("CREATE DATABASE IF NOT EXISTS test");
  17. stmt.execute("USE test");
  18. }
  19. }
  20. private static String getJSONData() {
  21. return "[{\"metric\": \"meters.current\", \"timestamp\": 1648432611249, \"value\": 10.3, \"tags\": {\"location\": \"California.SanFrancisco\", \"groupid\": 2}}," +
  22. " {\"metric\": \"meters.voltage\", \"timestamp\": 1648432611249, \"value\": 219, \"tags\": {\"location\": \"California.LosAngeles\", \"groupid\": 1}}, " +
  23. "{\"metric\": \"meters.current\", \"timestamp\": 1648432611250, \"value\": 12.6, \"tags\": {\"location\": \"California.SanFrancisco\", \"groupid\": 2}}," +
  24. " {\"metric\": \"meters.voltage\", \"timestamp\": 1648432611250, \"value\": 221, \"tags\": {\"location\": \"California.LosAngeles\", \"groupid\": 1}}]";
  25. }
  26. public static void main(String[] args) throws SQLException {
  27. try (Connection conn = getConnection()) {
  28. createDatabase(conn);
  29. SchemalessWriter writer = new SchemalessWriter(conn);
  30. String jsonData = getJSONData();
  31. writer.write(jsonData, SchemalessProtocolType.JSON, SchemalessTimestampType.NOT_CONFIGURED);
  32. }
  33. }
  34. }
  1. import json
  2. import taos
  3. from taos import SmlProtocol, SmlPrecision
  4. lines = [{"metric": "meters.current", "timestamp": 1648432611249, "value": 10.3, "tags": {"location": "California.SanFrancisco", "groupid": 2}},
  5. {"metric": "meters.voltage", "timestamp": 1648432611249, "value": 219,
  6. "tags": {"location": "California.LosAngeles", "groupid": 1}},
  7. {"metric": "meters.current", "timestamp": 1648432611250, "value": 12.6,
  8. "tags": {"location": "California.SanFrancisco", "groupid": 2}},
  9. {"metric": "meters.voltage", "timestamp": 1648432611250, "value": 221, "tags": {"location": "California.LosAngeles", "groupid": 1}}]
  10. def get_connection():
  11. return taos.connect()
  12. def create_database(conn):
  13. conn.execute("CREATE DATABASE test")
  14. conn.execute("USE test")
  15. def insert_lines(conn):
  16. global lines
  17. # note: the first parameter must be a list with only one element.
  18. affected_rows = conn.schemaless_insert(
  19. print(affected_rows) # 4
  20. if __name__ == '__main__':
  21. connection = get_connection()
  22. try:
  23. create_database(connection)
  24. insert_lines(connection)
  25. finally:
  26. connection.close()

view source code

  1. const taos = require("@tdengine/client");
  2. const conn = taos.connect({
  3. host: "localhost",
  4. });
  5. const cursor = conn.cursor();
  6. function createDatabase() {
  7. cursor.execute("CREATE DATABASE test");
  8. cursor.execute("USE test");
  9. }
  10. function insertData() {
  11. const lines = [
  12. {
  13. metric: "meters.current",
  14. timestamp: 1648432611249,
  15. value: 10.3,
  16. tags: { location: "California.SanFrancisco", groupid: 2 },
  17. },
  18. {
  19. metric: "meters.voltage",
  20. timestamp: 1648432611249,
  21. value: 219,
  22. tags: { location: "California.LosAngeles", groupid: 1 },
  23. },
  24. {
  25. metric: "meters.current",
  26. timestamp: 1648432611250,
  27. value: 12.6,
  28. tags: { location: "California.SanFrancisco", groupid: 2 },
  29. },
  30. {
  31. metric: "meters.voltage",
  32. timestamp: 1648432611250,
  33. value: 221,
  34. tags: { location: "California.LosAngeles", groupid: 1 },
  35. },
  36. ];
  37. cursor.schemalessInsert(
  38. [JSON.stringify(lines)],
  39. taos.SCHEMALESS_PROTOCOL.TSDB_SML_JSON_PROTOCOL,
  40. taos.SCHEMALESS_PRECISION.TSDB_SML_TIMESTAMP_NOT_CONFIGURED
  41. );
  42. }
  43. try {
  44. createDatabase();
  45. insertData();
  46. } finally {
  47. cursor.close();
  48. conn.close();
  49. }
  1. using TDengineDriver;
  2. namespace TDengineExample
  3. {
  4. internal class OptsJsonExample
  5. {
  6. static void Main()
  7. {
  8. try
  9. PrepareDatabase(conn);
  10. string[] lines = { "[{\"metric\": \"meters.current\", \"timestamp\": 1648432611249, \"value\": 10.3, \"tags\": {\"location\": \"California.SanFrancisco\", \"groupid\": 2}}," +
  11. " {\"metric\": \"meters.voltage\", \"timestamp\": 1648432611249, \"value\": 219, \"tags\": {\"location\": \"California.LosAngeles\", \"groupid\": 1}}, " +
  12. "{\"metric\": \"meters.current\", \"timestamp\": 1648432611250, \"value\": 12.6, \"tags\": {\"location\": \"California.SanFrancisco\", \"groupid\": 2}}," +
  13. " {\"metric\": \"meters.voltage\", \"timestamp\": 1648432611250, \"value\": 221, \"tags\": {\"location\": \"California.LosAngeles\", \"groupid\": 1}}]"
  14. };
  15. IntPtr res = TDengine.SchemalessInsert(conn, lines, 1, (int)TDengineSchemalessProtocol.TSDB_SML_JSON_PROTOCOL, (int)TDengineSchemalessPrecision.TSDB_SML_TIMESTAMP_NOT_CONFIGURED);
  16. if (TDengine.ErrorNo(res) != 0)
  17. {
  18. throw new Exception("SchemalessInsert failed since " + TDengine.Error(res));
  19. }
  20. else
  21. {
  22. int affectedRows = TDengine.AffectRows(res);
  23. Console.WriteLine($"SchemalessInsert success, affected {affectedRows} rows");
  24. }
  25. TDengine.FreeResult(res);
  26. }
  27. finally
  28. {
  29. TDengine.Close(conn);
  30. }
  31. }
  32. static IntPtr GetConnection()
  33. {
  34. string host = "localhost";
  35. short port = 6030;
  36. string username = "root";
  37. string password = "taosdata";
  38. string dbname = "";
  39. var conn = TDengine.Connect(host, username, password, dbname, port);
  40. if (conn == IntPtr.Zero)
  41. {
  42. throw new Exception("Connect to TDengine failed");
  43. }
  44. else
  45. {
  46. Console.WriteLine("Connect to TDengine success");
  47. }
  48. return conn;
  49. }
  50. static void PrepareDatabase(IntPtr conn)
  51. {
  52. IntPtr res = TDengine.Query(conn, "CREATE DATABASE test");
  53. if (TDengine.ErrorNo(res) != 0)
  54. {
  55. throw new Exception("failed to create database, reason: " + TDengine.Error(res));
  56. }
  57. res = TDengine.Query(conn, "USE test");
  58. if (TDengine.ErrorNo(res) != 0)
  59. {
  60. throw new Exception("failed to change database, reason: " + TDengine.Error(res));
  61. }
  62. }
  63. }
  64. }

view source code

  1. taos> use test;
  2. Database changed.
  3. taos> show stables;
  4. name |
  5. =================================
  6. meters.current |
  7. meters.voltage |
  8. Query OK, 2 row(s) in set (0.001954s)
  9. taos> select * from `meters.current`;
  10. _ts | _value | groupid | location |
  11. ===================================================================================================================
  12. 2022-03-28 09:56:51.249 | 10.300000000 | 2.000000000 | California.SanFrancisco |
  13. 2022-03-28 09:56:51.250 | 12.600000000 | 2.000000000 | California.SanFrancisco |
  14. Query OK, 2 row(s) in set (0.004076s)

If you want query the data of “tags”: {“location”: “California.LosAngeles”, “groupid”: 1},here is the query SQL: