Java Code Examples for org.influxdb.dto.Point#Builder

The following examples show how to use org.influxdb.dto.Point#Builder . 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: InfluxDBSink.java    From flink-learning with Apache License 2.0 6 votes vote down vote up
@Override
public void invoke(MetricEvent metricEvent, Context context) throws Exception {
    if (StringUtils.isNullOrWhitespaceOnly(metricEvent.getName())) {
        throw new RuntimeException("No measurement defined");
    }

    Point.Builder builder = Point.measurement(metricEvent.getName())
            .time(metricEvent.getTimestamp(), TimeUnit.MILLISECONDS);

    if (!CollectionUtil.isNullOrEmpty(metricEvent.getFields())) {
        builder.fields(metricEvent.getFields());
    }

    if (!CollectionUtil.isNullOrEmpty(metricEvent.getTags())) {
        builder.tag(metricEvent.getTags());
    }

    Point point = builder.build();
    influxDBClient.write(point);
}
 
Example 2
Source File: TicketTest.java    From influxdb-java with MIT License 6 votes vote down vote up
/**
 * Test for ticket #39
 *
 */
@Test
public void testTicket39() {
	String dbName = "ticket39_" + System.currentTimeMillis();
	this.influxDB.query(new Query("CREATE DATABASE " + dbName));
	BatchPoints batchPoints = BatchPoints
			.database(dbName)
			.tag("async", "true")
			.retentionPolicy(TestUtils.defaultRetentionPolicy(this.influxDB.version()))
			.consistency(InfluxDB.ConsistencyLevel.ALL)
			.build();
	Point.Builder builder = Point.measurement("my_type");
	builder.addField("my_field", "string_value");
	Point point = builder.build();
	batchPoints.point(point);
	this.influxDB.write(batchPoints);
	this.influxDB.query(new Query("DROP DATABASE " + dbName));
}
 
Example 3
Source File: InfluxdbDao.java    From dapeng-soa with Apache License 2.0 6 votes vote down vote up
public void writePoint(DataPoint dataPoint) {
    if (null == influxDB) {
        influxDB = getInfluxDBConnection();
    }
    long now = System.currentTimeMillis();
    Point.Builder commit = Point.measurement(dataPoint.bizTag);
    dataPoint.values.forEach(commit::addField);
    dataPoint.tags.forEach(commit::tag);
    commit.time(dataPoint.getTimestamp() == 0 ? now : dataPoint.getTimestamp(), TimeUnit.MILLISECONDS);
    try {
        influxDB.write(dataPoint.database, "", commit.build());
    } finally {
        if (influxDB != null) {
            influxDB.close();
        }
    }
}
 
Example 4
Source File: InfluxDBSink.java    From flink-learning with Apache License 2.0 6 votes vote down vote up
@Override
public void invoke(MetricEvent metricEvent, Context context) throws Exception {
    if (StringUtils.isNullOrWhitespaceOnly(metricEvent.getName())) {
        throw new RuntimeException("No measurement defined");
    }

    Point.Builder builder = Point.measurement(metricEvent.getName())
            .time(metricEvent.getTimestamp(), TimeUnit.MILLISECONDS);

    if (!CollectionUtil.isNullOrEmpty(metricEvent.getFields())) {
        builder.fields(metricEvent.getFields());
    }

    if (!CollectionUtil.isNullOrEmpty(metricEvent.getTags())) {
        builder.tag(metricEvent.getTags());
    }

    Point point = builder.build();
    influxDBClient.write(point);
}
 
Example 5
Source File: InfluxDBMapper.java    From influxdb-java with MIT License 6 votes vote down vote up
private void setField(
    final Point.Builder pointBuilder,
    final Class<?> fieldType,
    final String columnName,
    final Object value) {
  if (boolean.class.isAssignableFrom(fieldType) || Boolean.class.isAssignableFrom(fieldType)) {
    pointBuilder.addField(columnName, (boolean) value);
  } else if (long.class.isAssignableFrom(fieldType) || Long.class.isAssignableFrom(fieldType)) {
    pointBuilder.addField(columnName, (long) value);
  } else if (double.class.isAssignableFrom(fieldType)
      || Double.class.isAssignableFrom(fieldType)) {
    pointBuilder.addField(columnName, (double) value);
  } else if (int.class.isAssignableFrom(fieldType) || Integer.class.isAssignableFrom(fieldType)) {
    pointBuilder.addField(columnName, (int) value);
  } else if (String.class.isAssignableFrom(fieldType)) {
    pointBuilder.addField(columnName, (String) value);
  } else {
    throw new InfluxDBMapperException(
        "Unsupported type " + fieldType + " for column " + columnName);
  }
}
 
