tencent cloud

Connecting to an Instance and Writing Data
Last updated: 2025-04-30 16:33:26
Connecting to an Instance and Writing Data
Last updated: 2025-04-30 16:33:26
This section takes the Linux operating system as an example to help you connect to the TencentDB for CTSDB 3.0 instance in HTTP mode through the private network of Cloud Virtual Machine (CVM).

Prerequisites

Apply for a Linux CVM within the same region and VPC as the CTSDB 3.0.
Database instance is created, and its status is Running.
Obtain the private IP address and network port of the instance. For detailed operations, see Viewing Instances.
Obtain the access account and password information of the instance. For detailed operations, see Account Management.

Directions

Step 1: Preparing the Data

The following are time series data about vehicles. Each line represents information of a vehicle, including the vehicle's ID, located city, type, speed, and temperature. The timestamp is at the end of each line, indicating that this information was recorded at the corresponding timestamp. Now, these data will be written into the created database instance.
car,id=0,city=city_0,type=type_0 speed=100,temp=20 1675236656000000000
car,id=1,city=city_1,type=type_1 speed=101,temp=21 1675236656000000000
car,id=2,city=city_0,type=type_2 speed=102,temp=22 1675236656000000000
car,id=3,city=city_1,type=type_0 speed=103,temp=23 1675236656000000000
car,id=4,city=city_0,type=type_1 speed=104,temp=24 1675236656000000000
car,id=5,city=city_1,type=type_2 speed=105,temp=25 1675236656000000000
car,id=0,city=city_0,type=type_0 speed=106,temp=26 1675236657000000000
car,id=1,city=city_1,type=type_1 speed=107,temp=27 1675236657000000000
car,id=2,city=city_0,type=type_2 speed=108,temp=28 1675236657000000000
car,id=3,city=city_1,type=type_0 speed=100,temp=29 1675236657000000000
car,id=4,city=city_0,type=type_1 speed=101,temp=30 1675236657000000000
car,id=5,city=city_1,type=type_2 speed=102,temp=20 1675236657000000000
car,id=0,city=city_0,type=type_0 speed=103,temp=21 1675236658000000000
car,id=1,city=city_1,type=type_1 speed=104,temp=22 1675236658000000000
car,id=2,city=city_0,type=type_2 speed=105,temp=23 1675236658000000000
car,id=3,city=city_1,type=type_0 speed=106,temp=24 1675236658000000000
car,id=4,city=city_0,type=type_1 speed=107,temp=25 1675236658000000000
car,id=5,city=city_1,type=type_2 speed=108,temp=26 1675236658000000000
car,id=0,city=city_0,type=type_0 speed=100,temp=27 1675236659000000000
car,id=1,city=city_1,type=type_1 speed=101,temp=28 1675236659000000000
car,id=2,city=city_0,type=type_2 speed=102,temp=29 1675236659000000000
car,id=3,city=city_1,type=type_0 speed=103,temp=30 1675236659000000000
car,id=4,city=city_0,type=type_1 speed=104,temp=20 1675236659000000000
car,id=5,city=city_1,type=type_2 speed=105,temp=21 1675236659000000000
car,id=0,city=city_0,type=type_0 speed=106,temp=22 1675236660000000000
car,id=1,city=city_1,type=type_1 speed=107,temp=23 1675236660000000000
car,id=2,city=city_0,type=type_2 speed=108,temp=24 1675236660000000000
car,id=3,city=city_1,type=type_0 speed=100,temp=25 1675236660000000000
car,id=4,city=city_0,type=type_1 speed=101,temp=26 1675236660000000000
car,id=5,city=city_1,type=type_2 speed=102,temp=27 1675236660000000000
car,id=0,city=city_0,type=type_0 speed=103,temp=28 1675236661000000000
car,id=1,city=city_1,type=type_1 speed=104,temp=29 1675236661000000000
car,id=2,city=city_0,type=type_2 speed=105,temp=30 1675236661000000000
car,id=3,city=city_1,type=type_0 speed=106,temp=20 1675236661000000000
car,id=4,city=city_0,type=type_1 speed=107,temp=21 1675236661000000000
car,id=5,city=city_1,type=type_2 speed=108,temp=22 1675236661000000000

Step 2: Logging in to the CVM

