Skip to content
On this page



Data Ingestion Process

Use the following code to insert an object to GreptimeDB:

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")

WriteRows rows = WriteRows.newBuilder(tableSchema).build();

rows.insert("", System.currentTimeMillis(), 0.1, null)
    .insert("", System.currentTimeMillis(), 0.3, 0.5)

CompletableFuture<Result<WriteOk, Err>> writeFuture = greptimeDB.write(rows);

writeFuture.whenComplete((result, throwable) -> {
    if (throwable != null) {
    } else {

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

 * 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);
rowsSeveral rows of data to write to the database (all data must belong to the same table).
ctxThe 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.