本記事では、OBKV-Tableの書き込み機能について説明します。OBKV-Table Javaクライアントは、インデックス組織テーブルとヒープ組織テーブルへの単一行書き込みおよび一括書き込みをサポートしています。業務要件に応じて適切なAPIを選択してください。
書き込みAPI
以下のObTableClient APIが書き込み操作をサポートしています:
API |
説明 |
注意事項 |
|---|---|---|
insert(String tableName) |
単一行データの書き込み | 主キー競合時は書き込み失敗 |
insertOrUpdate(String tableName) |
単一行データの挿入または更新 | |
replace(String tableName) |
単一行データの置換 | 元の行が存在しない場合は新行を挿入 |
put(String tableName) |
単一行データの上書き |
|
書き込み例
前提条件
データを書き込む前に、以下の操作を完了してください:
ObTableClientを初期化しています。
データベースにインデックス組織テーブルまたはヒープ組織テーブルを作成しています。例:
-- Index-organized table CREATE TABLE IF NOT EXISTS `employees` ( `employeeId` int NOT NULL, `name` varchar(100) NOT NULL, `department` varchar(50), `position` varchar(50), `workYear` int, `comment` varchar(100), PRIMARY KEY (`employeeId`) ) PARTITION BY KEY(`employeeId`) PARTITIONS 97; -- Heap-organized table CREATE TABLE test ( c1 bigint, c2 varchar(20), c3 bigint, UNIQUE INDEX idx_unique_c1 (c1) -- A unique index can speed up queries ) ORGANIZATION = HEAP PARTITION BY KEY(c1) PARTITIONS 97; -- Use ORGANIZATION to specify a heap table
補足:
- JSONがサポートされています。
単一行書き込み
以下の例では、ObTableClientを使用して単一行データを書き込む方法を紹介します。
insert
この例では、主キーemployeeIdの値が1001の1行のデータを書き込みます。テーブルには4つの属性列が含まれます。
private static void insertRow(ObTableClient obkvHandle) throws Exception {
Row rowKey = row().add(colVal("employeeId", 1001));
Row properties = row().add(colVal("name", "zhangsan"))
.add(colVal("department", "R&D"))
.add(colVal("position", "dev"))
.add(colVal("workYear", 10));
obkvHandle.insert("employees")
.setRowKey(rowKey)
.addMutateRow(properties)
.execute();
}
この例では、insert APIを使用してヒープ組織テーブルに単一行データを書き込む方法を紹介します。ヒープ組織テーブルの場合、データが正しくルーティングされるように、パーティションキーを設定するためにsetPartitionKeyを呼び出す必要があります。
@Test
public void testSingleInsert() throws Exception {
try {
// Insert data
// Use setPartitionKey to set the partition key c1
// Use addMutateColVal to set the value of c1 as well
MutationResult result = client.insert("test")
.setPartitionKey(row(colVal("c1", 1000L)))
.addMutateColVal(colVal("c1", 1000L), colVal("c2", "test1"), colVal("c3", 3000L))
.execute();
assertEquals("Insert should affect 1 row", 1, result.getAffectedRows());
System.out.println("Single insert successful: " + result + " row affected");
} finally {
// Clean up data
truncateTable();
}
}
insertOrUpdate
この例では、主キー employeeId の値が 1001 で、4つの属性列を持つ1行のデータを挿入または更新します。
private static void insertOrUpdateRow(ObTableClient obkvHandle) throws Exception {
Row rowKey = row().add(colVal("employeeId", 1001));
Row properties = row().add(colVal("name", "zhangsan"))
.add(colVal("department", "R&D"))
.add(colVal("position", "dev"))
.add(colVal("workYear", 10));
obkvHandle.insertOrUpdate("employees")
.setRowKey(rowKey)
.addMutateRow(properties)
.execute();
}
この例では、insertOrUpdate API を使用してヒープ組織テーブルに単一行のデータを挿入または更新する方法を説明します。ヒープ組織テーブルでは、データが正しくルーティングされるように、パーティションキーを設定するために setPartitionKey を呼び出す必要があります。
@Test
public void testSingleInsertOrUpdate1() throws Exception {
try {
// Insert data
// Use setPartitionKey to set the partition key c1
// Use addMutateColVal to set the value of c1 as well
MutationResult result = client.insertOrUpdate(tableName)
.setPartitionKey(row(colVal("c1", 1000L)))
.addMutateColVal(colVal("c1", 1000L), colVal("c2", "test1"), colVal("c3", 3000L))
.execute();
assertEquals("Insert should affect 1 row", 1, result.getAffectedRows());
System.out.println("Single insertOrUpdate successful: " + result + " row affected");
} finally {
// Clean up data
truncateTable();
}
}
replace
この例では、主キー employeeId の値が 1001 で、4つの属性列を持つ1行のデータを置き換えます。
private static void replaceRow(ObTableClient obkvHandle) throws Exception {
Row rowKey = row().add(colVal("employeeId", 1001));
Row properties = row().add(colVal("name", "zhangsan"))
.add(colVal("department", "R&D"))
.add(colVal("position", "dev"))
.add(colVal("workYear", 10));
obkvHandle.replace("employees")
.setRowKey(rowKey)
.addMutateRow(properties)
.execute();
}
この例では、replace API を使用してヒープ組織テーブル内の単一行のデータを置き換える方法を説明します。ヒープ組織テーブルでは、データが正しくルーティングされるように、パーティションキーを設定するために setPartitionKey を呼び出す必要があります。
@Test
public void testSingleReplace1() throws Exception {
try {
// Insert data
// Use setPartitionKey to set the partition key c1
// Use addMutateColVal to set the value of c1 as well
MutationResult result = client.replace(tableName)
.setPartitionKey(row(colVal("c1", 1000L)))
.addMutateColVal(colVal("c1", 1000L), colVal("c2", "test1"), colVal("c3", 3000L))
.execute();
assertEquals("Insert should affect 1 row", 1, result.getAffectedRows());
System.out.println("Single replace successful: " + result + " row affected");
} finally {
// Clean up data
truncateTable();
}
}
put
注意
このAPIはヒープ組織テーブルをサポートしていません。
この例では、インデックス組織テーブル内の主キー employeeId の値が 1001 で、4つの属性列を持つ1行のデータを上書きします。
private static void putRow(ObTableClient obkvHandle) throws Exception {
Row rowKey = row().add(colVal("employeeId", 1001));
Row properties = row().add(colVal("name", "zhangsan"))
.add(colVal("department", "R&D"))
.add(colVal("position", "dev"))
.add(colVal("workYear", 1))
.add(colVal("comment", "new employee"));
obkvHandle.put("employees")
.setRowKey(rowKey)
.addMutateRow(properties)
.execute();
}
バッチ書き込み
以下の例では、ObTableClientを使用してデータを一括書き込む方法について説明します。
batchInsert
この例では、2行のデータを一括書き込みます。各行の主キー employeeId はそれぞれ 1001 と 1002 で、各行には4つの属性列が含まれます。
private static void batchInsertRow(ObTableClient obkvHandle) throws Exception {
Row rowKey1 = row().add(colVal("employeeId", 1001));
Row rowKey2 = row().add(colVal("employeeId", 1002));
Row properties1 = row().add(colVal("name", "zhangsan"))
.add(colVal("department", "R&D"))
.add(colVal("position", "dev"))
.add(colVal("workYear", 10));
Row properties2 = row().add(colVal("name", "lisi"))
.add(colVal("department", "R&D"))
.add(colVal("position", "dev"))
.add(colVal("workYear", 5));
Insert insertOp1 = insert().setRowKey(rowKey1).addMutateRow(properties1);
Insert insertOp2 = insert().setRowKey(rowKey2).addMutateRow(properties2);
BatchOperationResult res = obkvHandle.batchOperation("employees")
.addOperation(insertOp1,insertOp2)
.execute();
System.out.println(res.hasError());
}
この例では、batchInsert APIを使用してヒープ組織テーブルにデータを一括書き込む方法について説明します。ヒープ組織テーブルでは、データが正しくルーティングされるように、パーティションキーを設定するために setPartitionKey を呼び出す必要があります。
@Test
public void testBatchInsert() throws Exception {
try {
// Use BatchOperation for batch insert
BatchOperation batch = client.batchOperation("test");
// Add multiple insert operations
// Use setPartitionKey to set the partition key c1
// Use addMutateColVal to set the value of c1 as well
for (int i = 1; i <= 3; i++) {
Insert insert = new Insert();
insert.setPartitionKey(row(colVal("c1", (long)(1000 + i))))
.addMutateColVal(colVal("c1", (long)(1000 + i)), colVal("c2", "batch_test" + i), colVal("c3", (long)(3000 + i)));
batch.addOperation(insert);
}
batch.execute();
System.out.println("Batch insert successful: 3 operations executed");
} finally {
// Clean up data
truncateTable();
}
}
batchInsertOrUpdate
この例では、2行のデータを一括書き込みます。各行の主キー employeeId はそれぞれ 1001 と 1002 で、各行には4つの属性列が含まれます。
private static void batchInsertOrUpdateRow(ObTableClient obkvHandle) throws Exception {
Row rowKey1 = row().add(colVal("employeeId", 1001));
Row rowKey2 = row().add(colVal("employeeId", 1002));
Row properties1 = row().add(colVal("name", "zhangsan"))
.add(colVal("department", "R&D"))
.add(colVal("position", "dev"))
.add(colVal("workYear", 10));
Row properties2 = row().add(colVal("name", "lisi"))
.add(colVal("department", "R&D"))
.add(colVal("position", "dev"))
.add(colVal("workYear", 5));
InsertOrUpdate insertUpOp1 = insertOrUpdate().setRowKey(rowKey1).addMutateRow(properties1);
InsertOrUpdate insertUpOp2 = insertOrUpdate().setRowKey(rowKey2).addMutateRow(properties2);
BatchOperationResult res = obkvHandle.batchOperation("employees")
.addOperation(insertUpOp1,insertUpOp2)
.execute();
System.out.println(res.hasError());
}
この例では、batchInsertOrUpdate APIを使用してヒープ組織テーブルにデータを一括挿入または更新する方法について説明します。ヒープ組織テーブルでは、データが正しくルーティングされるように、パーティションキーを設定するために setPartitionKey を呼び出す必要があります。
@Test
public void testBatchInsertOrUpdate1() throws Exception {
try {
// Use BatchOperation for batch insert
BatchOperation batch = client.batchOperation(tableName);
// Add multiple insert operations
// Use setPartitionKey to set the partition key c1
// Use addMutateColVal to set the value of c1 as well
for (int i = 1; i <= 3; i++) {
InsertOrUpdate insertOrUpdate = new InsertOrUpdate();
insertOrUpdate.setPartitionKey(row(colVal("c1", (long)(1000 + i))))
.addMutateColVal(colVal("c1", (long)(1000 + i)), colVal("c2", "batch_test" + i), colVal("c3", (long)(3000 + i)));
batch.addOperation(insertOrUpdate);
}
batch.execute();
System.out.println("Batch insertOrUpdate successful: 3 operations executed");
} finally {
// Clean up data
truncateTable();
}
}
batchReplace
この例では、2行のデータを一括書き込みます。各行の主キー employeeId はそれぞれ 1001 と 1002 で、各行には4つの属性列が含まれます。
private static void batchReplaceRow(ObTableClient obkvHandle) throws Exception {
Row rowKey1 = row().add(colVal("employeeId", 1001));
Row rowKey2 = row().add(colVal("employeeId", 1002));
Row properties1 = row().add(colVal("name", "zhangsan"))
.add(colVal("department", "R&D"))
.add(colVal("position", "dev"))
.add(colVal("workYear", 10));
Row properties2 = row().add(colVal("name", "lisi"))
.add(colVal("department", "R&D"))
.add(colVal("position", "dev"))
.add(colVal("workYear", 5));
Replace replaceOp1 = replace().setRowKey(rowKey1).addMutateRow(properties1);
Replace replaceOp2 = replace().setRowKey(rowKey2).addMutateRow(properties2);
BatchOperationResult res = obkvHandle.batchOperation("employees")
.addOperation(replaceOp1,replaceOp2)
.execute();
System.out.println(res.hasError());
}
この例では、batchReplace APIを使用してヒープ組織テーブル内のデータを一括置き換える方法について説明します。ヒープ組織テーブルでは、データが正しくルーティングされるように、パーティションキーを設定するために setPartitionKey を呼び出す必要があります。
@Test
public void testBatchReplace1() throws Exception {
try {
// Use BatchOperation for batch insert
BatchOperation batch = client.batchOperation(tableName);
// Add multiple replace operations
// Use setPartitionKey to set the partition key c1
// Use addMutateColVal to set the value of c1 as well
for (int i = 1; i <= 3; i++) {
Replace replace = new Replace();
replace.setPartitionKey(row(colVal("c1", (long)(1000 + i))))
.addMutateColVal(colVal("c1", (long)(1000 + i)), colVal("c2", "batch_test" + i), colVal("c3", (long)(3000 + i)));
batch.addOperation(replace);
}
batch.execute();
System.out.println("Batch replace successful: 3 operations executed");
} finally {
// Clean up data
truncateTable();
}
}
batchPut
注意
このAPIはヒープ構造のテーブルをサポートしていません。
この例では、2行のデータを一括書き込みます。各行の主キー employeeId はそれぞれ 1001 と 1002 です。各行には、テーブルの全5つの属性列が含まれています。
private static void batchPutRow(ObTableClient obkvHandle) throws Exception {
Row rowKey1 = row().add(colVal("employeeId", 1001));
Row rowKey2 = row().add(colVal("employeeId", 1002));
Row properties1 = row().add(colVal("name", "zhangsan"))
.add(colVal("department", "R&D"))
.add(colVal("position", "dev"))
.add(colVal("workYear", 1))
.add(colVal("comment", "new employee"));
Row properties2 = row().add(colVal("name", "lisi"))
.add(colVal("department", "R&D"))
.add(colVal("position", "dev"))
.add(colVal("workYear", 1))
.add(colVal("comment", "new employee"));
Put putOp1 = put().setRowKey(rowKey1).addMutateRow(properties1);
Put putOp2 = put().setRowKey(rowKey2).addMutateRow(properties2);
BatchOperationResult res = obkvHandle.batchOperation("employees")
.addOperation(putOp1,putOp2)
.execute();
System.out.println(res.hasError());
}