(11) JAVA operation InfluxDB

The following content comes from Shang Silicon Valley. I wrote this series of articles mainly to facilitate my own subsequent viewing. I don’t have to carry around a PDF to find it, which is too troublesome!

Chapter 11 JAVA Operation InfluxDB

1. For InfluxDB client, please refer to: https://github.com/influxdata/influxdb-client-java

11.1 Create a maven project

1. Here I created a maven project named java4influx

Insert image description here

11.2 Import maven dependencies

1. Add the following dependencies to pom.xml.

Insert image description here

<dependencies>
 <dependency>
 <groupId>com.influxdb</groupId>
 <artifactId>influxdb-client-java</artifactId>
 <version>6.5.0</version>
 </dependency>
</dependencies>

Refresh maven and download dependencies.

11.3 Create a package

1. Create a package under src/main/java. The name here is com.atguigu.influxdb.client. Finally, the project structure is shown in the figure

Insert image description here

11.4 Example: View InfluxDB health status

11.4.1 Create the ExampleHealthy class

1. As shown in the figure
Insert image description here

11.4.2 Creating client objects

1. Influxdb-client-java actually encapsulates various HTTP requests, so token, org and other HTTP API needs to be considered when creating the client.

Insert image description here
2. From the picture above, you can see that the InfluxDBClientFactory.create method actually has multiple overloads. This is because different interfaces require different permissions and operation scopes. For example, a token with read and write permissions can only operate on a certain bucket. Then the bucket should be specified when establishing a connection, that is, use the overload in the figure below.

Insert image description here
3. But if you are using an operator token and want to complete some operations of creating organizations and deleting users, you should not specify the bucket when creating the connection. In this case, the overload shown in the figure below should be used.

Insert image description here
4. However, checking the health status of InfluxDB does not require any permissions or tokens. At this point, we only need to specify a URL, and then we can use the overload shown in the figure below.

Insert image description here
5. I am demonstrating here on Ubuntu, and the target URL is http://localhost:8086. So the final code is as follows.

The InfluxDBClient object is our client object.

InfluxDBClient can return various API objects.

InfluxDBClient influxDBClient =  InfluxDBClientFactory.create("http://localhost:8086");

6. As shown in the figure below. This reflects Java's encapsulation of the InfluxDB HTTP API.

Insert image description here

11.4.3 Calling API

1. Some simple APIs can also be called directly through the InfluxDBClient object. For example, when we check the health status of InfluxDB, we can directly call the ping method of the InfluxDBClient object. As follows.

System.out.println(influxDBClient.ping());

Insert image description here

11.4.4 Operation

1. The ping method will return a Boolean value. If InfluxDB can ping successfully, it will get true, otherwise it will return false and record a failure log.

Insert image description here

11.4.5 Supplement

1. In the previous version, there was an API called health for testing the health of InfluxDB, but now this interface has been marked as obsolete. The health method returns a HealthCheck object. Relatively speaking, processing this object is much more troublesome than directly processing Boolean values. In future versions, it is recommended to use the ping method to check health status.

Insert image description here

11.5 Example: Querying data in InfluxDB

11.5.1 Create a JAVA class

1. Create a new java class, ExampleQuery, under com.atguigu.influxdb.client.

Insert image description here

11.5.2 Add a main method

1. Our query logic will be written in the main method later.
Insert image description here

11.5.3 Create InfluxDB client object

1. This time we are going to operate the data of a specific bucket in InfluxDB. When establishing a connection, it is recommended to choose the overload method in the figure.
Insert image description here
2. This method requires 4 parameters.

parameter illustrate
url The URL of the InfluxDB service is http://localhost:8086 in the teacher's case.
token Authorization token, and the type must be char[].
org Specify the organization to access.
bucket The bucket to access.

3. Now, we declare 4 static variables under ExampleQuery. As shown below:

Insert image description here

11.5.4 Obtain query API object

1. Click on the InfluxDBClient object and you can see that InfluxDBClient actually provides two APIs. This is also considered for compatibility. InfluxQLQueryAPI is forward compatible with InfluxDB2.x. Here we choose the first method, which is getQueryApi, which means we use the v2 api to query.

Insert image description here

11.5.5 Understanding the Query API

1. In summary, there are two methods query and queryRaw under the QueryApi object.

Insert image description here
2. Both methods need to pass in a FLUX script as a query statement. But the main difference lies in the results returned.

  • The queryRaw method returns the CSV format data (String type) in the API.
  • The query method view encapsulates the query results into various objects (you can specify it yourself or use the FluxTable provided by influxdb-client-java).

3. Each of the two methods has many different implementations, most of which are used to specify connection parameters. For example, if you do not specify org and bucket when creating the connection object, you can delay specifying it until the specific API is called.

11.5.6 query

1. Now, we first use the query method to query the data in InfluxDB. Now we want to query the last 2 minutes of data in the test_init bucket. code show as below:

