org.influxdb.BatchOptions Java Examples

The following examples show how to use org.influxdb.BatchOptions. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: InfluxdbGateway.java    From redis-rdb-cli with Apache License 2.0 6 votes vote down vote up
protected InfluxDB create() {
    //
    final OkHttpClient.Builder http = new OkHttpClient.Builder();
    Dispatcher dispatcher = new Dispatcher();
    dispatcher.setMaxRequests(2); dispatcher.setMaxRequestsPerHost(2);
    http.dispatcher(dispatcher);
    http.connectionPool(new ConnectionPool(threads, 5, TimeUnit.MINUTES));
    
    //
    final InfluxDB r = InfluxDBFactory.connect(url, user, password, http);
    BatchOptions opt = DEFAULTS;
    opt = opt.consistency(consistency).jitterDuration(jitter);
    opt = opt.actions(this.actions).threadFactory(new XThreadFactory("influxdb"));
    opt = opt.exceptionHandler(new ExceptionHandler()).bufferLimit(capacity).flushDuration(interval);
    r.setDatabase(this.database).setRetentionPolicy(retention).enableBatch((opt)).enableGzip();
    return r;
}
 
Example #2
Source File: InfluxDBImpl.java    From influxdb-java with MIT License 6 votes vote down vote up
@Override
public InfluxDB enableBatch(final BatchOptions batchOptions) {

  if (this.batchEnabled.get()) {
    throw new IllegalStateException("BatchProcessing is already enabled.");
  }
  this.batchProcessor = BatchProcessor
          .builder(this)
          .actions(batchOptions.getActions())
          .exceptionHandler(batchOptions.getExceptionHandler())
          .interval(batchOptions.getFlushDuration(), batchOptions.getJitterDuration(), TimeUnit.MILLISECONDS)
          .threadFactory(batchOptions.getThreadFactory())
          .bufferLimit(batchOptions.getBufferLimit())
          .consistencyLevel(batchOptions.getConsistency())
          .precision(batchOptions.getPrecision())
          .build();
  this.batchEnabled.set(true);
  return this;
}
 
Example #3
Source File: StatsCollector.java    From cloudstack with Apache License 2.0 5 votes vote down vote up
/**
 * Writes batches of InfluxDB database points into a given database.
 */
protected void writeBatches(InfluxDB influxDbConnection, String dbName, List<Point> points) {
    BatchPoints batchPoints = BatchPoints.database(dbName).build();
    influxDbConnection.enableBatch(BatchOptions.DEFAULTS);

    for (Point point : points) {
        batchPoints.point(point);
    }

    influxDbConnection.write(batchPoints);
}
 
Example #4
Source File: InfluxDbAppender.java    From karaf-decanter with Apache License 2.0 4 votes vote down vote up
public void activate(Dictionary<String, Object> config) {
    this.config = config;
    if (config.get("url") == null) {
        throw new IllegalArgumentException("url property is mandatory");
    }
    String url = (String) config.get("url");
    String username = null;
    if (config.get("username") != null) {
        username = (String) config.get("username");
    }
    String password = null;
    if (config.get("password") != null) {
        password = (String) config.get("password");
    }
    if (username != null) {
        this.influxDB = InfluxDBFactory.connect(url, username, password);
    } else {
        this.influxDB = InfluxDBFactory.connect(url);
    }
    String database = "decanter";
    if (config.get("database") != null) {
        database = (String) config.get("database");
    }

    BatchOptions batchOptions = BatchOptions.DEFAULTS;
    if (config.get("batchActionsLimit") != null) {
        int batchActionsLimit = Integer.parseInt((String) config.get("batchActionsLimit"));
        batchOptions = batchOptions.actions(batchActionsLimit);
    }
    if (config.get("precision") != null) {
        TimeUnit timeUnit = TimeUnit.valueOf((String) config.get("precision"));
        batchOptions = batchOptions.precision(timeUnit);
    }

    if (config.get("flushDuration") != null) {
        int flushDuration = Integer.parseInt((String) config.get("flushDuration"));
        batchOptions = batchOptions.flushDuration(flushDuration);
    }

    String prefix = "tag.";
    for (Enumeration<String> e = config.keys(); e.hasMoreElements(); ) {
        String key = e.nextElement();
        if( key.startsWith(prefix)) {
            Object value = this.config.get(key);
            if (value != null)
                globalTags.put(key.substring(4),value.toString());
        }
    }

    this.influxDB.enableBatch(batchOptions);
    this.influxDB.setDatabase(database);
}
 
Example #5
Source File: InfluxDBImpl.java    From influxdb-java with MIT License 4 votes vote down vote up
@Override
public InfluxDB enableBatch() {
  enableBatch(BatchOptions.DEFAULTS);
  return this;
}
 
Example #6
Source File: BatchProcessorTest.java    From influxdb-java with MIT License 4 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void precision() throws Exception {
  String dbName = "write_unittest_" + System.currentTimeMillis();
  String rpName = "somePolicy";
  BatchWriter batchWriter;
  try (InfluxDB influxDB = TestUtils.connectToInfluxDB()) {
    try {
      influxDB.createDatabase(dbName);
      influxDB.query(new Query("CREATE RETENTION POLICY " + rpName + " ON " + dbName + " DURATION 30h REPLICATION 2 DEFAULT"));

      influxDB.enableBatch(BatchOptions.DEFAULTS.actions(2000).precision(TimeUnit.SECONDS).flushDuration(100));

      BatchProcessor batchProcessor = getPrivateField(influxDB, "batchProcessor");
      BatchWriter originalBatchWriter = getPrivateField(batchProcessor, "batchWriter");
      batchWriter = Mockito.spy(originalBatchWriter);
      setPrivateField(batchProcessor, "batchWriter", batchWriter);

      Point point1 = Point.measurement("cpu")
          .time(System.currentTimeMillis() /1000, TimeUnit.SECONDS)
          .addField("idle", 90L)
          .addField("user", 9L)
          .addField("system", 1L)
          .build();

      influxDB.write(dbName, rpName, point1);

    } finally {
      influxDB.deleteDatabase(dbName);
    }
  }

  ArgumentCaptor<Collection<BatchPoints>> argument = ArgumentCaptor.forClass(Collection.class);

  verify(batchWriter, atLeastOnce()).write(argument.capture());

  for (Collection<BatchPoints> list : argument.getAllValues()) {
    for (BatchPoints p : list) {
      assertTrue(p.toString().contains("precision=SECONDS"));
      assertFalse(p.toString().contains("precision=NANOSECONDS"));
    }
  }
}