All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.influxdb.v3.client.InfluxDBClient Maven / Gradle / Ivy

Go to download

The Java client that provides an easy and convenient way to interact with InfluxDB 3. This package supports both writing data to InfluxDB and querying data using the FlightSQL client, which allows you to execute SQL queries against InfluxDB IOx.

The newest version!
/*
 * The MIT License
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.influxdb.v3.client;

import java.net.MalformedURLException;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import org.apache.arrow.vector.VectorSchemaRoot;

import com.influxdb.v3.client.config.ClientConfig;
import com.influxdb.v3.client.internal.InfluxDBClientImpl;
import com.influxdb.v3.client.query.QueryOptions;
import com.influxdb.v3.client.write.WriteOptions;

/**
 * The InfluxDBClient interface provides a client for interact with InfluxDB 3.
 * This client supports both writing data to InfluxDB and querying data using the FlightSQL client,
 * which allows you to execute SQL queries against InfluxDB IOx.
 */
public interface InfluxDBClient extends AutoCloseable {

    /**
     * Write a record specified in the InfluxDB Line Protocol to the InfluxDB server.
     *
     * @param record the record specified in the InfluxDB Line Protocol, can be null
     */
    void writeRecord(@Nullable final String record);

    /**
     * Write a record specified in the InfluxDB Line Protocol to the InfluxDB server.
     *
     * @param record  the record specified in the InfluxDB Line Protocol, can be null
     * @param options the options for writing data to InfluxDB
     */
    void writeRecord(@Nullable final String record, @Nonnull final WriteOptions options);

    /**
     * Write records specified in the InfluxDB Line Protocol to the InfluxDB server.
     *
     * @param records the records specified in the InfluxDB Line Protocol, cannot be null
     */
    void writeRecords(@Nonnull final List records);

    /**
     * Write records specified in the InfluxDB Line Protocol to the InfluxDB server.
     *
     * @param records the records specified in the InfluxDB Line Protocol, cannot be null
     * @param options the options for writing data to InfluxDB
     */
    void writeRecords(@Nonnull final List records, @Nonnull final WriteOptions options);

    /**
     * Write a {@link Point} to the InfluxDB server.
     *
     * @param point the {@link Point} to write, can be null
     */
    void writePoint(@Nullable final Point point);

    /**
     * Write a {@link Point} to the InfluxDB server.
     *
     * @param point   the {@link Point} to write, can be null
     * @param options the options for writing data to InfluxDB
     */
    void writePoint(@Nullable final Point point, @Nonnull final WriteOptions options);

    /**
     * Write a list of {@link Point} to the InfluxDB server.
     *
     * @param points the list of {@link Point} to write, cannot be null
     */
    void writePoints(@Nonnull final List points);

    /**
     * Write a list of {@link Point} to the InfluxDB server.
     *
     * @param points  the list of {@link Point} to write, cannot be null
     * @param options the options for writing data to InfluxDB
     */
    void writePoints(@Nonnull final List points, @Nonnull final WriteOptions options);

