Search in sources :

Example 11 with InfluxDBClient

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);
    }
}
Also used : InfluxDBClient(com.influxdb.client.InfluxDBClient)

Example 12 with InfluxDBClient

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);
    }
}
Also used : InfluxDBClient(com.influxdb.client.InfluxDBClient) PlcSubscriptionRequest(org.apache.plc4x.java.api.messages.PlcSubscriptionRequest) DefaultPlcSubscriptionEvent(org.apache.plc4x.java.spi.messages.DefaultPlcSubscriptionEvent) Point(com.influxdb.client.write.Point) PlcConnection(org.apache.plc4x.java.api.PlcConnection) ConfigurationException(org.apache.commons.configuration2.ex.ConfigurationException) PlcException(org.apache.plc4x.java.api.exceptions.PlcException) PlcRuntimeException(org.apache.plc4x.java.api.exceptions.PlcRuntimeException) PlcResponseCode(org.apache.plc4x.java.api.types.PlcResponseCode) PlcException(org.apache.plc4x.java.api.exceptions.PlcException) WriteApi(com.influxdb.client.WriteApi) PlcSubscriptionResponse(org.apache.plc4x.java.api.messages.PlcSubscriptionResponse) ResponseItem(org.apache.plc4x.java.spi.messages.utils.ResponseItem)

Example 13 with InfluxDBClient

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;
}
Also used : lombok.val(lombok.val) InfluxDBClient(com.influxdb.client.InfluxDBClient)

Example 14 with 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();
}
Also used : InfluxDBClient(com.influxdb.client.InfluxDBClient) InfluxDBClientOptions(com.influxdb.client.InfluxDBClientOptions)

Example 15 with InfluxDBClient

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;
}
Also used : InfluxDBClient(com.influxdb.client.InfluxDBClient)

Aggregations

InfluxDBClient (com.influxdb.client.InfluxDBClient)24 InfluxDBClientOptions (com.influxdb.client.InfluxDBClientOptions)4 QueryApi (com.influxdb.client.QueryApi)3 Ready (com.influxdb.client.domain.Ready)3 IOException (java.io.IOException)3 lombok.val (lombok.val)3 Cancellable (com.influxdb.Cancellable)2 Query (com.influxdb.client.domain.Query)2 Point (com.influxdb.client.write.Point)2 GeneralSecurityException (java.security.GeneralSecurityException)2 InitializationException (org.apache.nifi.reporting.InitializationException)2 MockProcessContext (org.apache.nifi.util.MockProcessContext)2 MockProcessorInitializationContext (org.apache.nifi.util.MockProcessorInitializationContext)2 InfluxDatabaseService_2 (org.influxdata.nifi.services.InfluxDatabaseService_2)2 StandardInfluxDatabaseService_2 (org.influxdata.nifi.services.StandardInfluxDatabaseService_2)2 NonExecutablePhysicalTaskException (cn.edu.tsinghua.iginx.engine.physical.exception.NonExecutablePhysicalTaskException)1 PhysicalException (cn.edu.tsinghua.iginx.engine.physical.exception.PhysicalException)1 PhysicalTaskExecuteFailureException (cn.edu.tsinghua.iginx.engine.physical.exception.PhysicalTaskExecuteFailureException)1 StorageInitializationException (cn.edu.tsinghua.iginx.engine.physical.exception.StorageInitializationException)1 StandardUsernamePasswordCredentials (com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials)1