From 8bec5b80d5bc6c471c5bee0e13e39949c7494107 Mon Sep 17 00:00:00 2001 From: Andy Feller Date: Tue, 30 May 2017 11:34:23 -0400 Subject: [PATCH 1/2] Simplify the write methods, ignore maven directory - Added optional state to InfluxDB interface and implementation to provide database, retention policy, and consistency - Added new write() methods based on non-batch point calls to remove need to specify database, retention policy, and consistency - Added tests around new write() methods - Added .m2 directory to git ignore --- .gitignore | 3 +- src/main/java/org/influxdb/InfluxDB.java | 48 ++++++++++++++ .../java/org/influxdb/impl/InfluxDBImpl.java | 48 ++++++++++++++ src/test/java/org/influxdb/InfluxDBTest.java | 65 +++++++++++++++++++ 4 files changed, 163 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index d2ada48e7..fed44b33d 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,5 @@ target/ .settings/ test-output/ .idea/ -*iml \ No newline at end of file +*iml +.m2/ diff --git a/src/main/java/org/influxdb/InfluxDB.java b/src/main/java/org/influxdb/InfluxDB.java index 19cd6b956..0db309e46 100644 --- a/src/main/java/org/influxdb/InfluxDB.java +++ b/src/main/java/org/influxdb/InfluxDB.java @@ -113,6 +113,28 @@ public String value() { */ public String version(); + /** + * Write a single Point to the default database. + * + * @param point + * The point to write + */ + public void write(final Point point); + + /** + * Write a set of Points to the default database with the string records. + * + * @param records + */ + public void write(final String records); + + /** + * Write a set of Points to the default database with the list of string records. + * + * @param records + */ + public void write(final List records); + /** * Write a single Point to the database. * @@ -196,4 +218,30 @@ public String value() { */ public List describeDatabases(); + /** + * Set the consistency level which is used for writing points. + * + * @param consistency + * the consistency level to set. + * @return the InfluxDB instance to be able to use it in a fluent manner. + */ + public InfluxDB setConsistency(final ConsistencyLevel consistency); + + /** + * Set the database which is used for writing points. + * + * @param database + * the database to set. + * @return the InfluxDB instance to be able to use it in a fluent manner. + */ + public InfluxDB setDatabase(final String database); + + /** + * Set the retention policy which is used for writing points. + * + * @param retentionPolicy + * the retention policy to set. + * @return the InfluxDB instance to be able to use it in a fluent manner. + */ + public InfluxDB setRetentionPolicy(final String retentionPolicy); } diff --git a/src/main/java/org/influxdb/impl/InfluxDBImpl.java b/src/main/java/org/influxdb/impl/InfluxDBImpl.java index f17d064ea..0f5db96ee 100644 --- a/src/main/java/org/influxdb/impl/InfluxDBImpl.java +++ b/src/main/java/org/influxdb/impl/InfluxDBImpl.java @@ -50,6 +50,9 @@ public class InfluxDBImpl implements InfluxDB { private final AtomicLong batchedCount = new AtomicLong(); private final HttpLoggingInterceptor loggingInterceptor; private LogLevel logLevel = LogLevel.NONE; + private String database; + private String retentionPolicy = "autogen"; + private ConsistencyLevel consistency = ConsistencyLevel.ONE; public InfluxDBImpl(final String url, final String username, final String password, final OkHttpClient.Builder client) { @@ -66,6 +69,15 @@ public InfluxDBImpl(final String url, final String username, final String passwo this.influxDBService = this.retrofit.create(InfluxDBService.class); } + public InfluxDBImpl(final String url, final String username, final String password, + final OkHttpClient.Builder client, final String database, final String retentionPolicy, final ConsistencyLevel consistency) { + this(url, username, password, client); + + setConsistency(consistency); + setDatabase(database); + setRetentionPolicy(retentionPolicy); + } + @Override public InfluxDB setLogLevel(final LogLevel logLevel) { switch (logLevel) { @@ -146,6 +158,21 @@ public String version() { return ping().getVersion(); } + @Override + public void write(Point point) { + write(database, retentionPolicy, point); + } + + @Override + public void write(String records) { + write(database, retentionPolicy, consistency, records); + } + + @Override + public void write(List records) { + write(database, retentionPolicy, consistency, records); + } + @Override public void write(final String database, final String retentionPolicy, final Point point) { if (this.batchEnabled.get()) { @@ -256,6 +283,27 @@ public List describeDatabases() { return databases; } + @Override + public InfluxDB setConsistency(ConsistencyLevel consistency) { + + this.consistency = consistency; + return this; + } + + @Override + public InfluxDB setDatabase(String database) { + + this.database = database; + return this; + } + + @Override + public InfluxDB setRetentionPolicy(String retentionPolicy) { + + this.retentionPolicy = retentionPolicy; + return this; + } + private T execute(Call call) { try { Response response = call.execute(); diff --git a/src/test/java/org/influxdb/InfluxDBTest.java b/src/test/java/org/influxdb/InfluxDBTest.java index 6215ea15a..0971b2ab6 100644 --- a/src/test/java/org/influxdb/InfluxDBTest.java +++ b/src/test/java/org/influxdb/InfluxDBTest.java @@ -147,6 +147,23 @@ public void testWriteStringData() { this.influxDB.deleteDatabase(dbName); } + /** + * Test writing to the database using string protocol with simpler interface. + */ + @Test + public void testWriteStringDataSimple() { + String dbName = "write_unittest_" + System.currentTimeMillis(); + this.influxDB.createDatabase(dbName); + String rp = TestUtils.defaultRetentionPolicy(this.influxDB.version()); + this.influxDB.setDatabase(dbName); + this.influxDB.setRetentionPolicy(rp); + this.influxDB.write("cpu,atag=test idle=90,usertime=9,system=1"); + Query query = new Query("SELECT * FROM cpu GROUP BY *", dbName); + QueryResult result = this.influxDB.query(query); + Assert.assertFalse(result.getResults().get(0).getSeries().get(0).getTags().isEmpty()); + this.influxDB.deleteDatabase(dbName); + } + /** * Test writing multiple records to the database using string protocol. */ @@ -167,6 +184,28 @@ public void testWriteMultipleStringData() { this.influxDB.deleteDatabase(dbName); } + /** + * Test writing multiple records to the database using string protocol with simpler interface. + */ + @Test + public void testWriteMultipleStringDataSimple() { + String dbName = "write_unittest_" + System.currentTimeMillis(); + this.influxDB.createDatabase(dbName); + String rp = TestUtils.defaultRetentionPolicy(this.influxDB.version()); + this.influxDB.setDatabase(dbName); + this.influxDB.setRetentionPolicy(rp); + + this.influxDB.write("cpu,atag=test1 idle=100,usertime=10,system=1\ncpu,atag=test2 idle=200,usertime=20,system=2\ncpu,atag=test3 idle=300,usertime=30,system=3"); + Query query = new Query("SELECT * FROM cpu GROUP BY *", dbName); + QueryResult result = this.influxDB.query(query); + + Assert.assertEquals(result.getResults().get(0).getSeries().size(), 3); + Assert.assertEquals(result.getResults().get(0).getSeries().get(0).getTags().get("atag"), "test1"); + Assert.assertEquals(result.getResults().get(0).getSeries().get(1).getTags().get("atag"), "test2"); + Assert.assertEquals(result.getResults().get(0).getSeries().get(2).getTags().get("atag"), "test3"); + this.influxDB.deleteDatabase(dbName); + } + /** * Test writing multiple separate records to the database using string protocol. */ @@ -191,6 +230,32 @@ public void testWriteMultipleStringDataLines() { this.influxDB.deleteDatabase(dbName); } + /** + * Test writing multiple separate records to the database using string protocol with simpler interface. + */ + @Test + public void testWriteMultipleStringDataLinesSimple() { + String dbName = "write_unittest_" + System.currentTimeMillis(); + this.influxDB.createDatabase(dbName); + String rp = TestUtils.defaultRetentionPolicy(this.influxDB.version()); + this.influxDB.setDatabase(dbName); + this.influxDB.setRetentionPolicy(rp); + + this.influxDB.write(Arrays.asList( + "cpu,atag=test1 idle=100,usertime=10,system=1", + "cpu,atag=test2 idle=200,usertime=20,system=2", + "cpu,atag=test3 idle=300,usertime=30,system=3" + )); + Query query = new Query("SELECT * FROM cpu GROUP BY *", dbName); + QueryResult result = this.influxDB.query(query); + + Assert.assertEquals(result.getResults().get(0).getSeries().size(), 3); + Assert.assertEquals(result.getResults().get(0).getSeries().get(0).getTags().get("atag"), "test1"); + Assert.assertEquals(result.getResults().get(0).getSeries().get(1).getTags().get("atag"), "test2"); + Assert.assertEquals(result.getResults().get(0).getSeries().get(2).getTags().get("atag"), "test3"); + this.influxDB.deleteDatabase(dbName); + } + /** * Test that creating database which name is composed of numbers only works */ From 165d8592b5f7f272056f7967866a084d4b47a79d Mon Sep 17 00:00:00 2001 From: Andy Feller Date: Tue, 30 May 2017 11:44:17 -0400 Subject: [PATCH 2/2] updated readme to highlight new simpler write() methods --- README.md | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9d8d25fd5..c698fa1f2 100644 --- a/README.md +++ b/README.md @@ -72,6 +72,36 @@ influxDB.query(query); influxDB.deleteDatabase(dbName); ``` +If all of your points are written to the same database and retention policy, the simpler write() methods can be used. + +```java +InfluxDB influxDB = InfluxDBFactory.connect("http://172.17.0.2:8086", "root", "root"); +String dbName = "aTimeSeries"; +influxDB.createDatabase(dbName); +influxDB.setDatabase(dbName); +influxDB.setRetentionPolicy("autogen"); + +// Flush every 2000 Points, at least every 100ms +influxDB.enableBatch(2000, 100, TimeUnit.MILLISECONDS); + +influxDB.write(Point.measurement("cpu") + .time(System.currentTimeMillis(), TimeUnit.MILLISECONDS) + .addField("idle", 90L) + .addField("user", 9L) + .addField("system", 1L) + .build()); + +influxDB.write(Point.measurement("disk") + .time(System.currentTimeMillis(), TimeUnit.MILLISECONDS) + .addField("used", 80L) + .addField("free", 1L) + .build()); + +Query query = new Query("SELECT idle FROM cpu", dbName); +influxDB.query(query); +influxDB.deleteDatabase(dbName); +``` + ### Changes in 2.4 influxdb-java now uses okhttp3 and retrofit2. As a result, you can now pass an ``OkHttpClient.Builder`` to the ``InfluxDBFactory.connect`` if you wish to add more interceptors, etc, to OkHttp. @@ -87,7 +117,7 @@ that allow this to be specified (default is still GET). org.influxdb influxdb-java - 2.3 + 2.4 ```