Skip to content

Commit

Permalink
Merge pull request #137 from slomek/master
Browse files Browse the repository at this point in the history
Allow to write lines directly to the database
  • Loading branch information
majst01 committed Dec 14, 2015
2 parents abde80b + c64e751 commit b5dbfaf
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/main/java/org/influxdb/InfluxDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,26 @@ public String value() {
*/
public void write(final BatchPoints batchPoints);

/**
* Write a set of Points to the influxdb database with the string records.
*
* {@linkplain "https://github.com/influxdb/influxdb/pull/2696"}
*
* @param records
*/
public void write(final String database, final String retentionPolicy, final ConsistencyLevel consistency, final String records);

/**
* Write a set of Points to the influxdb database with the list of string records.
*
* {@linkplain "https://github.com/influxdb/influxdb/pull/2696"}
*
* @param records
*/
public void write(final String database, final String retentionPolicy, final ConsistencyLevel consistency, final List<String> records);

/**
/**
* Execute a query agains a database.
*
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/org/influxdb/impl/InfluxDBImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;

import com.google.common.base.Joiner;
import org.influxdb.InfluxDB;
import org.influxdb.dto.BatchPoints;
import org.influxdb.dto.Point;
Expand Down Expand Up @@ -153,6 +154,23 @@ public void write(final BatchPoints batchPoints) {

}

@Override
public void write(final String database, final String retentionPolicy, final ConsistencyLevel consistency, final String records) {
this.influxDBService.writePoints(
this.username,
this.password,
database,
retentionPolicy,
TimeUtil.toTimePrecision(TimeUnit.NANOSECONDS),
consistency.value(),
new TypedString(records));
}
@Override
public void write(final String database, final String retentionPolicy, final ConsistencyLevel consistency, final List<String> records) {
final String joinedRecords = Joiner.on("\n").join(records);
write(database, retentionPolicy, consistency, joinedRecords);
}

/**
* {@inheritDoc}
*/
Expand Down
58 changes: 58 additions & 0 deletions src/test/java/org/influxdb/InfluxDBTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.influxdb;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down Expand Up @@ -184,6 +185,63 @@ public void testWrite() {
this.influxDB.deleteDatabase(dbName);
}

/**
* Test writing to the database using string protocol.
*/
@Test(enabled = true)
public void testWriteStringData() {
String dbName = "write_unittest_" + System.currentTimeMillis();
this.influxDB.createDatabase(dbName);

this.influxDB.write(dbName, "default", InfluxDB.ConsistencyLevel.ONE, "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.
*/
@Test(enabled = true)
public void testWriteMultipleStringData() {
String dbName = "write_unittest_" + System.currentTimeMillis();
this.influxDB.createDatabase(dbName);

this.influxDB.write(dbName, "default", InfluxDB.ConsistencyLevel.ONE, "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.
*/
@Test(enabled = true)
public void testWriteMultipleStringDataLines() {
String dbName = "write_unittest_" + System.currentTimeMillis();
this.influxDB.createDatabase(dbName);

this.influxDB.write(dbName, "default", InfluxDB.ConsistencyLevel.ONE, 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
*/
Expand Down

0 comments on commit b5dbfaf

Please sign in to comment.