InfluxDB Line Protocol

Introduction

In the InfluxDB Line protocol format, a single line of text is used to represent one row of data. Each line contains 4 parts as shown below.

  1. measurement,tag_set field_set timestamp
  • measurement will be used as the name of the STable Enter a comma (,) between measurement and tag_set.
  • tag_set will be used as tags, with format like <tag_key>=<tag_value>,<tag_key>=<tag_value> Enter a space between tag_set and field_set.
  • field_setwill be used as data columns, with format like <field_key>=<field_value>,<field_key>=<field_value> Enter a space between field_set and timestamp.
  • timestamp is the primary key timestamp corresponding to this row of data

For example:

  1. meters,location=California.LosAngeles,groupid=2 current=13.4,voltage=223,phase=0.29 1648432611249500
InfluxDB Line Protocol - 图1note
  • All the data in tag_set will be converted to NCHAR type automatically .
  • Each data in field_set must be self-descriptive for its data type. For example 1.2f32 means a value 1.2 of float type. Without the “f” type suffix, it will be treated as type double.
  • Multiple kinds of precision can be used for the timestamp field. Time precision can be from nanosecond (ns) to hour (h).
  • 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 created automatically. 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.
  • It is assumed that the order of field_set in a supertable is consistent, meaning that the first record contains all fields and subsequent records store fields in the same order. If the order is not consistent, set smlDataFormat in taos.cfg to false. Otherwise, data will be written out of order and a database error will occur.(smlDataFormat in taos.cfg default to false after version of 3.0.1.3)

For more details please refer to InfluxDB Line Protocol and TDengine Schemaless

Examples

  • 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 LineProtocolExample {
  10. // format: measurement,tag_set field_set timestamp
  11. private static String[] lines = {
  12. "meters,location=California.LosAngeles,groupid=2 current=11.8,voltage=221,phase=0.28 1648432611249000", // micro
  13. // seconds
  14. "meters,location=California.LosAngeles,groupid=2 current=13.4,voltage=223,phase=0.29 1648432611249500",
  15. "meters,location=California.LosAngeles,groupid=3 current=10.8,voltage=223,phase=0.29 1648432611249300",
  16. "meters,location=California.LosAngeles,groupid=3 current=11.3,voltage=221,phase=0.35 1648432611249800",
  17. };
  18. private static Connection getConnection() throws SQLException {
  19. String jdbcUrl = "jdbc:TAOS://localhost:6030?user=root&password=taosdata";
  20. return DriverManager.getConnection(jdbcUrl);
  21. }
  22. private static void createDatabase(Connection conn) throws SQLException {
  23. try (Statement stmt = conn.createStatement()) {
  24. // the default precision is ms (millisecond), but we use us(microsecond) here.
  25. stmt.execute("CREATE DATABASE IF NOT EXISTS test PRECISION 'us'");
  26. stmt.execute("USE test");
  27. }
  28. }
  29. public static void main(String[] args) throws SQLException {
  30. try (Connection conn = getConnection()) {
  31. createDatabase(conn);
  32. SchemalessWriter writer = new SchemalessWriter(conn);
  33. writer.write(lines, SchemalessProtocolType.LINE, SchemalessTimestampType.MICRO_SECONDS);
  34. }
  35. }
  36. }

view source code

  1. import taos
  2. from taos import SmlProtocol, SmlPrecision
  3. lines = ["meters,location=California.LosAngeles,groupid=2 current=11.8,voltage=221,phase=0.28 1648432611249000",
  4. "meters,location=California.LosAngeles,groupid=2 current=13.4,voltage=223,phase=0.29 1648432611249500",
  5. "meters,location=California.LosAngeles,groupid=3 current=10.8,voltage=223,phase=0.29 1648432611249300",
  6. "meters,location=California.LosAngeles,groupid=3 current=11.3,voltage=221,phase=0.35 1648432611249800",
  7. ]
  8. def get_connection():
  9. # create connection use firstEP in taos.cfg.
  10. return taos.connect()
  11. def create_database(conn):
  12. # the default precision is ms (microsecond), but we use us(microsecond) here.
  13. conn.execute("CREATE DATABASE test precision 'us'")
  14. conn.execute("USE test")
  15. def insert_lines(conn):
  16. affected_rows = conn.schemaless_insert(
  17. lines, SmlProtocol.LINE_PROTOCOL, SmlPrecision.MICRO_SECONDS)
  18. print(affected_rows) # 8
  19. if __name__ == '__main__':
  20. connection = get_connection()
  21. try:
  22. create_database(connection)
  23. insert_lines(connection)
  24. finally:
  25. connection.close()

