OpenTSDB Line Protocol

    • will be used as the STable name.
    • timestamp is the timestamp of current row of data. The time precision will be determined automatically based on the length of the timestamp. Second and millisecond time precision are supported.
    • value is a metric which must be a numeric value, The corresponding column name is “value”.
    • The last part is the tag set separated by spaces, all tags will be converted to NCHAR type automatically.

    For example:

    1. meters.current 1648432611250 11.3 location=California.LosAngeles groupid=3
    • 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. Please refer to for more details.
    • 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 TelnetLineProtocolExample {
    10. // format: <metric> <timestamp> <value> <tagk_1>=<tagv_1>[ <tagk_n>=<tagv_n>]
    11. private static String[] lines = { "meters.current 1648432611249 10.3 location=California.SanFrancisco groupid=2",
    12. "meters.current 1648432611250 12.6 location=California.SanFrancisco groupid=2",
    13. "meters.current 1648432611249 10.8 location=California.LosAngeles groupid=3",
    14. "meters.current 1648432611250 11.3 location=California.LosAngeles groupid=3",
    15. "meters.voltage 1648432611249 219 location=California.SanFrancisco groupid=2",
    16. "meters.voltage 1648432611250 218 location=California.SanFrancisco groupid=2",
    17. "meters.voltage 1648432611249 221 location=California.LosAngeles groupid=3",
    18. "meters.voltage 1648432611250 217 location=California.LosAngeles groupid=3",
    19. };
    20. private static Connection getConnection() throws SQLException {
    21. String jdbcUrl = "jdbc:TAOS://localhost:6030?user=root&password=taosdata";
    22. return DriverManager.getConnection(jdbcUrl);
    23. }
    24. private static void createDatabase(Connection conn) throws SQLException {
    25. try (Statement stmt = conn.createStatement()) {
    26. // the default precision is ms (microsecond), but we use us(microsecond) here.
    27. stmt.execute("CREATE DATABASE IF NOT EXISTS test precision 'us'");
    28. stmt.execute("USE test");
    29. }
    30. }
    31. public static void main(String[] args) throws SQLException {
    32. try (Connection conn = getConnection()) {
    33. createDatabase(conn);
    34. SchemalessWriter writer = new SchemalessWriter(conn);
    35. writer.write(lines, SchemalessProtocolType.TELNET, SchemalessTimestampType.NOT_CONFIGURED);
    36. }
    37. }
    38. }

    1. package main
    2. import (
    3. "github.com/taosdata/driver-go/v3/af"
    4. )
    5. _, err := conn.Exec("CREATE DATABASE test")
    6. if err != nil {
    7. panic(err)
    8. }
    9. _, err = conn.Exec("USE test")
    10. if err != nil {
    11. panic(err)
    12. }
    13. }
    14. func main() {
    15. conn, err := af.Open("localhost", "root", "taosdata", "", 6030)
    16. if err != nil {
    17. log.Fatalln("fail to connect, err:", err)
    18. }
    19. defer conn.Close()
    20. prepareDatabase(conn)
    21. var lines = []string{
    22. "meters.current 1648432611249 10.3 location=California.SanFrancisco groupid=2",
    23. "meters.current 1648432611250 12.6 location=California.SanFrancisco groupid=2",
    24. "meters.current 1648432611249 10.8 location=California.LosAngeles groupid=3",
    25. "meters.current 1648432611250 11.3 location=California.LosAngeles groupid=3",
    26. "meters.voltage 1648432611249 219 location=California.SanFrancisco groupid=2",
    27. "meters.voltage 1648432611250 218 location=California.SanFrancisco groupid=2",
    28. "meters.voltage 1648432611249 221 location=California.LosAngeles groupid=3",
    29. "meters.voltage 1648432611250 217 location=California.LosAngeles groupid=3",
    30. }
    31. err = conn.OpenTSDBInsertTelnetLines(lines)
    32. if err != nil {
    33. log.Fatalln("insert error:", err)
    34. }
    35. }

    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. "meters.current 1648432611249 10.3 location=California.SanFrancisco groupid=2",
    13. "meters.current 1648432611250 12.6 location=California.SanFrancisco groupid=2",
    14. "meters.current 1648432611249 10.8 location=California.LosAngeles groupid=3",
    15. "meters.current 1648432611250 11.3 location=California.LosAngeles groupid=3",
    16. "meters.voltage 1648432611249 219 location=California.SanFrancisco groupid=2",
    17. "meters.voltage 1648432611250 218 location=California.SanFrancisco groupid=2",
    18. "meters.voltage 1648432611249 221 location=California.LosAngeles groupid=3",
    19. "meters.voltage 1648432611250 217 location=California.LosAngeles groupid=3",
    20. ];
    21. cursor.schemalessInsert(
    22. taos.SCHEMALESS_PROTOCOL.TSDB_SML_TELNET_PROTOCOL,
    23. taos.SCHEMALESS_PRECISION.TSDB_SML_TIMESTAMP_NOT_CONFIGURED
    24. }
    25. try {
    26. createDatabase();
    27. insertData();
    28. } finally {
    29. cursor.close();
    30. conn.close();
    31. }

    1. int main() {
    2. TAOS *taos = taos_connect("localhost", "root", "taosdata", "", 6030);
    3. if (taos == NULL) {
    4. printf("failed to connect to server\n");
    5. exit(EXIT_FAILURE);
    6. }
    7. executeSQL(taos, "DROP DATABASE IF EXISTS test");
    8. executeSQL(taos, "CREATE DATABASE test");
    9. executeSQL(taos, "USE test");
    10. char *lines[] = {
    11. "meters.current 1648432611249 10.3 location=California.SanFrancisco groupid=2",
    12. "meters.current 1648432611250 12.6 location=California.SanFrancisco groupid=2",
    13. "meters.current 1648432611249 10.8 location=California.LosAngeles groupid=3",
    14. "meters.current 1648432611250 11.3 location=California.LosAngeles groupid=3",
    15. "meters.voltage 1648432611249 219 location=California.SanFrancisco groupid=2",
    16. "meters.voltage 1648432611250 218 location=California.SanFrancisco groupid=2",
    17. "meters.voltage 1648432611249 221 location=California.LosAngeles groupid=3",
    18. "meters.voltage 1648432611250 217 location=California.LosAngeles groupid=3",
    19. };
    20. TAOS_RES *res = taos_schemaless_insert(taos, lines, 8, TSDB_SML_TELNET_PROTOCOL, TSDB_SML_TIMESTAMP_NOT_CONFIGURED);
    21. if (taos_errno(res) != 0) {
    22. printf("failed to insert schema-less data, reason: %s\n", taos_errstr(res));
    23. } else {
    24. int affectedRow = taos_affected_rows(res);
    25. printf("successfully inserted %d rows\n", affectedRow);
    26. }
    27. taos_free_result(res);
    28. taos_close(taos);
    29. taos_cleanup();
    30. }
    31. // output:
    32. // successfully inserted 8 rows

    view source code

    2 STables will be created automatically and each STable has 4 rows of data in the above sample 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.002544s)
    9. taos> select tbname, * from `meters.current`;
    10. tbname | _ts | _value | groupid | location |
    11. ==================================================================================================================================
    12. t_0e7bcfa21a02331c06764f275... | 2022-03-28 09:56:51.249 | 10.800000000 | 3 | California.LosAngeles |
    13. t_0e7bcfa21a02331c06764f275... | 2022-03-28 09:56:51.250 | 11.300000000 | 3 | California.LosAngeles |
    14. t_7e7b26dd860280242c6492a16... | 2022-03-28 09:56:51.249 | 10.300000000 | 2 | California.SanFrancisco |
    15. t_7e7b26dd860280242c6492a16... | 2022-03-28 09:56:51.250 | 12.600000000 | 2 | California.SanFrancisco |
    16. Query OK, 4 row(s) in set (0.005399s)