use of com.influxdb.client.InfluxDBClient in project oner365-cloud by xiaozhao32.
the class InfluxdbTest method main.
public static void main(final String[] args) {
// You can generate an API token from the "API Tokens Tab" in the UI
String url = "http://localhost:8086";
String token = "KQQQL_LpMrdpoopKu5_QvYPNVlL-KOoc1zY3dBTfu4xZGnicgAPhbSsryghUH1A21fXzHxPZ-W5hrNVG6tgIbQ==";
String bucket = "oner365";
String org = "oner365";
try (InfluxDBClient client = InfluxDBClientFactory.create(url, token.toCharArray())) {
// 保存
// writeData(client, bucket, org);
// 查询
queryData(client, bucket, org);
} catch (Exception e) {
LOGGER.error("influxdb error:", e);
}
}
use of com.influxdb.client.InfluxDBClient in project plc4x by apache.
the class HelloInflux method run.
public void run() {
InfluxDBClient dbConnection = connectToDb();
WriteApi writeApi = dbConnection.getWriteApi();
try {
PlcConnection plcConnection = connectToPlc();
final PlcSubscriptionRequest subscriptionRequest = plcConnection.subscriptionRequestBuilder().addChangeOfStateField("query", configuration.getString("plc.query")).build();
final PlcSubscriptionResponse subscriptionResponse = subscriptionRequest.execute().get(10, TimeUnit.SECONDS);
subscriptionResponse.getSubscriptionHandle("query").register(plcSubscriptionEvent -> {
DefaultPlcSubscriptionEvent internalEvent = (DefaultPlcSubscriptionEvent) plcSubscriptionEvent;
final Point point = Point.measurement(configuration.getString("influx.measurement")).time(plcSubscriptionEvent.getTimestamp().toEpochMilli(), WritePrecision.MS);
final Map<String, ResponseItem<PlcValue>> values = internalEvent.getValues();
values.forEach((fieldName, fieldResponsePair) -> {
final PlcResponseCode responseCode = fieldResponsePair.getCode();
final PlcValue plcValue = fieldResponsePair.getValue();
if (responseCode == PlcResponseCode.OK) {
PlcStruct structValue = (PlcStruct) plcValue;
for (String key : structValue.getKeys()) {
PlcValue subValue = structValue.getValue(key);
registerFields(point, key, subValue);
}
}
});
writeApi.writePoint(configuration.getString("influx.bucket"), configuration.getString("influx.org"), point);
});
} catch (PlcException e) {
logger.error("PLC Error", e);
} catch (Exception e) {
logger.error("General Error", e);
}
}
use of com.influxdb.client.InfluxDBClient in project incubator-pulsar by apache.
the class InfluxDBClientBuilderImpl method build.
@Override
public InfluxDBClient build(InfluxDBSinkConfig influxDBSinkConfig) {
val options = InfluxDBClientOptions.builder().url(influxDBSinkConfig.getInfluxdbUrl()).authenticateToken(influxDBSinkConfig.getToken().toCharArray()).org(influxDBSinkConfig.getOrganization()).bucket(influxDBSinkConfig.getBucket()).logLevel(LogLevel.valueOf(influxDBSinkConfig.getLogLevel().toUpperCase())).build();
InfluxDBClient influxDBClient = InfluxDBClientFactory.create(options);
if (!influxDBSinkConfig.isGzipEnable()) {
return influxDBClient;
}
influxDBClient.enableGzip();
return influxDBClient;
}
use of com.influxdb.client.InfluxDBClient in project openhab-addons by openhab.
the class InfluxDB2RepositoryImpl method connect.
/**
* Connect to InfluxDB server
*
* @return True if successful, otherwise false
*/
@Override
public boolean connect() {
InfluxDBClientOptions.Builder optionsBuilder = InfluxDBClientOptions.builder().url(configuration.getUrl()).org(configuration.getDatabaseName()).bucket(configuration.getRetentionPolicy());
char[] token = configuration.getTokenAsCharArray();
if (token.length > 0) {
optionsBuilder.authenticateToken(token);
} else {
optionsBuilder.authenticate(configuration.getUser(), configuration.getPassword().toCharArray());
}
InfluxDBClientOptions clientOptions = optionsBuilder.build();
final InfluxDBClient createdClient = InfluxDBClientFactory.create(clientOptions);
this.client = createdClient;
logger.debug("Succesfully connected to InfluxDB. Instance ready={}", createdClient.ready());
queryAPI = createdClient.getQueryApi();
writeAPI = createdClient.getWriteApi();
return checkConnectionStatus();
}
use of com.influxdb.client.InfluxDBClient in project openhab-addons by openhab.
the class InfluxDB2RepositoryImpl method disconnect.
/**
* Disconnect from InfluxDB server
*/
@Override
public void disconnect() {
final InfluxDBClient currentClient = this.client;
if (currentClient != null) {
currentClient.close();
}
this.client = null;
}
Aggregations