view source code

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

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,location=California.LosAngeles,groupid=2 current=11.8,voltage=221,phase=0.28 1648432611249",
  13. "meters,location=California.LosAngeles,groupid=2 current=13.4,voltage=223,phase=0.29 1648432611250",
  14. "meters,location=California.LosAngeles,groupid=3 current=10.8,voltage=223,phase=0.29 1648432611249",
  15. "meters,location=California.LosAngeles,groupid=3 current=11.3,voltage=221,phase=0.35 1648432611250",
  16. ];
  17. cursor.schemalessInsert(
  18. lines,
  19. taos.SCHEMALESS_PROTOCOL.TSDB_SML_LINE_PROTOCOL,
  20. taos.SCHEMALESS_PRECISION.TSDB_SML_TIMESTAMP_MILLI_SECONDS
  21. );
  22. }
  23. try {
  24. createDatabase();
  25. insertData();
  26. } finally {
  27. cursor.close();
  28. conn.close();
  29. }

view source code

  1. using TDengineDriver;
  2. namespace TDengineExample
  3. {
  4. internal class InfluxDBLineExample
  5. {
  6. static void Main()
  7. {
  8. IntPtr conn = GetConnection();
  9. PrepareDatabase(conn);
  10. string[] lines = {
  11. "meters,location=California.LosAngeles,groupid=2 current=11.8,voltage=221,phase=0.28 1648432611249",
  12. "meters,location=California.LosAngeles,groupid=2 current=13.4,voltage=223,phase=0.29 1648432611250",
  13. "meters,location=California.LosAngeles,groupid=3 current=10.8,voltage=223,phase=0.29 1648432611249",
  14. "meters,location=California.LosAngeles,groupid=3 current=11.3,voltage=221,phase=0.35 1648432611250"
  15. };
  16. IntPtr res = TDengine.SchemalessInsert(conn, lines, lines.Length, (int)TDengineSchemalessProtocol.TSDB_SML_LINE_PROTOCOL, (int)TDengineSchemalessPrecision.TSDB_SML_TIMESTAMP_MILLI_SECONDS);
  17. if (TDengine.ErrorNo(res) != 0)
  18. {
  19. throw new Exception("SchemalessInsert failed since " + TDengine.Error(res));
  20. }
  21. else
  22. {
  23. int affectedRows = TDengine.AffectRows(res);
  24. Console.WriteLine($"SchemalessInsert success, affected {affectedRows} rows");
  25. }
  26. TDengine.FreeResult(res);
  27. }
  28. static IntPtr GetConnection()
  29. {
  30. string host = "localhost";
  31. short port = 6030;
  32. string username = "root";
  33. string password = "taosdata";
  34. string dbname = "";
  35. var conn = TDengine.Connect(host, username, password, dbname, port);
  36. if (conn == IntPtr.Zero)
  37. {
  38. throw new Exception("Connect to TDengine failed");
  39. }
  40. else
  41. {
  42. Console.WriteLine("Connect to TDengine success");
  43. }
  44. return conn;
  45. }
  46. static void PrepareDatabase(IntPtr conn)
  47. {
  48. IntPtr res = TDengine.Query(conn, "CREATE DATABASE test");
  49. if (TDengine.ErrorNo(res) != 0)
  50. {
  51. throw new Exception("failed to create database, reason: " + TDengine.Error(res));
  52. }
  53. res = TDengine.Query(conn, "USE test");
  54. if (TDengine.ErrorNo(res) != 0)
  55. {
  56. throw new Exception("failed to change database, reason: " + TDengine.Error(res));
  57. }
  58. }
  59. }
  60. }

view source code

  1. int main() {
  2. TAOS *taos = taos_connect("localhost", "root", "taosdata", "", 0);
  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[] = {"meters,location=California.LosAngeles,groupid=2 current=11.8,voltage=221,phase=0.28 1648432611249",
  11. "meters,location=California.LosAngeles,groupid=2 current=13.4,voltage=223,phase=0.29 1648432611250",
  12. "meters,location=California.LosAngeles,groupid=3 current=10.8,voltage=223,phase=0.29 1648432611249",
  13. "meters,location=California.LosAngeles,groupid=3 current=11.3,voltage=221,phase=0.35 1648432611250"};
  14. TAOS_RES *res = taos_schemaless_insert(taos, lines, 4, TSDB_SML_LINE_PROTOCOL, TSDB_SML_TIMESTAMP_MILLI_SECONDS);
  15. if (taos_errno(res) != 0) {
  16. printf("failed to insert schema-less data, reason: %s\n", taos_errstr(res));
  17. } else {
  18. int affectedRows = taos_affected_rows(res);
  19. printf("successfully inserted %d rows\n", affectedRows);
  20. }
  21. taos_free_result(res);
  22. taos_close(taos);
  23. taos_cleanup();
  24. }
  25. // output:
  26. // successfully inserted 4 rows

view source code

Query Examples

If you want query the data of location=California.LosAngeles,groupid=2,here is the query SQL:

  1. SELECT * FROM meters WHERE location = "California.LosAngeles" AND groupid = 2;