Java
Insert
Use the following code to insert an object to GreptimeDB:
java
TableSchema tableSchema = TableSchema.newBuilder(TableName.with("db_name", "monitor"))
.semanticTypes(SemanticType.Tag, SemanticType.Timestamp, SemanticType.Field, SemanticType.Field)
.dataTypes(ColumnDataType.String, ColumnDataType.Int64, ColumnDataType.Float64, ColumnDataType.Float64)
.columnNames("host", "ts", "cpu", "memory")
.build();
WriteRows rows = WriteRows.newBuilder(tableSchema).build();
rows.insert("127.0.0.1", System.currentTimeMillis(), 0.1, null)
.insert("127.0.0.2", System.currentTimeMillis(), 0.3, 0.5)
.finish();
CompletableFuture<Result<WriteOk, Err>> writeFuture = greptimeDB.write(rows);
writeFuture.whenComplete((result, throwable) -> {
if (throwable != null) {
throwable.printStackTrace();
} else {
System.out.println(result);
}
});
To begin, we must create a TableSchema
and then use it to construct a WriteRows
object. Since the TableSchema
can be reused, caching it can prevent unnecessary construction.
Once the WriteRows
object is created, data can be added to it. However, this data is only stored in memory and has not yet been sent to the server. To insert multiple rows efficiently, we use batch insertion. Once all desired data has been added to the WriteRows
, remember to call its finish
method before sending it to the server.
After calling the write
method on our completed WriteRows
, a future will be returned which allows us to obtain write results through its callback function.
Write API
java
/**
* Write a single table multi rows data to database.
*
* @param rows rows with one table
* @param ctx invoke context
* @return write result
*/
CompletableFuture<Result<WriteOk, Err>> write(WriteRows rows, Context ctx);
Name | Description |
---|---|
rows | Several rows of data to write to the database (all data must belong to the same table). |
ctx | The KV in ctx will be written to the gRPC headers metadata then sent to GreptimeDB server. |
Result<WriteOk, Err> | Inspired by Result in Rust, where WriteOk and Err only one is meaningful and the other is empty.If the call succeeds, you can extract the appropriate information from WriteOk, otherwise you need to extract useful information from Err. |