1. Log in to the CVM Console.
2. In the left sidebar, select Instance.
3. At the top of the instance management page, select a region.
4. Find the applied CVM in the instance list and click Log in in the Operation column on the right.
5. Enter the username and password set when applying for a CVM to log in to the CVM.

Step 3: Creating a Database

The syntax format of CREATE DATABASE is as follows:
CREATE DATABASE <database_name> [WITH [DURATION <duration>[default unlimited]>]]
database_name: Refers to the database name.
DURATION: Refers to the Time To Live (TTL), in which the unit is day, the value adopts non-zero integer, and the parameter is optional. If TTL is not enabled, this parameter is not configured. If TTL is enabled, the data in the database will be automatically deleted after reaching the expiration time. For example, DURATION 180d means that the data in the database will be automatically deleted after reaching 180 days.
The execution format for creating database time_series_test_db is as follows.
curl --get http://${influxdb_ip}:8086/query \\
--user "time_series_test_account":"test_password" \\
--data-urlencode "pretty=true" \\
--data-urlencode 'q=CREATE DATABASE time_series_test_db'
${influxdb_ip}: Refers to the private IP address of the database instance. The default port is 8086. Replace it according to the actual situation.
user: Refers to the account and password information for accessing the instance. The default account is named after the instance ID. You can view account information on the Account Management page in the Console. For detailed operations, see Account Management.
data-urlencode "pretty=true": When pretty is set to true, the information will be returned in JSON Pretty Print format.
data-urlencode: Refers to the syntax format of CREATE DATABASE, such as 'q=CREATE DATABASE time_series_test_db', 'q=CREATE DATABASE time_series_test_db WITH DURATION 180d'.
The following information is returned, indicating that it is under creation.
{
"code": "invalid",
"message": "Tsdb create database operation is executing, please try again later(maybe failture)."
}
The following information is returned, indicating that it is created successfully.
{
"results": [
{
"statement_id": 0
}
]
}
Execute show database to view the created database, with the execution format as follows:
curl --get http://${influxdb_ip}:8086/query \\
--user "time_series_test_account":"test_password" \\
--data-urlencode "pretty=true" \\
--data-urlencode 'q=show databases'
The execution example is as follows:
[root@VM-16-41-centos ~]# curl --get http://10.16.12.8:8086/query \\
> --user "ctsdbi-8ym3****":"test@123" \\
> --data-urlencode "pretty=true" \\
> --data-urlencode 'q=show databases'
{
"results": [
{
"statement_id": 0,
"series": [
{
"name": "databases",
"columns": [
"name"
],
"columns_types": [
"string"
],
"values": [
[
"test0407"
],
[
"time_series_test_db"
],
[
"_internal"
],
[
"TestAlvinDatabase"
],
[
"time_series_test_db0"
]
]
}
]
}
]
}

Step 4: Writing Data

