Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dagger-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ jacocoTestCoverageVerification {
violationRules {
rule {
limit {
minimum = 0.87
minimum = 0.85
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import com.google.common.base.Strings;
import com.gotocompany.dagger.common.configuration.Configuration;
import org.apache.flink.util.Preconditions;
import org.influxdb.InfluxDB;
import org.influxdb.dto.Point;
import org.influxdb.dto.Point.Builder;
Expand All @@ -21,6 +22,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

public class InfluxDBWriter implements SinkWriter<Row, Void, Void> {
Expand All @@ -32,11 +34,13 @@ public class InfluxDBWriter implements SinkWriter<Row, Void, Void> {
private String[] columnNames;
private ErrorHandler errorHandler;
private ErrorReporter errorReporter;
private boolean useRowFieldNames;

public InfluxDBWriter(Configuration configuration, InfluxDB influxDB, String[] columnNames, ErrorHandler errorHandler, ErrorReporter errorReporter) {
databaseName = configuration.getString(Constants.SINK_INFLUX_DB_NAME_KEY, Constants.SINK_INFLUX_DB_NAME_DEFAULT);
retentionPolicy = configuration.getString(Constants.SINK_INFLUX_RETENTION_POLICY_KEY, Constants.SINK_INFLUX_RETENTION_POLICY_DEFAULT);
measurementName = configuration.getString(Constants.SINK_INFLUX_MEASUREMENT_NAME_KEY, Constants.SINK_INFLUX_MEASUREMENT_NAME_DEFAULT);
useRowFieldNames = configuration.getBoolean(Constants.SINK_INFLUX_USING_ROW_FIELD_NAMES_KEY, Constants.SINK_INFLUX_USING_ROW_FIELD_NAMES_DEFAULT);
this.influxDB = influxDB;
this.columnNames = columnNames;
this.errorHandler = errorHandler;
Expand All @@ -47,8 +51,27 @@ public InfluxDBWriter(Configuration configuration, InfluxDB influxDB, String[] c
public void write(Row row, Context context) throws IOException, InterruptedException {
LOGGER.info("row to influx: " + row);

Builder pointBuilder = Point.measurement(measurementName);
Builder pointBuilder;
Map<String, Object> fields = new HashMap<>();
if (useRowFieldNames) {
pointBuilder = writeUsingRowFieldNames(row, fields);
} else {
pointBuilder = writeUsingColumnNames(row, fields);
}

addErrorMetricsAndThrow();

try {
influxDB.write(databaseName, retentionPolicy, pointBuilder.fields(fields).build());
} catch (Exception exception) {
errorReporter.reportFatalException(exception);
throw exception;
}
}

private Builder writeUsingColumnNames(Row row, Map<String, Object> fields) {
Builder pointBuilder = Point.measurement(measurementName);

for (int i = 0; i < columnNames.length; i++) {
String columnName = columnNames[i];
if (columnName.equals("window_timestamp")) {
Expand All @@ -65,15 +88,31 @@ public void write(Row row, Context context) throws IOException, InterruptedExcep
}
}
}
return pointBuilder;
}

addErrorMetricsAndThrow();
private Builder writeUsingRowFieldNames(Row row, Map<String, Object> fields) {
Builder pointBuilder = Point.measurement(measurementName);

try {
influxDB.write(databaseName, retentionPolicy, pointBuilder.fields(fields).build());
} catch (Exception exception) {
errorReporter.reportFatalException(exception);
throw exception;
Set<String> fieldNames = row.getFieldNames(false);
Preconditions.checkNotNull(fieldNames, "Error! in writeUsingRowFieldNames, getFieldNames() returned null");

for (String fieldName : fieldNames) {
if (fieldName.equals("window_timestamp")) {
LocalDateTime timeField = (LocalDateTime) row.getField(fieldName);
ZonedDateTime zonedDateTime = timeField.atZone(ZoneOffset.UTC);
pointBuilder.time(zonedDateTime.toInstant().toEpochMilli(), TimeUnit.MILLISECONDS);
} else if (fieldName.startsWith("tag_")) {
pointBuilder.tag(fieldName, String.valueOf(row.getField(fieldName)));
} else if (fieldName.startsWith("label_")) {
pointBuilder.tag(fieldName.substring("label_".length()), String.valueOf(row.getField(fieldName)));
} else {
if (!(Strings.isNullOrEmpty(fieldName) || row.getField(fieldName) == null)) {
fields.put(fieldName, row.getField(fieldName));
}
}
}
return pointBuilder;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ public class Constants {
public static final int SINK_INFLUX_BATCH_SIZE_DEFAULT = 0;
public static final String SINK_INFLUX_FLUSH_DURATION_MS_KEY = "SINK_INFLUX_FLUSH_DURATION_MS";
public static final int SINK_INFLUX_FLUSH_DURATION_MS_DEFAULT = 0;
public static final String SINK_INFLUX_USING_ROW_FIELD_NAMES_KEY = "SINK_INFLUX_WITH_ROW_NAMES_WRITER";
public static final boolean SINK_INFLUX_USING_ROW_FIELD_NAMES_DEFAULT = false;

public static final String SOURCE_KAFKA_CONSUME_LARGE_MESSAGE_ENABLE_KEY = "SOURCE_KAFKA_CONSUME_LARGE_MESSAGE_ENABLE";
public static final boolean SOURCE_KAFKA_CONSUME_LARGE_MESSAGE_ENABLE_DEFAULT = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public void setUp() throws Exception {
when(configuration.getString(Constants.SINK_INFLUX_DB_NAME_KEY, Constants.SINK_INFLUX_DB_NAME_DEFAULT)).thenReturn("dagger_test");
when(configuration.getString(Constants.SINK_INFLUX_RETENTION_POLICY_KEY, Constants.SINK_INFLUX_RETENTION_POLICY_DEFAULT)).thenReturn("two_day_policy");
when(configuration.getString(Constants.SINK_INFLUX_MEASUREMENT_NAME_KEY, Constants.SINK_INFLUX_MEASUREMENT_NAME_DEFAULT)).thenReturn("test_table");
when(configuration.getString(Constants.SINK_INFLUX_USING_ROW_FIELD_NAMES_KEY, Constants.SINK_INFLUX_USING_ROW_FIELD_NAMES_KEY)).thenReturn("false");
when(initContext.metricGroup()).thenReturn(metricGroup);
when(metricGroup.addGroup(Constants.SINK_INFLUX_LATE_RECORDS_DROPPED_KEY)).thenReturn(metricGroup);
when(metricGroup.addGroup(Constants.NONFATAL_EXCEPTION_METRIC_GROUP_KEY,
Expand Down
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.12.0
0.12.1