    /**
     * Query data from InfluxDB IOx using FlightSQL.
     * 

* The result stream should be closed after use, you can use try-resource pattern to close it automatically: *

     * try (Stream<Object[]> rows = client.query("select * from cpu")) {
     *      rows.forEach(row -> {
     *          // process row
     *      }
     * });
     * 
* * @param query the SQL query string to execute, cannot be null * @return Batches of rows returned by the query */ @Nonnull Stream query(@Nonnull final String query); /** * Query data from InfluxDB IOx using FlightSQL. *

* The result stream should be closed after use, you can use try-resource pattern to close it automatically: *

     * try (Stream<Object[]> rows = client.query("select * from cpu where host=$host",
     *                                                 Map.of("host", "server-a")) {
     *      rows.forEach(row -> {
     *          // process row
     *      }
     * });
     * 
* * @param query the SQL query string to execute, cannot be null * @param parameters query named parameters * @return Batches of rows returned by the query */ @Nonnull Stream query(@Nonnull final String query, @Nonnull final Map parameters); /** * Query data from InfluxDB IOx using FlightSQL. *

* The result stream should be closed after use, you can use try-resource pattern to close it automatically: *

     * try (Stream<Object[]> rows = client.query("select * from cpu", options)) {
     *      rows.forEach(row -> {
     *          // process row
     *      }
     * });
     * 
* * @param query the query string to execute, cannot be null * @param options the options for querying data from InfluxDB * @return Batches of rows returned by the query */ @Nonnull Stream query(@Nonnull final String query, @Nonnull final QueryOptions options); /** * Query data from InfluxDB IOx using FlightSQL. *

* The result stream should be closed after use, you can use try-resource pattern to close it automatically: *

     * try (Stream<Object[]> rows = client.query("select * from cpu where host=$host",
     *                                                 Map.of("host", "server-a"), options)) {
     *      rows.forEach(row -> {
     *          // process row
     *      }
     * });
     * 
* * @param query the query string to execute, cannot be null * @param parameters query named parameters * @param options the options for querying data from InfluxDB * @return Batches of rows returned by the query */ @Nonnull Stream query(@Nonnull final String query, @Nonnull final Map parameters, @Nonnull final QueryOptions options); /** * Query data from InfluxDB IOx into Point structure using FlightSQL. *

* The result stream should be closed after use, you can use try-resource pattern to close it automatically: *

     * try (Stream<PointValues> rows = client.queryPoints("select * from cpu", options)) {
     *      rows.forEach(row -> {
     *          // process row
     *      }
     * });
     * 
* * @param query the SQL query string to execute, cannot be null * @return Batches of PointValues returned by the query */ @Nonnull Stream queryPoints(@Nonnull final String query); /** * Query data from InfluxDB IOx into Point structure using FlightSQL. *

* The result stream should be closed after use, you can use try-resource pattern to close it automatically: *

     * try (Stream<PointValues> rows = client.queryPoints("select * from cpu where host=$host",
     *                                                          Map.of("host", "server-a"))) {
     *      rows.forEach(row -> {
     *          // process row
     *      }
     * });
     * 
* * @param query the SQL query string to execute, cannot be null * @param parameters query named parameters * @return Batches of PointValues returned by the query */ @Nonnull Stream queryPoints(@Nonnull final String query, @Nonnull final Map parameters); /** * Query data from InfluxDB IOx into Point structure using FlightSQL. *

* The result stream should be closed after use, you can use try-resource pattern to close it automatically: *

     * try (Stream<PointValues> rows = client.queryPoints("select * from cpu", options)) {
     *      rows.forEach(row -> {
     *          // process row
     *      }
     * });
     * 
* * @param query the query string to execute, cannot be null * @param options the options for querying data from InfluxDB * @return Batches of PointValues returned by the query */ @Nonnull Stream queryPoints(@Nonnull final String query, @Nonnull final QueryOptions options); /** * Query data from InfluxDB IOx into Point structure using FlightSQL. *

* The result stream should be closed after use, you can use try-resource pattern to close it automatically: *

     * try (Stream<PointValues> rows = client.queryPoints("select * from cpu where host=$host",
     *                                                          Map.of("host", "server-a"),
     *                                                          options)) {
     *      rows.forEach(row -> {
     *          // process row
     *      }
     * });
     * 
* * @param query the query string to execute, cannot be null * @param parameters query named parameters * @param options the options for querying data from InfluxDB * @return Batches of PointValues returned by the query */ @Nonnull Stream queryPoints(@Nonnull final String query, @Nonnull final Map parameters, @Nonnull final QueryOptions options); /** * Query data from InfluxDB IOx using FlightSQL. *

* The result stream should be closed after use, you can use try-resource pattern to close it automatically: *

     * try (Stream<VectorSchemaRoot> batches = client.queryBatches("select * from cpu")) {
     *      batches.forEach(batch -> {
     *          // process batch
     *      }
     * });
     * 
* * @param query the SQL query string to execute, cannot be null * @return Batches of rows returned by the query */ @Nonnull Stream queryBatches(@Nonnull final String query); /** * Query data from InfluxDB IOx using FlightSQL. *

* The result stream should be closed after use, you can use try-resource pattern to close it automatically: *

     * try (Stream<VectorSchemaRoot> batches = client.queryBatches("select * from cpu where host=$host",
     *                                                                   Map.of("host", "server-a"))) {
     *      batches.forEach(batch -> {
     *          // process batch
     *      }
     * });
     * 
* * @param query the SQL query string to execute, cannot be null * @param parameters query named parameters * @return Batches of rows returned by the query */ @Nonnull Stream queryBatches(@Nonnull final String query, @Nonnull final Map parameters); /** * Query data from InfluxDB IOx using FlightSQL. *
     * try (Stream<VectorSchemaRoot> batches = client.queryBatches("select * from cpu", options)) {
     *      batches.forEach(batch -> {
     *          // process batch
     *      }
     * });
     * 
* * @param query the query string to execute, cannot be null * @param options the options for querying data from InfluxDB * @return Batches of rows returned by the query */ @Nonnull Stream queryBatches(@Nonnull final String query, @Nonnull final QueryOptions options); /** * Query data from InfluxDB IOx using FlightSQL. *
     * try (Stream<VectorSchemaRoot> batches = client.queryBatches("select * from cpu where host=$host",
     *                                                                   Map.of("host", "server-a"),
     *                                                                   options)) {
     *      batches.forEach(batch -> {
     *          // process batch
     *      }
     * });
     * 
* * @param query the query string to execute, cannot be null * @param parameters query named parameters * @param options the options for querying data from InfluxDB * @return Batches of rows returned by the query */ @Nonnull Stream queryBatches(@Nonnull final String query, @Nonnull final Map parameters, @Nonnull final QueryOptions options); /** * Creates a new instance of the {@link InfluxDBClient} for interacting with an InfluxDB server, simplifying * common operations such as writing, querying. * * @param host the URL of the InfluxDB server * @param token the authentication token for accessing the InfluxDB server, can be null * @param database the database to be used for InfluxDB operations, can be null * @return new instance of the {@link InfluxDBClient} */ @Nonnull static InfluxDBClient getInstance(@Nonnull final String host, @Nullable final char[] token, @Nullable final String database) { ClientConfig config = new ClientConfig.Builder() .host(host) .token(token) .database(database) .build(); return getInstance(config); } /** * Creates a new instance of the {@link InfluxDBClient} for interacting with an InfluxDB server, simplifying * common operations such as writing, querying. * * @param host the URL of the InfluxDB server * @param token the authentication token for accessing the InfluxDB server, can be null * @param database the database to be used for InfluxDB operations, can be null * @param defaultTags tags to be added by default to writes of points * @return new instance of the {@link InfluxDBClient} */ @Nonnull static InfluxDBClient getInstance(@Nonnull final String host, @Nullable final char[] token, @Nullable final String database, @Nullable Map defaultTags) { ClientConfig config = new ClientConfig.Builder() .host(host) .token(token) .database(database) .defaultTags(defaultTags) .build(); return getInstance(config); } /** * Creates a new instance of the {@link InfluxDBClient} for interacting with an InfluxDB server, simplifying * common operations such as writing, querying. * For possible configuration options see {@link ClientConfig}. * * @param config the configuration for the InfluxDB client * @return new instance of the {@link InfluxDBClient} */ @Nonnull static InfluxDBClient getInstance(@Nonnull final ClientConfig config) { return new InfluxDBClientImpl(config); } /** * Creates a new instance of the {@link InfluxDBClient} from the connection string in URL format. *

* Example: *

     * client = InfluxDBClient.getInstance("https://us-east-1-1.aws.cloud2.influxdata.com/"
     *         + "?token=my-token&database=my-database");
     * 
*

* Supported parameters: *

    *
  • token - authentication token (required)
  • *
  • org - organization name
  • *
  • database - database (bucket) name
  • *
  • precision - timestamp precision when writing data
  • *
  • gzipThreshold - payload size size for gzipping data
  • *
* * @param connectionString connection string * @return instance of {@link InfluxDBClient} */ @Nonnull static InfluxDBClient getInstance(@Nonnull final String connectionString) { try { return getInstance(new ClientConfig.Builder().build(connectionString)); } catch (MalformedURLException e) { throw new IllegalArgumentException(e); // same exception as ClientConfig.validate() } } /** * Creates a new instance of the {@link InfluxDBClient} from environment variables and/or system properties. * Environment variables take precedence over system properties. *

* Example: *

     * client = InfluxDBClient.getInstance();
     * 
*

* Supported environment variables: *

    *
  • INFLUX_HOST - cloud/server URL required
  • *
  • INFLUX_TOKEN - authentication token required
  • *
  • INFLUX_AUTH_SCHEME - authentication scheme
  • *
  • INFLUX_ORG - organization name
  • *
  • INFLUX_DATABASE - database (bucket) name
  • *
  • INFLUX_PRECISION - timestamp precision when writing data
  • *
  • INFLUX_GZIP_THRESHOLD - payload size size for gzipping data
  • *
* Supported system properties: *
    *
  • influx.host - cloud/server URL required
  • *
  • influx.token - authentication token required
  • *
  • influx.authScheme - authentication scheme
  • *
  • influx.org - organization name
  • *
  • influx.database - database (bucket) name
  • *
  • influx.precision - timestamp precision when writing data
  • *
  • influx.gzipThreshold - payload size size for gzipping data
  • *
* * @return instance of {@link InfluxDBClient} */ @Nonnull static InfluxDBClient getInstance() { return getInstance(new ClientConfig.Builder().build(System.getenv(), System.getProperties())); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy