Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to write lines directly to the database #137

Merged
merged 1 commit into from
Dec 14, 2015
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
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