更新时间:2024-08-05 GMT+08:00

创建Kudu表

功能简介

通过KuduClient.createTable(String name, Schema schema, CreateTableOptions builder)方法创建表对象,其中需要指定表的schema和分区信息。

代码样例

如下是创建表的代码片段:

// Set up a table name.
String tableName = "example";

// Set up a simple schema.
List<ColumnSchema> columns = new ArrayList<>(2);
columns.add(new ColumnSchema.ColumnSchemaBuilder("key", Type.INT32)
        .key(true)
        .build());
columns.add(new ColumnSchema.ColumnSchemaBuilder("value",Type.STRING).nullable(true)
        .build());
Schema schema = new Schema(columns);

// Set up the partition schema, which distributes rows to different tablets by hash.
// Kudu also supports partitioning by key range. Hash and range partitioning can be combined.
// For more information, see http://kudu.apache.org/docs/schema_design.html.
CreateTableOptions cto = new CreateTableOptions();
List<String> hashKeys = new ArrayList<>(1);
hashKeys.add("key");
int numBuckets = 8;
cto.addHashPartitions(hashKeys, numBuckets);

// Create the table.
client.createTable(tableName, schema, cto);

示例代码中,定义了一张表,包含key和value两列。其中key是int32类型的主键字段,value是string类型的非主键字段且可以为空;同时该表在主键字段key上做了8个hash分区,表示数据会分成8个独立的tablet。