Search in sources :

Example 21 with InfluxDBClient

use of com.influxdb.client.InfluxDBClient in project nifi-influxdb-bundle by influxdata.

the class StandardInfluxDatabaseService_2 method create.

@NonNull
@Override
public InfluxDBClient create() {
    ConfigurationContext context = getConfigurationContext();
    // SSL
    SSLContextService sslService = context.getProperty(SSL_CONTEXT_SERVICE).asControllerService(SSLContextService.class);
    ClientAuth clientAuth = getEnumValue(CLIENT_AUTH, context, ClientAuth.class, DEFAULT_CLIENT_AUTH);
    // Connection
    String influxDbUrl = getDatabaseURL();
    long connectionTimeout = context.getProperty(INFLUX_DB_CONNECTION_TIMEOUT).asTimePeriod(TimeUnit.SECONDS);
    // Credentials
    String token = context.getProperty(INFLUX_DB_ACCESS_TOKEN).evaluateAttributeExpressions().getValue();
    try {
        InfluxDBClient client = connect(token, sslService, clientAuth, influxDbUrl, connectionTimeout);
        getLogger().info("InfluxDB 2.0 connection created for host {}", new Object[] { influxDbUrl });
        return client;
    } catch (Exception e) {
        getLogger().error("Error while getting connection {}", new Object[] { e.getLocalizedMessage() }, e);
        throw new RuntimeException("Error while getting connection " + e.getLocalizedMessage(), e);
    }
}
Also used : ConfigurationContext(org.apache.nifi.controller.ConfigurationContext) InfluxDBClient(com.influxdb.client.InfluxDBClient) SSLContextService(org.apache.nifi.ssl.SSLContextService) ClientAuth(org.apache.nifi.security.util.ClientAuth) IOException(java.io.IOException) NonNull(edu.umd.cs.findbugs.annotations.NonNull)

Example 22 with InfluxDBClient

use of com.influxdb.client.InfluxDBClient in project nifi-influxdb-bundle by influxdata.

the class AbstractTestStandardInfluxDatabaseService_2 method assertConnectToDatabase.

protected void assertConnectToDatabase() {
    InfluxDBClient client = service.create();
    HealthCheck result = client.health();
    Assert.assertEquals("Unexpected health check: " + result, HealthCheck.StatusEnum.PASS, result.getStatus());
}
Also used : InfluxDBClient(com.influxdb.client.InfluxDBClient) HealthCheck(com.influxdb.client.domain.HealthCheck)

Example 23 with InfluxDBClient

use of com.influxdb.client.InfluxDBClient in project nifi-influxdb-bundle by influxdata.

the class TestStandardInfluxDatabaseService_2 method successConnect.

@Test
public void successConnect() throws IOException, GeneralSecurityException {
    answerConnect = new CallsRealMethods();
    testRunner.enableControllerService(service);
    InfluxDBClient influxDB = service.create();
    Assert.assertNotNull(influxDB);
}
Also used : InfluxDBClient(com.influxdb.client.InfluxDBClient) CallsRealMethods(org.mockito.internal.stubbing.answers.CallsRealMethods) Test(org.junit.Test)

Example 24 with InfluxDBClient

use of com.influxdb.client.InfluxDBClient in project nifi-influxdb-bundle by influxdata.

the class AbstractTestGetInfluxDatabaseRecord_2 method before.

@Before
public void before() throws IOException, GeneralSecurityException, InitializationException {
    InfluxDBClient mockInfluxDBClient = Mockito.mock(InfluxDBClient.class);
    QueryApi mockQueryApi = Mockito.mock(QueryApi.class);
    Mockito.doAnswer(invocation -> mockQueryApi).when(mockInfluxDBClient).getQueryApi();
    Mockito.doAnswer(invocation -> {
        if (queryOnErrorValue != null) {
            // noinspection unchecked
            Consumer<Exception> onError = invocation.getArgument(3, Consumer.class);
            onError.accept(queryOnErrorValue);
        }
        queryOnResponseRecords.forEach(record -> {
            // noinspection unchecked
            BiConsumer<Cancellable, FluxRecord> onRecord = invocation.getArgument(2, BiConsumer.class);
            onRecord.accept(Mockito.mock(Cancellable.class), record);
        });
        boolean wasException = queryOnErrorValue != null;
        try {
            return queryAnswer.answer(invocation);
        } catch (Exception e) {
            wasException = true;
            throw e;
        } finally {
            if (!wasException) {
                Runnable onComplete = invocation.getArgument(4, Runnable.class);
                onComplete.run();
            }
        }
    }).when(mockQueryApi).query(Mockito.any(Query.class), Mockito.anyString(), Mockito.any(), Mockito.any(), Mockito.any(Runnable.class));
    processor = Mockito.spy(new GetInfluxDatabaseRecord_2());
    runner = TestRunners.newTestRunner(processor);
    runner.setProperty(GetInfluxDatabaseRecord_2.ORG, "my-org");
    runner.setProperty(GetInfluxDatabaseRecord_2.QUERY, "from(bucket:\"my-bucket\") |> range(start: 0) |> last()");
    runner.setProperty(GetInfluxDatabaseRecord_2.INFLUX_DB_SERVICE, "influxdb-service");
    runner.setProperty(GetInfluxDatabaseRecord_2.WRITER_FACTORY, "writer");
    InfluxDatabaseService_2 influxDatabaseService = Mockito.spy(new StandardInfluxDatabaseService_2());
    Mockito.doAnswer(invocation -> mockInfluxDBClient).when(influxDatabaseService).create();
    runner.addControllerService("influxdb-service", influxDatabaseService);
    runner.setProperty(influxDatabaseService, INFLUX_DB_ACCESS_TOKEN, "my-token");
    runner.enableControllerService(influxDatabaseService);
    writer = new ArrayListRecordWriter(null) {

        @Override
        public RecordSchema getSchema(final Map<String, String> variables, final RecordSchema readSchema) {
            return readSchema;
        }
    };
    runner.addControllerService("writer", writer);
    runner.enableControllerService(writer);
    MockProcessContext context = new MockProcessContext(processor);
    MockProcessorInitializationContext initContext = new MockProcessorInitializationContext(processor, context);
    logger = initContext.getLogger();
    processor.initialize(initContext);
    processor.onScheduled(runner.getProcessContext());
    processor.initWriterFactory(runner.getProcessContext());
}
Also used : ArrayListRecordWriter(org.apache.nifi.serialization.record.ArrayListRecordWriter) InfluxDBClient(com.influxdb.client.InfluxDBClient) QueryApi(com.influxdb.client.QueryApi) Query(com.influxdb.client.domain.Query) InfluxDatabaseService_2(org.influxdata.nifi.services.InfluxDatabaseService_2) StandardInfluxDatabaseService_2(org.influxdata.nifi.services.StandardInfluxDatabaseService_2) Cancellable(com.influxdb.Cancellable) MockProcessorInitializationContext(org.apache.nifi.util.MockProcessorInitializationContext) StandardInfluxDatabaseService_2(org.influxdata.nifi.services.StandardInfluxDatabaseService_2) InitializationException(org.apache.nifi.reporting.InitializationException) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException) FluxRecord(com.influxdb.query.FluxRecord) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) MockProcessContext(org.apache.nifi.util.MockProcessContext) Before(org.junit.Before)

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