Contoh berikut menggunakan SDK Java.
Sisipkan satu baris
PutRow mendukung nomor versi yang dihasilkan sistem, nomor versi kustom, dan penulisan bersyarat.
Nomor versi yang dihasilkan sistem
Sisipkan satu baris dengan 10 kolom atribut, masing-masing menyimpan satu versi data. Sistem secara otomatis menghasilkan nomor versinya.
private static void putRow(SyncClient client, String pkValue) {
// Construct the primary key.
PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
primaryKeyBuilder.addPrimaryKeyColumn("pk", PrimaryKeyValue.fromString(pkValue));
PrimaryKey primaryKey = primaryKeyBuilder.build();
// Specify the name of the table.
RowPutChange rowPutChange = new RowPutChange("<TABLE_NAME>", primaryKey);
// Add attribute columns.
for (int i = 0; i < 10; i++) {
rowPutChange.addColumn(new Column("Col" + i, ColumnValue.fromLong(i)));
}
client.putRow(new PutRowRequest(rowPutChange));
}
Nomor versi kustom
Sisipkan satu baris dengan 10 kolom atribut, masing-masing menyimpan tiga versi data dengan nomor versi kustom.
private static void putRow(SyncClient client, String pkValue) {
// Construct the primary key.
PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
primaryKeyBuilder.addPrimaryKeyColumn("pk", PrimaryKeyValue.fromString(pkValue));
PrimaryKey primaryKey = primaryKeyBuilder.build();
// Specify the name of the table.
RowPutChange rowPutChange = new RowPutChange("<TABLE_NAME>", primaryKey);
// Add attribute columns.
long ts = System.currentTimeMillis();
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 3; j++) {
rowPutChange.addColumn(new Column("Col" + i, ColumnValue.fromLong(j), ts + j));
}
}
client.putRow(new PutRowRequest(rowPutChange));
}
Kondisi keberadaan baris
Sisipkan satu baris dengan 10 kolom atribut (masing-masing tiga versi) hanya jika baris yang ditentukan tidak ada.
private static void putRow(SyncClient client, String pkValue) {
// Construct the primary key.
PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
primaryKeyBuilder.addPrimaryKeyColumn("pk", PrimaryKeyValue.fromString(pkValue));
PrimaryKey primaryKey = primaryKeyBuilder.build();
// Specify the name of the table.
RowPutChange rowPutChange = new RowPutChange("<TABLE_NAME>", primaryKey);
// Specify a row existence condition that expects the specified row to not exist.
rowPutChange.setCondition(new Condition(RowExistenceExpectation.EXPECT_NOT_EXIST));
// Add attribute columns.
long ts = System.currentTimeMillis();
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 3; j++) {
rowPutChange.addColumn(new Column("Col" + i, ColumnValue.fromLong(j), ts + j));
}
}
client.putRow(new PutRowRequest(rowPutChange));
}
Kondisi keberadaan baris dan nilai kolom
Sisipkan satu baris dengan 10 kolom atribut (masing-masing tiga versi) ketika baris tersebut ada dan nilai Col0 lebih besar dari 100.
private static void putRow(SyncClient client, String pkValue) {
// Construct the primary key.
PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
primaryKeyBuilder.addPrimaryKeyColumn("pk", PrimaryKeyValue.fromString(pkValue));
PrimaryKey primaryKey = primaryKeyBuilder.build();
// Specify the name of the table.
RowPutChange rowPutChange = new RowPutChange("<TABLE_NAME>", primaryKey);
// Specify a row existence condition and a column-based condition that expect the specified row to exist and the value of the Col0 column to be greater than 100.
Condition condition = new Condition(RowExistenceExpectation.EXPECT_EXIST);
condition.setColumnCondition(new SingleColumnValueCondition("Col0",
SingleColumnValueCondition.CompareOperator.GREATER_THAN, ColumnValue.fromLong(100)));
rowPutChange.setCondition(condition);
// Add attribute columns.
long ts = System.currentTimeMillis();
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 3; j++) {
rowPutChange.addColumn(new Column("Col" + i, ColumnValue.fromLong(j), ts + j));
}
}
client.putRow(new PutRowRequest(rowPutChange));
}
Perbarui satu baris
UpdateRow mendukung pembaruan tanpa syarat maupun pembaruan bersyarat berdasarkan keberadaan baris dan nilai kolom.
Perbarui tanpa syarat
Perbarui beberapa kolom, hapus versi tertentu dari data dalam suatu kolom, dan hapus kolom.
private static void updateRow(SyncClient client, String pkValue) {
// Construct the primary key.
PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
primaryKeyBuilder.addPrimaryKeyColumn(PRIMARY_KEY_NAME, PrimaryKeyValue.fromString(pkValue));
PrimaryKey primaryKey = primaryKeyBuilder.build();
// Specify the name of the table.
RowUpdateChange rowUpdateChange = new RowUpdateChange("<TABLE_NAME>", primaryKey);
// Update columns.
for (int i = 0; i < 10; i++) {
rowUpdateChange.put(new Column("Col" + i, ColumnValue.fromLong(i)));
}
// Delete a specific version of data from a column.
rowUpdateChange.deleteColumn("Col10", 1465373223000L);
// Remove a column.
rowUpdateChange.deleteColumns("Col11");
client.updateRow(new UpdateRowRequest(rowUpdateChange));
}
Kondisi keberadaan baris dan nilai kolom
Perbarui baris ketika baris tersebut ada dan nilai Col0 lebih besar dari 100.
private static void updateRow(SyncClient client, String pkValue) {
// Construct the primary key.
PrimaryKeyBuilder primaryKeyBuilder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
primaryKeyBuilder.addPrimaryKeyColumn(PRIMARY_KEY_NAME, PrimaryKeyValue.fromString(pkValue));
PrimaryKey primaryKey = primaryKeyBuilder.build();
// Specify the name of the table.
RowUpdateChange rowUpdateChange = new RowUpdateChange("<TABLE_NAME>", primaryKey);
// Specify a row existence condition and a column-based condition that expect the specified row to exist and the value of the Col0 column to be greater than 100.
Condition condition = new Condition(RowExistenceExpectation.EXPECT_EXIST);
condition.setColumnCondition(new SingleColumnValueCondition("Col0",
SingleColumnValueCondition.CompareOperator.GREATER_THAN, ColumnValue.fromLong(100)));
rowUpdateChange.setCondition(condition);
// Update columns.
for (int i = 0; i < 10; i++) {
rowUpdateChange.put(new Column("Col" + i, ColumnValue.fromLong(i)));
}
// Delete a specific version of data from a column.
rowUpdateChange.deleteColumn("Col10", 1465373223000L);
// Remove a column.
rowUpdateChange.deleteColumns("Col11");
client.updateRow(new UpdateRowRequest(rowUpdateChange));
}
Tulis beberapa baris sekaligus
Contoh berikut mengirim permintaan BatchWriteRow yang berisi dua operasi PutRow, satu operasi UpdateRow, dan satu operasi DeleteRow.
private static void batchWriteRow(SyncClient client) {
BatchWriteRowRequest batchWriteRowRequest = new BatchWriteRowRequest();
// Construct rowPutChange1.
PrimaryKeyBuilder pk1Builder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
pk1Builder.addPrimaryKeyColumn("pk", PrimaryKeyValue.fromString("pk1"));
// Specify the name of the data table.
RowPutChange rowPutChange1 = new RowPutChange("<TABLE_NAME>", pk1Builder.build());
// Add columns.
for (int i = 0; i < 10; i++) {
rowPutChange1.addColumn(new Column("Col" + i, ColumnValue.fromLong(i)));
}
// Add rowPutChange1 to the code of the batch operation.
batchWriteRowRequest.addRowChange(rowPutChange1);
// Construct rowPutChange2.
PrimaryKeyBuilder pk2Builder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
pk2Builder.addPrimaryKeyColumn("pk", PrimaryKeyValue.fromString("pk2"));
// Specify the name of the data table.
RowPutChange rowPutChange2 = new RowPutChange("<TABLE_NAME>", pk2Builder.build());
// Add columns.
for (int i = 0; i < 10; i++) {
rowPutChange2.addColumn(new Column("Col" + i, ColumnValue.fromLong(i)));
}
// Add rowPutChange2 to the code of the batch operation.
batchWriteRowRequest.addRowChange(rowPutChange2);
// Construct rowUpdateChange.
PrimaryKeyBuilder pk3Builder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
pk3Builder.addPrimaryKeyColumn("pk", PrimaryKeyValue.fromString("pk3"));
// Specify the name of the data table.
RowUpdateChange rowUpdateChange = new RowUpdateChange("<TABLE_NAME>", pk3Builder.build());
// Add columns.
for (int i = 0; i < 10; i++) {
rowUpdateChange.put(new Column("Col" + i, ColumnValue.fromLong(i)));
}
// Remove a column.
rowUpdateChange.deleteColumns("Col10");
// Add rowUpdateChange to the code of the batch operation.
batchWriteRowRequest.addRowChange(rowUpdateChange);
// Construct rowDeleteChange.
PrimaryKeyBuilder pk4Builder = PrimaryKeyBuilder.createPrimaryKeyBuilder();
pk4Builder.addPrimaryKeyColumn("pk", PrimaryKeyValue.fromString("pk4"));
// Specify the name of the data table.
RowDeleteChange rowDeleteChange = new RowDeleteChange("<TABLE_NAME>", pk4Builder.build());
// Add rowDeleteChange to the code of the batch operation.
batchWriteRowRequest.addRowChange(rowDeleteChange);
BatchWriteRowResponse response = client.batchWriteRow(batchWriteRowRequest);
System.out.println("Whether all operations are successful:" + response.isAllSucceed());
if (!response.isAllSucceed()) {
for (BatchWriteRowResponse.RowResult rowResult : response.getFailedRows()) {
System.out.println("Failed rows:" + batchWriteRowRequest.getRowChange(rowResult.getTableName(), rowResult.getIndex()).getPrimaryKey());
System.out.println("Cause of failures:" + rowResult.getError());
}
/**
* You can use the createRequestForRetry method to construct another request to retry the operations on failed rows. Only the retry request is constructed here.
* We recommend that you use the custom retry policy in Tablestore SDKs as the retry method. This feature allows you to retry failed rows after batch operations. After you set the retry policy, you do not need to add retry code to call the operation.
*/
BatchWriteRowRequest retryRequest = batchWriteRowRequest.createRequestForRetry(response.getFailedRows());
}
}