Example 6
Source File: InfluxDBUploader.java    From xDrip with GNU General Public License v3.0 6 votes vote down vote up
private Point createCalibrationPoint(Calibration record) {
    Point.Builder builder = Point.measurement("calibration")
            .time(record.timestamp, TimeUnit.MILLISECONDS)
            .tag("device", "xDrip-" + prefs.getString("dex_collection_method", "BluetoothWixel"))
            .tag("type", "cal");

    if (record.check_in) {
        builder.addField("slope", record.first_slope)
                .addField("intercept", record.first_intercept)
                .addField("scale", record.first_scale);
    } else {
        builder.addField("slope", (1000 / record.slope))
                .addField("intercept", ((record.intercept * -1000) / record.slope))
                .addField("scale", 1);
    }

    return builder.build();
}
 
Example 7
Source File: InfluxDBUploader.java    From xDrip-plus with GNU General Public License v3.0 6 votes vote down vote up
private Point createCalibrationPoint(Calibration record) {
    Point.Builder builder = Point.measurement("calibration")
            .time(record.timestamp, TimeUnit.MILLISECONDS)
            .tag("device", "xDrip-" + prefs.getString("dex_collection_method", "BluetoothWixel"))
            .tag("type", "cal");

    if (record.check_in) {
        builder.addField("slope", record.first_slope)
                .addField("intercept", record.first_intercept)
                .addField("scale", record.first_scale);
    } else {
        builder.addField("slope", (1000 / record.slope))
                .addField("intercept", ((record.intercept * -1000) / record.slope))
                .addField("scale", 1);
    }

    return builder.build();
}
 
Example 8
Source File: InfluxDBSink.java    From bahir-flink with Apache License 2.0 6 votes vote down vote up
/**
 * Called when new data arrives to the sink, and forwards it to InfluxDB.
 *
 * @param dataPoint {@link InfluxDBPoint}
 */
@Override
public void invoke(InfluxDBPoint dataPoint, Context context) throws Exception {
    if (StringUtils.isNullOrWhitespaceOnly(dataPoint.getMeasurement())) {
        throw new RuntimeException("No measurement defined");
    }

    Point.Builder builder = Point.measurement(dataPoint.getMeasurement())
            .time(dataPoint.getTimestamp(), TimeUnit.MILLISECONDS);

    if (!CollectionUtil.isNullOrEmpty(dataPoint.getFields())) {
        builder.fields(dataPoint.getFields());
    }

    if (!CollectionUtil.isNullOrEmpty(dataPoint.getTags())) {
        builder.tag(dataPoint.getTags());
    }

    Point point = builder.build();
    influxDBClient.write(point);
}
 
Example 9
Source File: InfluxDBReporter.java    From statsd-jvm-profiler with MIT License 5 votes vote down vote up
private Point   constructPoint(long time, String key, Number value) {
    Point.Builder builder = Point.measurement(key)
            .time(time, TimeUnit.MILLISECONDS)
            .field(VALUE_COLUMN, value);
    for (Map.Entry<String, String> entry : tags.entrySet()) {
        builder = builder.tag(entry.getKey(), entry.getValue());
    }

    return builder.build();
}
 
Example 10
Source File: MetricMapper.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
static Point map(MeasurementInfo info, Instant timestamp, Gauge<?> gauge) {
	Point.Builder builder = builder(info, timestamp);
	Object value = gauge.getValue();
	if (value instanceof Number) {
		builder.addField("value", (Number) value);
	} else {
		builder.addField("value", String.valueOf(value));
	}
	return builder.build();
}
 
Example 11
Source File: InfluxDbPublisher.java    From kafka-metrics with Apache License 2.0 5 votes vote down vote up
public void tryPublish(MeasurementV1 m) {
    if (influxDB == null) {
        influxDB = InfluxDBFactory.connect(address, username, password);
        influxDB.enableBatch(1000, 100, TimeUnit.MILLISECONDS);
    }
    Point.Builder builder = Point.measurement(m.getName().toString()).time(m.getTimestamp(), TimeUnit.MILLISECONDS);
    for (java.util.Map.Entry<String, String> tag : m.getTags().entrySet()) {
        builder.tag(tag.getKey().toString(), tag.getValue().toString());
    }
    for (java.util.Map.Entry<String, Double> field : m.getFields().entrySet()) {
        builder.field(field.getKey().toString(), field.getValue());
    }
    influxDB.write(dbName, retention, builder.build());
}
 
