To the reader

InfluxDb Chinese tutorial

After installing InfluxDB, we started to do some interesting things. Within this chapter we will use the Influx command line tool, which is included with the InfluxDB installation package and is a lightweight command line tool for manipulating databases. It communicates directly with InfluxDB via its HTTP interface (default 8086 if not modified).

Note: You can also send a naked HTTP request to a database, such as curl. Rest is a slightly more complex interface.

1. InfluxDB Creates a database

If you’ve already installed InfluxDB locally, you can use the Influx command line directly to connect to a local InfluxDB instance. The output would look like this:

Description:

  • The HTTP interface of InfluxDB starts by default8086, soinfluxThe default is also local8086Port that you can pass throughinflux --helpSee how to change the default values.
  • -precisionParameters specify the format and precision of any returned timestamp. In the example above,rfc3339Yes Send the InfluxDB backRFC339Timestamp in the format YYYY-MM-DDTHH:MM: ss.nnNNNNNnz.

So the command line is ready to receive a FURTHER influx query, and exit can be used to exit the command line.

After installing the InfluxDB DATABASE for the first time, there is no DATABASE (except _internal). Therefore, creating a DATABASE is the first thing we need to do, by using the InfluxDB

statement.

is the name of the database. The database name can be any Unicode character enclosed by double quotes. If the name contains only ASCII letters, numbers, or underscores, and does not start with a number, it may not be raised.

Create a myDB database

Create a myDB database:

CREATE DATABASE mydb
Copy the code

In the CLI, the statement was executed without error. If there is an error message, there must be something wrong. No message is good news. This is not something many people are used to, because in mysql there is feedback, and influxDB does not have feedback, making it seem that one does not know what is happening.

Now that the database myDB is created, we can use the SHOW DATABASES statement to view the existing database. We can also use the lower case SHOW DATABASES:

>> SHOW DATABASES
name: databases
---------------
name
_internal
mydb
Copy the code

Note: The _internal database is used to store real-time monitoring data within the InfluxDB.

Unlike SHOW DATABASES, most InfluxQL needs to be applied to a specific database. You can of course put the name of the database you want on each query, but the CLI provides a more convenient way to USE

, which will be set to that database for all your subsequent requests. Such as:

| > USE mydb |
| Using database mydb |
| > |
Copy the code

The following operations apply to myDB, as well as to mysql.


3. Write data

Now that we have a database, InfluxDB can start receiving reads and writes.

First of all, the format of data storage is introduced. The data stored in the InfluxDB is called time series data and contains a number, usually time, which can be represented by a number.

Time series data has zero or more data points, each of which is an index value. Data points include time(a timestamp), measurement, at least one K-V field(that is, key-value pairs “value=0.64” or “temperature=21.2”), zero or more tag tags, It is generally metadata for the index value (for example, “host=server01”, “region=EMEA”, “dc=Frankfurt”).

Conceptually, you can think of measurement as a table in SQL, where the primary key index is always timestamp. Tag and field are other columns in the table, tag is indexed, field is not. The difference is that you can have millions of measurements in InfluxDB, you do not have to define the data scheme in advance, and null values are not stored.

To write data points to the InfluxDB, just follow the following insert protocol:

| insert <measurement>[,<tag-key>=<tag-value>...]  <field-key>=<field-value>[,<field2-key>=<field2-value>...]  [unix-nano-timestamp] |Copy the code

In the expansion of the content, is optional, can have, can not have.

Insert some data into a table. Insert statements must have the following characteristics:

  • 1 table name measurement
  • Zero or more tag labels
  • One or more fields
  • An optional timestamp unix-nano-timestamp

Here is an example of the format in which data is written to InfluxDB:

| insert CPU, host = serverA, region = us_west value = 0.64 | | payment, device = mobile, product = Notepad, method = credit billed=33,licenses=3i 1434067467100293230 |Copy the code

CPU stands for table, the other equals are key-value pairs, and the last one is a timestamp.

Here’s an example:

Use the command line to INSERT single time series data into the InfluxDB, followed by INSERT data points:

INSERT the CPU, the host = serverA, region = us_west value = 0.64Copy the code

A data point whose measurement is CPU, tag is host and region, and value is 0.64 is written to the InfluxDB. Note that there is no write time, indicating that the default is the system time.

Note that the space above is the cut-off point between tag and field


4. Query data

Now we find the data that was written:

| use mydb |
| SELECT "host", "region", "value" FROM "cpu" |
Copy the code

The result is as follows:

Note: We do not include a timestamp when writing, and when there is no timestamp, InfluxDB automatically adds the local current time as its timestamp.

Let’s write another block of data that contains two fields:

| INSERT temperature,machine=unit42,type=assembly external=25,internal=37 |
Copy the code
  • Temperature is the table name measurement
  • Machine and type are tags
  • External and internal are fields
  • Tags and fields are separated by Spaces

If you want to return all fields and tags in a query, you can use *.

| > SELECT * FROM "temperature" | | name: Temperature | | -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - | | time external internal machine type | | 2015-10-21 T19:28:08. 25 37 385013942 z unit42 assembly |Copy the code

There are also many features and uses of InfluxQL that are not mentioned, including golang-style re support, for example:

| SELECT * FROM /.*/ LIMIT 1 |
Copy the code

This means to check the data in all tables as follows:


5, summary

This chapter is just the beginning, and there are more read and write operations on Influxdb. Stay tuned for the following articles.