List query = queryApi.query("from(bucket:\"test_init\") |> range(start:-2m)");

Insert image description here
2. Our query result List actually corresponds to the table flow concept in the FLUX query language. We can print the query variable.

Insert image description here

3. Now, we only take the first FluxTable and see what is in it.
Insert image description here

4. When we talked about FLUX before, we talked about the relationship between table flow and groupKey. Now you can print groupKey to see what's in it.
The code is as follows:

Insert image description here
5. The output results are as follows:

Insert image description here
6. This actually shows that our entire table flow is the result of groupKey grouping with _start, _stop, _field, _measurement 4 columns. Now, we can try to print the data. code show as below:

Insert image description here
7. The results are as follows:

Insert image description here

8. You can explore the remaining functions by yourself.

11.5.7 queryRaw

1. I created a new class here called ExampleQueryRaw. The code copied is all from ExampleQuery. The only difference is that queryApi.query is changed to queryApi.queryRaw. At the same time, the type of query variable has also changed from List to String.

Insert image description here

2. Now, we will print the query results.

Insert image description here
3. As you can see, we printed out data in CSV format. This is because the InfluxDB HTTP API originally placed data in CSV format in the request body. So the QueryRaw method actually returns the original CSV.

11.6 The difference between synchronous writing and asynchronous writing

1. Synchronous writing means that when I call the write method, I immediately initiate a request to InfluxDB to transfer the data, and the current thread will remain blocked waiting for the write operation to complete.

2. Asynchronous writing. In fact, when I call the write method, I do not perform the writing operation first, but put the data into a buffer. When the buffer is full, I actually send the data to InfluxDB, which is equivalent to achieving the effect of a batch. In the following examples, I would recommend first creating a bucket named example_java in InfluxDB before looking at the writing examples later.

11.7 Example: Synchronous writing to InfluxDB

11.7.1 Creating a class

1. This time we create a class named ExampleWriteSync. The org, bucket, url, token and so on are still copied from the previous example, but here we change the bucket to example_java. The overall code is as follows:

public class ExampleWriteSync {
    
    
	private static String org = "atguigu";
	private static String bucket = "example_java";
	private static String url = "http://localhost:8086";
	private static char[] token = "ZA8uWTSRFflhKhFvNW4TcZwwvd2NHFW1YIVlcj9Am5iJ4ueHawWh49_jszoKybEymHqgR5mAWg4XMv4tb9TP3w==".toCharArray();
	
	public static void main(String[] args) {
    
    
	InfluxDBClient influxDBClient = InfluxDBClientFactory.create(url, token, org, bucket);
	}
}

Insert image description here

11.7.2 Obtaining API objects

1. We can see that there are multiple methods on InfluxDBClient to obtain write operation API objects. Among them, WriteApiBlocking is synchronous writing and WriteApi is asynchronous writing.

Insert image description here
2. We now use the getWriteApiBlocking method to obtain the synchronous writing API.

11.7.3 What are the writing methods?

Insert image description here

1. To summarize simply, the writing API provides users with 3 types of methods to write data. In addition, these three types of methods have corresponding versions with an s suffix, indicating that multiple entries can be written at one time.

project Value
writeMeasuremen Users can write their own POJO classes
writePoint influxdb-client-java provides a Point class. Users can encapsulate pieces of data into Points and write them into InfluxDB.
writeRecord Users can write data to InfluxDB using strings that conform to the InfluxDB row protocol.

11.7.4 Writing to InfluxDB through Point object

11.7.4.1 Construct Point object

1. Use the following code to create a point object.

Point point = Point.measurement("temperature")
		.addTag("location", "west")
		.addField("value", 55D)
		.time(Instant.now(), WritePrecision.MS);

2. This is a typical constructor design pattern. Measurement is a static method that will help us create a new Point. addTag and addField are no longer explained. For the final time, we specify the precision of the written timestamp through the second parameter. Here, the writing time precision is determined as milliseconds. If you pass in a nanosecond timestamp, but the precision specifies milliseconds, the part exceeding milliseconds will be truncated directly.

11.7.4.2 Write the point

1. Use the following code to directly write points to InfluxDB. Remember to create the example_java bucket before doing this.

writeApiBlocking.writePoint(point);
11.7.4.3 Verify write results

1. After executing the program, check whether there is new data in example_java in InfluxDB DataExplorer and display it.

Insert image description here

11.7.5 Writing to InfluxDB via row protocol

11.7.5.1 Commenting the code of the previous example

1. Now, we comment out the previous code for writing data through Point.

Insert image description here

11.7.5.2 Writing code

1. Chase the following code in the main method

Here we omit the timestamp in the row protocol and let InfluxDB automatically fill in the time for us.

writeApiBlocking.writeRecord(WritePrecision.NS,"temperature,location=west value=60.0");
11.7.5.3 Verify write results

