Reputation: 51
Trying to connect InfluxDB through Java. Please check below the following code :
public InfluxConnect() {
this.influxDB = InfluxDBFactory.connect("http://localhost:8086", "avinflux", "Stallions@7891");
this.influxDB.setLogLevel(InfluxDB.LogLevel.BASIC);
Pong response = this.influxDB.ping();
if (response.getVersion().equalsIgnoreCase("unknown")) {
System.out.println("unknown");
}
else System.out.println("response is : "+response.toString());
try {
@SuppressWarnings("deprecation")
String databaseName = "NOAA_water_database";
this.influxDB.query(new Query("CREATE DATABASE " + databaseName));
this.influxDB.setDatabase(databaseName);
}
catch(Exception e)
{
System.out.println("In Catch");
e.printStackTrace();
}
}
**Error which i am getting **
Jul 27, 2021 3:40:57 PM okhttp3.internal.platform.Platform log
INFO: <-- 401 Unauthorized http://192.168.105.191:8086/query?q=CREATE+DATABASE+NOAA_water_database (0ms, 48-byte body)
In Catch
org.influxdb.InfluxDBException: {"code":"unauthorized","message":"Unauthorized"}
at org.influxdb.InfluxDBException.buildExceptionForErrorState(InfluxDBException.java:175)
Same credential when i am using after hitting http://localhost:8086/ on web browser and it is working fine but when i am trying to connect through java code it is not working and throwing above exception
Not able to find where exactly is the problem.
Can anybody please suggest/find out the problem?
Upvotes: 1
Views: 3705
Reputation: 41
I also faced the same issue when I was trying to write the data in influxdb. But I have fixed this issue by update the library. I believe you are using influxdb 2.x version not 1.x. In this case, please use the following dependency.
instead of
<dependency>
<groupId>org.influxdb</groupId>
<artifactId>influxdb-java</artifactId>
<version>${influxdb.version}</version>
</dependency>
use this
<dependency>
<groupId>com.influxdb</groupId>
<artifactId>influxdb-client-java</artifactId>
<version>4.0.0</version>
</dependency>
with this library, you can perform the write and query operations like below. If you observe the below code, you can clearly see you need to supply url, token, org and bucket instead of username and password.
package com.influx.writer;
import java.time.Instant;
import java.util.List;
import com.influxdb.annotations.Column;
import com.influxdb.annotations.Measurement;
import com.influxdb.client.InfluxDBClient;
import com.influxdb.client.InfluxDBClientFactory;
import com.influxdb.client.QueryApi;
import com.influxdb.client.WriteApiBlocking;
import com.influxdb.client.domain.WritePrecision;
import com.influxdb.client.write.Point;
import com.influxdb.query.FluxRecord;
import com.influxdb.query.FluxTable;
public class InfluxDB2Example {
private static char[] token = "my-token".toCharArray();
private static String org = "my-org";
private static String bucket = "my-bucket";
public static void main(final String[] args) {
InfluxDBClient influxDBClient = InfluxDBClientFactory.create("http://localhost:8086", token, org, bucket);
//
// Write data
//
WriteApiBlocking writeApi = influxDBClient.getWriteApiBlocking();
//
// Write by Data Point
//
Point point = Point.measurement("temperature")
.addTag("location", "west")
.addField("value", 55D)
.time(Instant.now().toEpochMilli(), WritePrecision.MS);
writeApi.writePoint(point);
//
// Write by LineProtocol
//
writeApi.writeRecord(WritePrecision.NS, "temperature,location=north value=60.0");
//
// Write by POJO
//
Temperature temperature = new Temperature();
temperature.location = "south";
temperature.value = 62D;
temperature.time = Instant.now();
writeApi.writeMeasurement( WritePrecision.NS, temperature);
//
// Query data
//
String flux = "from(bucket:\"my-bucket\") |> range(start: 0)";
QueryApi queryApi = influxDBClient.getQueryApi();
List<FluxTable> tables = queryApi.query(flux);
for (FluxTable fluxTable : tables) {
List<FluxRecord> records = fluxTable.getRecords();
for (FluxRecord fluxRecord : records) {
System.out.println(fluxRecord.getTime() + ": " + fluxRecord.getValueByKey("_value"));
}
}
influxDBClient.close();
}
@Measurement(name = "temperature")
private static class Temperature {
@Column(tag = true)
String location;
@Column
Double value;
@Column(timestamp = true)
Instant time;
}
}
Upvotes: 4