Write data to the database time_series_test_db.
curl -v --data-binary @/tmp/line_protocol_sample.txt \\
--user "time_series_test_account":"test_password" \\
-H "Content-Type: text/plain" \\
-H "Accept: application/json" -X POST \\
"http://${influxdb_ip}:8086/write?db=time_series_test_db&precision=ns"
@/tmp/line_protocol_sample.txt: Refers to the relative path of the file for data writing.
user: Refers to the access account and password for configuring the instance.
influxdb_ip: Refers to the instance private IP address.
db: Refers to the database name.
precision: Refers to the precision of time data. The available precision includes nanosecond (ns), microsecond (?s), millisecond (ms), second (s), minute (m), and hour (h).
The execution example is as follows. Write time series data in document /tmp/test.txt to database time_series_test_db.
[root@VM-16-41-centos tmp]# curl -v --data-binary @/tmp/test.txt \\
> --user "ctsdbi-8ym3****":"test@123" \\
> -H "Content-Type: text/plain" \\
> -H "Accept: application/json" -X POST \\
> "http://10.16.12.8:8086/write?db=time_series_test_db&precision=ns"
* About to connect() to 10.16.12.8 port 8086 (#0)
* Trying 10.16.12.8...
* Connected to 10.16.12.8 (10.16.12.8) port 8086 (#0)
* Server auth using Basic with user 'ctsdbi-8ym3****'
> POST /write?db=time_series_test_db&precision=ns HTTP/1.1
> Authorization: Basic Y3RzZGJpLTh5********************
> User-Agent: curl/7.29.0
> Host: 10.16.12.8:8086
> Content-Type: text/plain
> Accept: application/json
> Content-Length: 2552
> Expect: 100-continue
>
* Done waiting for 100-continue
< HTTP/1.1 204 No Content
< Date: Mon, 10 Apr 2023 03:48:51 GMT
< Content-Type: application/json
< Content-Length: 0
<
* Connection #0 to host 10.16.12.8 left intact
In the returned information of data writing, Date: Mon, 10 Apr 2023 03:48:51 GMT indicates the time of successful writing completion without any error messages. For common error messages, see the table below.
Error Message
Error Meaning
Processing Method
{

    "error": "NotFoundCollection cannot find collections"

}
Connection failed.
Troubleshoot whether the private IP address and port connected with the database are correct.
{

    "error": "BadParam ERR wrong password.\\r\\n"

}
The account password for accessing the database is incorrect.
Check whether the password information is correct.
{

    "error": "NotFoundAccount ERR account id or name not exists: 1nB3pmg=="

}
The account for accessing the data does not exist.
Check whether there is an input error in the account information.
{

    "error": "InvalidFieldFormat ParsePoint() failed|status=InvalidFieldFormat InvalidBooleanFormat not start with tTfF|line=727|col=14\\ncar id=0,city=city_0,type=type_0 speed=100,temp=20 0\\n              | start here"

}
Data writing failed.
Check if there is a format issue with Line Protocol at the 14th character of this row of data: car id=0,city=city_0,type=type_0 speed=100,temp=20.
View the written measurements through show measurements. The execution method is as follows:
curl --get http://${influxdb_ip}:8086/query \\
--user "time_series_test_account":"test_password" \\
--data-urlencode 'db=time_series_test_db' \\
--data-urlencode "pretty=true" \\
--data-urlencode "q=show measurements"
The execution example is as follows:
[root@VM-16-41-centos ~]# curl --get http://10.16.12.8:8086/query \\
> --user "ctsdbi-8ym3****":"test@123" \\
> --data-urlencode 'db=time_series_test_db' \\
> --data-urlencode "pretty=true" \\
> --data-urlencode "q=show measurements"
{
"results": [
{
"statement_id": 0,
"series": [
{
"name": "measurements",
"columns": [
"name"
],
"columns_types": [
"string"
],
"values": [
[
"car"
],
[
"d=0"
]
]
}
]
}
]
}

Step 5: Querying Data

The syntax format of Select is as follows:
select_stmt = "SELECT" fields from_clause [ where_clause ] [ group_by_clause ] [ order_by_clause ] [ limit_clause ] [ offset_clause ] [ slimit_clause ] [ soffset_clause ] [ sql_order_by_clause ]
For the parameter meanings, see the table blow.
Parameter
Parameter Description
fields
Specifies the list of field(s) to be queried, which can consist of one or more fields, separated by commas. If you need to query all fields, you can use the wildcard "*".
from_clause
Specifies the name of the measurement to be queried.
where_clause
Specifies the condition(s) to be queried, which can consist of one or more conditions. Multiple conditions can be combined using logical operators such as AND, OR, and NOT. Conditions include tags and fields, which can be compared using comparison operators (such as =, !=, >, <).
group_by_clause
Specifies the tag(s) by which the query results need to be grouped, which can consist of one or more tags, separated by commas.
order_by_clause
Specifies the field(s) by which the query results need to be sorted, which can consist of one or more fields, separated by commas. By default, the query results are sorted by timestamp.
limit_clause
Specifies the maximum number of rows for the query results.
offset_clause
Specifies the starting row number for the query results.
slimit_clause
Similar to limit_clause, but only applies to the result set within each group.
soffset_clause
Similar to offset_clause, but only applies to the result set within each group.
sql_order_by_clause
Similar to order_by_clause, but sorts by using SQL syntax.
The execution method for querying all data is as follows:
curl --get http://${influxdb_ip}:8086/query \\
--user "time_series_test_account":"test_password" \\
--data-urlencode 'db=time_series_test_db' \\
--data-urlencode "pretty=true" \\
--data-urlencode 'q=select * from car'
The execution method for paging query is as follows:
curl --get http://${influxdb_ip}:8086/query \\
--user "time_series_test_account":"test_password" \\
--data-urlencode 'db=time_series_test_db' \\
--data-urlencode "pretty=true" \\
--data-urlencode 'q=select * from car limit 3 offset 2'
The execution method for querying aggregate function is as follows:
curl --get http://${influxdb_ip}:8086/query \\
--user "time_series_test_account":"test_password" \\
--data-urlencode 'db=time_series_test_db' \\
--data-urlencode "pretty=true" \\
--data-urlencode 'q=select max(speed) as fun1, min(speed) as fun2 from car group by time(3s), type fill(none)'
The returning result of executing aggregate function is as follows:
{
"results": [
{
"statement_id": 0,
"series": [
{
"name": "car",
"tags": {
"type": "type_0"
},
"columns": [
"time",
"fun1",
"fun2"
],
"columns_types": [
"time",
"float",
"float"
],
"values": [
[
"2023-02-01T07:30:54Z",
103.0,
100.0
],
[
"2023-02-01T07:30:57Z",
106.0,
100.0
],
[
"2023-02-01T07:31:00Z",
106.0,
100.0
]
]
},
{
"name": "car",
"tags": {
"type": "type_1"
},
"columns": [
"time",
"fun1",
"fun2"
],
"columns_types": [
"time",
"float",
"float"
],
"values": [
[
"2023-02-01T07:30:54Z",
104.0,
101.0
],
[
"2023-02-01T07:30:57Z",
107.0,
101.0
],
[
"2023-02-01T07:31:00Z",
107.0,
101.0
]
]
},
{
"name": "car",
"tags": {
"type": "type_2"
},
"columns": [
"time",
"fun1",
"fun2"
],
"columns_types": [
"time",
"float",
"float"
],
"values": [
[
"2023-02-01T07:30:54Z",
105.0,
102.0
],
[
"2023-02-01T07:30:57Z",
108.0,
102.0
],
[
"2023-02-01T07:31:00Z",
108.0,
102.0
]
]
}
]
}
]
}

Step 6: Deleting a Database

DELETE
The syntax format for deleting the specific data is as follows:
DELETE FROM <measurement_name> WHERE [<tag_key>='<tag_value>'] | [<time interval>]
measurement_name: Specify the name of the measurement for data to be deleted.
tag_key, tag_value: Optional parameters. Specify the corresponding tag key-value pairs of the data points to be deleted. If tag_key and tag_value are specified, only the data points that meet the conditions will be deleted. If tag_key and tag_value are not specified, all data points in measurement_name will be deleted.
time interval: Optional parameter. Specify the time range to be deleted. The following formats can be used for specification:
Timestamp: Delete the data point of the specified timestamp, for example: "time >= 1434059627 and time <= 1434060027"
Relative time: Delete data points within a certain time range relative to the current time, for example: "time >= now() - 1h"
Time period: Delete data points within the specified time period, for example: "time >= '2018-01-01T00:00:00Z' and time <= '2018-01-02T00:00:00Z'"
The execution method is as follows:
curl --get http://${influxdb_ip}:8086/query \\
--user "time_series_test_account":"test_password" \\
--data-urlencode 'db=time_series_test_db' \\
--data-urlencode "pretty=true" \\
--data-urlencode "q=delete from car where city = 'city_0'"
DROP MEASUREMENT
The syntax format for deleting the measurement is as follows:
DROP MEASUREMENT <measurement_name>
The execution method is as follows:
curl --get http://${influxdb_ip}:8086/query \\
--user "time_series_test_account":"test_password" \\
--data-urlencode 'db=time_series_test_db' \\
--data-urlencode "pretty=true" \\
--data-urlencode 'q=drop measurement car'
DROP DATABASE 
The syntax format for deleting the database is as follows:
DROP DATABASE <database_name>
The execution method is as follows:
curl --get http://${influxdb_ip}:8086/query \\
--user "time_series_test_account":"test_password" \\
--data-urlencode "pretty=true" \\
--data-urlencode 'q=DROP DATABASE time_series_test_db'
The following information is returned, indicating that it is deleted successfully.
{
"results": [
{
"statement_id": 0
}
]
}

More References

The use of CTSDB 3.0 is the same as that of native InfluxDB. For details on how to manage the database, see Manage your database using InfluxQL.
The CTSDB 3.0 uses the native SDK of InfluxDB. For examples of multi-language SDKs, see InfluxDB client libraries.
Was this page helpful?
You can also Contact Sales or Submit a Ticket for help.
Yes
No

Feedback