1. After running the code, still go to InfluxDB to view the data. As shown in the figure, the second piece of data has been successfully entered into InfluxDB.

Insert image description here

11.7.6 Writing to InfluxDB through POJO classes

11.7.6.1 Commenting the code of the previous example

1. Similarly, let’s comment out the code that last used the InfluxDB row protocol to write data.

Insert image description here

11.7.6.2 Add a static inner class
private static class Temperature {
    
    
}
11.7.6.3 @Measurement annotation

1. Add an annotation to the static inner class.

The @Measuremet annotation must be added to the class to indicate which measurement name in InfluxDB this class corresponds to.

@Measurement(name = "temperature")
private static class Temperature {
    
    
}
11.7.6.4 Add member variables
@Measurement(name = "temperature")
private static class Temperature {
    
    
String location;
Double value;
Instant time;
} 
11.7.6.5 @Column annotation

1. The @Column annotation can only be used on member variables.

2. There are 4 implementations of @Column, as shown in the figure below.

Insert image description here
3. You can specify a member variable as a tag, measurement, timestamp or field. The final code is as follows:

@Measurement(name = "temperature")
private static class Temperature {
    
    

@Column(tag = true)
String location;

@Column
Double value;

@Column(timestamp = true)
Instant time;
}
11.7.6.6 Create a Temperature object and assign values ​​to its properties

1. The code is as follows

Temperature temperature = new Temperature();
temperature.location = "west";
temperature.value = 40D;
temperature.time = Instant.now();
11.7.6.7 Writing to InfluxDB

Now, we write this POJO class object to InfluxDB

writeApiBlocking.writeMeasurement(WritePrecision.NS,temperature);

The final code is shown below:

Insert image description here

11.7.6.8 Verify writing effect

1. Run the main method and check the output on DataExplorer. As shown in the figure, the data has been successfully entered into InfluxDB.

Insert image description here

11.8 Example: Asynchronous writing to InfluxDB

11.8.1 Creating a class

1. Create a class named ExampleWriteAsync, and still reuse our previous org, bucket, url and token. And create an InfluxDBClient object. The basic code is as shown in the figure below.

Insert image description here

11.8.2 Obtaining API objects

1. You can see that the getWriteApi method has been marked as deprecated.
Insert image description here

2. It is now encouraged to use the makeWriteApi method. In fact, in the current version (2.4.0), the internal implementation of getWriteApi already calls makeWriteApi directly.

Insert image description here

11.8.3 Writing code

1. Like the previous synchronous writing, the writeApi object also has multiple writing methods such as writeRecord, writePoint, and wirteMeasurement, which will not be described here. Here, we only use the simplest writeRecord method to insert a piece of data and run the code through.

writeApi.writeRecord(WritePrecision.NS,"temperature,location=north value=60.0");

11.8.4 Verify write results (write failure)

1. At this point, the code we are running is as shown in the figure below.

Insert image description here
2. Open Data Explorer on the Web UI to view the writing results. As you can see, the data we just did not appear here, which means that our write failed.
Insert image description here

3. However, our java program did not report an error. This is because WriteApi will use a daemon thread to help us manage the buffer. It will write out the data when the buffer is full or 1 second has passed since the last time the data was written. We just put a piece of data, the buffer is not full, and the program exits immediately after calling the write method, so the background thread does not perform writing operations at all.

11.8.5 Modify code

1. Now, there are two ways for the daemon thread to perform write operations.

  • Manually trigger buffer flushing
 writeApi.flush();
  • Close InfluxDBClient
influxDBClient.close();

2. Here, we use the second one first. The modified code is as follows

Insert image description here

3. Run, and then go to Data Explorer to view the results.

11.8.6 Verify writing result (writing successful)

1. If you can see the north tag appearing on the location Tag and find out a piece of data, then the write operation is successful!

Insert image description here

11.8.7 Summary: Asynchronous writing work logic

  • There is a buffer in writeApi. The default size of this buffer is 10,000 pieces of data.
  • Although there is a buffer, writeApi does not write out the entire buffer at once, but writes it in units of batches (the default is 1000 items).
  • When pressure occurs or writing fails, the daemon thread will automatically retry writing data.

11.8.8 Configuration of asynchronous writing

1. The daemon thread of the asynchronous batch accumulation operation is performed implicitly. Fortunately, we can configure its behavior in detail.

Insert image description here
2. influxdb-client-java provides us with a WriteOption object, which can be passed in when calling makeWriteApi. From the prompts in the above figure, we can see that the size of the buffer, the size of the batch, and the refresh interval are all Can be explicitly specified.

11.8.9 Default configuration

Insert image description here

11.9 Compatible with V1 API

1. Here is only a brief introduction.

2. When using InfluxDBClientFactory to create a Client object, call the createV1 method. What you get at this time is a Client object that is compatible with V1 API.

Insert image description here

Guess you like

Origin blog.csdn.net/qq_38263083/article/details/131930716