Example 12
Source File: InfluxHistory.java    From monsoon with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private Point.Builder getOrCreatePlain() {
    if (plainValue.isEmpty()) {
        final Point.Builder newPlainValue = template.getBuilder();
        plainValue.add(newPlainValue);
        return newPlainValue;
    }

    Point.Builder last = plainValue.get(plainValue.size() - 1);
    if (verifyDatabaseConnection && last.build().lineProtocol().length() >= 32768) {
        last = template.getBuilder();
        plainValue.add(last);
    }
    return last;
}
 
Example 13
Source File: MetricMapper.java    From flink with Apache License 2.0 5 votes vote down vote up
static Point map(MeasurementInfo info, Instant timestamp, Gauge<?> gauge) {
	Point.Builder builder = builder(info, timestamp);
	Object value = gauge.getValue();
	if (value instanceof Number) {
		builder.addField("value", (Number) value);
	} else {
		builder.addField("value", String.valueOf(value));
	}
	return builder.build();
}
 
Example 14
Source File: InfluxDBConverter.java    From RuuviCollector with MIT License 5 votes vote down vote up
private static void createAndAddLegacyFormatPointIfNotNull(List<Point> points, String measurement, Number value, String extraTagKey, String extraTagValue) {
    if (value != null) {
        Point.Builder p = Point.measurement(measurement).addField("value", value);
        if (extraTagValue != null) {
            p.tag(extraTagKey, extraTagValue);
        }
        points.add(p.build());
    }
}
 
Example 15
Source File: InfluxDBConverter.java    From RuuviCollector with MIT License 5 votes vote down vote up
private static void addValueIfAllowed(Point.Builder point,
                                      String name,
                                      EnhancedRuuviMeasurement measurement,
                                      Function<EnhancedRuuviMeasurement, ? extends Number> getter,
                                      Predicate<String> allowField) {
    final Number value = getter.apply(measurement);
    if (value != null && allowField.test(name)) {
        point.addField(name, value);
    }
}
 
Example 16
Source File: InfluxDBConverter.java    From RuuviCollector with MIT License 5 votes vote down vote up
/**
 * Converts a {@link EnhancedRuuviMeasurement} into an {@link org.influxdb.dto.Point}.
 *
 * @param measurement The measurement to convert
 * @param allowField A function that tells whether any given field of the given {@code RuuviMeasurement} should
 *                   be included in the resulting {@code Point} or not
 * @return A {@code Point}, ready to be saved into InfluxDB
 */
public static Point toInflux(EnhancedRuuviMeasurement measurement, Predicate<String> allowField) {
    Point.Builder p = Point.measurement(Config.getInfluxMeasurement()).tag("mac", measurement.getMac());
    if (measurement.getName() != null) {
        p.tag("name", measurement.getName());
    }
    if (measurement.getDataFormat() != null) {
        p.tag("dataFormat", String.valueOf(measurement.getDataFormat()));
    }
    if (measurement.getTime() != null) {
        p.time(measurement.getTime(), TimeUnit.MILLISECONDS);
    }
    addValueIfAllowed(p, "temperature", measurement, EnhancedRuuviMeasurement::getTemperature, allowField);
    addValueIfAllowed(p, "humidity", measurement, EnhancedRuuviMeasurement::getHumidity, allowField);
    addValueIfAllowed(p, "pressure", measurement, EnhancedRuuviMeasurement::getPressure, allowField);
    addValueIfAllowed(p, "accelerationX", measurement, EnhancedRuuviMeasurement::getAccelerationX, allowField);
    addValueIfAllowed(p, "accelerationY", measurement, EnhancedRuuviMeasurement::getAccelerationY, allowField);
    addValueIfAllowed(p, "accelerationZ", measurement, EnhancedRuuviMeasurement::getAccelerationZ, allowField);
    addValueIfAllowed(p, "batteryVoltage", measurement, EnhancedRuuviMeasurement::getBatteryVoltage, allowField);
    addValueIfAllowed(p, "txPower", measurement, EnhancedRuuviMeasurement::getTxPower, allowField);
    addValueIfAllowed(p, "movementCounter", measurement, EnhancedRuuviMeasurement::getMovementCounter, allowField);
    addValueIfAllowed(p, "measurementSequenceNumber", measurement, EnhancedRuuviMeasurement::getMeasurementSequenceNumber, allowField);
    addValueIfAllowed(p, "rssi", measurement, EnhancedRuuviMeasurement::getRssi, allowField);
    addValueIfAllowed(p, "accelerationTotal", measurement, EnhancedRuuviMeasurement::getAccelerationTotal, allowField);
    addValueIfAllowed(p, "absoluteHumidity", measurement, EnhancedRuuviMeasurement::getAbsoluteHumidity, allowField);
    addValueIfAllowed(p, "dewPoint", measurement, EnhancedRuuviMeasurement::getDewPoint, allowField);
    addValueIfAllowed(p, "equilibriumVaporPressure", measurement, EnhancedRuuviMeasurement::getEquilibriumVaporPressure, allowField);
    addValueIfAllowed(p, "airDensity", measurement, EnhancedRuuviMeasurement::getAirDensity, allowField);
    addValueIfAllowed(p, "accelerationAngleFromX", measurement, EnhancedRuuviMeasurement::getAccelerationAngleFromX, allowField);
    addValueIfAllowed(p, "accelerationAngleFromY", measurement, EnhancedRuuviMeasurement::getAccelerationAngleFromY, allowField);
    addValueIfAllowed(p, "accelerationAngleFromZ", measurement, EnhancedRuuviMeasurement::getAccelerationAngleFromZ, allowField);
    return p.build();
}
 
Example 17
Source File: InfluxHistory.java    From monsoon with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public Point.Builder getBuilder(@NonNull Histogram.Range range) {
    return getBuilder()
            .tag(MONSOON_RANGE_TAG, range.getFloor() + ".." + range.getCeil());
}
 
Example 18
Source File: InfluxHistory.java    From monsoon with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public Stream<Point.Builder> stream() {
    final Stream<Point.Builder> plainStream = plainValue.stream();
    final Stream<Point.Builder> histogramStream = histogramValue.values().stream();
    return Stream.concat(plainStream, histogramStream);
}
 
Example 19
Source File: MetricMapper.java    From flink with Apache License 2.0 4 votes vote down vote up
private static Point.Builder builder(MeasurementInfo info, Instant timestamp) {
	return Point.measurement(info.getName())
		.tag(info.getTags())
		.time(timestamp.toEpochMilli(), TimeUnit.MILLISECONDS);
}
 
Example 20
Source File: InfluxDBMapper.java    From influxdb-java with MIT License 4 votes vote down vote up
public <T> void save(final T model) {
  throwExceptionIfMissingAnnotation(model.getClass());
  cacheMeasurementClass(model.getClass());

  ConcurrentMap<String, Field> colNameAndFieldMap = getColNameAndFieldMap(model.getClass());

  try {
    Class<?> modelType = model.getClass();
    String measurement = getMeasurementName(modelType);
    String database = getDatabaseName(modelType);
    String retentionPolicy = getRetentionPolicy(modelType);
    TimeUnit timeUnit = getTimeUnit(modelType);
    long time = timeUnit.convert(System.currentTimeMillis(), TimeUnit.MILLISECONDS);
    Point.Builder pointBuilder = Point.measurement(measurement).time(time, timeUnit);

    for (String key : colNameAndFieldMap.keySet()) {
      Field field = colNameAndFieldMap.get(key);
      Column column = field.getAnnotation(Column.class);
      String columnName = column.name();
      Class<?> fieldType = field.getType();

      if (!field.isAccessible()) {
        field.setAccessible(true);
      }

      Object value = field.get(model);

      if (column.tag()) {
        /** Tags are strings either way. */
        pointBuilder.tag(columnName, value.toString());
      } else if ("time".equals(columnName)) {
        if (value != null) {
          setTime(pointBuilder, fieldType, timeUnit, value);
        }
      } else {
        setField(pointBuilder, fieldType, columnName, value);
      }
    }

    Point point = pointBuilder.build();

    if ("[unassigned]".equals(database)) {
      influxDB.write(point);
    } else {
      influxDB.write(database, retentionPolicy, point);
    }

  } catch (IllegalAccessException e) {
    throw new InfluxDBMapperException(e);
  }
}