1001
微信
多行數據操作__Java-SDK_SDK 參考_表格存儲-阿裏雲
表格存儲的 SDK 提供了 BatchGetRow、BatchWriteRow、GetRange 和 GetByIterator 等多行操作的接口。
批量讀(BatchGetRow)
批量讀取一個或多個表中的若幹行數據。
BatchGetRow 操作可視為多個 GetRow 操作的集合,各個操作獨立執行,獨立返回結果,獨立計算服務能力單元。
與執行大量的 GetRow 操作相比,使用 BatchGetRow 操作可以有效減少請求的響應時間,提高數據的讀取速率。
接口
/**
* 從多張表(Table)中獲取多行數據。
*
* @param batchGetRowRequest 執行BatchGetRow操作所需參數的封裝。
* @return BatchGetRow操作的響應內容。
* @throws OTSException OTS訪問返回錯誤消息
* @throws ClientException 請求的返回結果無效, 或由於網絡原因請求失敗, 或訪問超時。
*/
public BatchGetRowResult batchGetRow(BatchGetRowRequest batchGetRowRequest)
throws OTSException, ClientException;
示例
批量一次讀 10 行。
try
{
// 構造BatchGetRow對象和參數
BatchGetRowRequest request = new BatchGetRowRequest();
MultiRowQueryCriteria tableRows = new MultiRowQueryCriteria(tableName);
for (int j = 0; j < 10; ++j) {
RowPrimaryKey primaryKeys = new RowPrimaryKey();
primaryKeys.addPrimaryKeyColumn("pk0", PrimaryKeyValue.fromLong(j));
primaryKeys.addPrimaryKeyColumn("pk1", PrimaryKeyValue.fromLong(j)));
tableRows.addRow(primaryKeys);
}
// 隻返回col0和col1列
tableRows.addColumnsToGet(new String[] {"col0", "col1"});
request.addMultiRowQueryCriteria(tableRows);
// 調用batchGetRow接口
BatchGetRowResult result = client.batchGetRow(request);
// 統計是否有某些行執行失敗,如果有,則重試
BatchGetRowRequest failedOperations = null;
List<Row> succeedRows = new ArrayList<Row>();
int retryCount = 0;
do {
failedOperations = new BatchGetRowRequest();
Map<String, List<BatchGetRowResult.RowStatus>> status = result.getTableToRowsStatus();
for (Entry<String, List<BatchGetRowResult.RowStatus>> entry : status.entrySet()) {
tableName = entry.getKey();
tableRows = new MultiRowQueryCriteria(tableName);
List<BatchGetRowResult.RowStatus> statuses = entry.getValue();
for (int index = 0; index < statuses.size(); index++) {
BatchGetRowResult.RowStatus rowStatus = statuses.get(index);
if (!rowStatus.isSucceed()) {
// 操作失敗, 需要放到重試列表中再次重試
// 需要重試的操作可以從request中獲取參數
tableRows.addRow(request.getPrimaryKey(tableName, index));
} else {
succeedRows.add(rowStatus.getRow());
System.out.println("Read row:");
System.out.println("pk0:" + rowStatus.getRow().getColumns().get("pk0"));
System.out.println("pk1:" + rowStatus.getRow().getColumns().get("pk1"));
System.out.println("col0:" + rowStatus.getRow().getColumns().get("col0"));
}
}
if (!tableRows.getRowKeys().isEmpty()) {
tableRows.addColumnsToGet(new String[] {"col0", "col1"});
failedOperations.addMultiRowQueryCriteria(tableRows);
}
}
if (failedOperations.isEmpty() || ++retryCount > 3) {
// 如果所有操作都成功了或者重試次數達到上線, 則不再需要重試。
break;
}
// 如果有需要重試的操作, 則稍微等待一會後再次重試, 否則繼續出錯的概率很高。
try {
// 100ms後繼續重試
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
request = failedOperations;
result = client.batchGetRow(request);
} while (true);
System.out.println(String.format("Get row finish, succeeded count:%d", succeedRows.size()));
} catch (ClientException ex) {
System.out.println("Batch get row failed.");
} catch (OTSException ex) {
System.out.println("Batch get row failed.");
}
提示:
批量讀也支持通過條件語句過濾。
詳細代碼:batchGetRow@GitHub。
批量寫(BatchWriteRow)
批量插入、修改或刪除一個或多個表中的若幹行數據。
BatchWriteRow 操作可視為多個 PutRow、UpdateRow、DeleteRow 操作的集合,各個操作獨立執行,獨立返回結果,獨立計算服務能力單元。
接口
/**
* 對多張表(Table)中的多行數據進行增加、刪除或者更改操作。
*
* @param batchWriteRowRequest 執行BatchWriteRow操作所需參數的封裝。
* @return BatchWriteRow操作的響應內容。
* @throws OTSException OTS訪問返回錯誤消息
* @throws ClientException 請求的返回結果無效, 或由於網絡原因請求失敗, 或訪問超時。
*/
public BatchWriteRowResult batchWriteRow(
BatchWriteRowRequest batchWriteRowRequest) throws OTSException,
ClientException;
示例
批量導入 30 行數據。
try
{
BatchWriteRowRequest request = new BatchWriteRowRequest();
// 向3張表中插入數據, 每張表插入10行。
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 10; ++j) {
RowPutChange rowPutChange = new RowPutChange("SampleTable" + i);
RowPrimaryKey primaryKey = new RowPrimaryKey();
primaryKey.addPrimaryKeyColumn("pk0", PrimaryKeyValue.fromLong(i));
primaryKey.addPrimaryKeyColumn("pk1", PrimaryKeyValue.fromLong(j));
rowPutChange.setPrimaryKey(primaryKey);
rowPutChange.addAttributeColumn("col0", ColumnValue.fromLong(4));
rowPutChange.addAttributeColumn("col2", ColumnValue.fromString("成杭京"));
request.addRowPutChange(rowPutChange);
}
}
// batchWriteRow接口會返回一個結果集, 結果集中包含的結果個數與插入的行數相同。 結果集中的結果不一定都是成功,用戶需要自己對不成功的操作進行重試。
BatchWriteRowResult result = client.batchWriteRow(request);
// 重試邏輯類似於上麵的batchGetRow示例中的邏輯,這裏省略
} catch (ClientException ex) {
System.out.println("Batch write row failed.");
} catch (OTSException ex) {
System.out.println("Batch write row failed.");
}
提示:
批量寫也支持條件語句。
詳細代碼:batchWriteRow@GitHub。
範圍讀(GetRange)
讀取指定主鍵範圍內的數據。
接口
/**
* 從表(Table)中查詢一個範圍內的多行數據。
*
* @param getRangeRequest 執行GetRange操作所需參數的封裝。
* @return GetRange操作的響應內容。
* @throws OTSException OTS訪問返回錯誤消息
* @throws ClientException 請求的返回結果無效, 或由於網絡原因請求失敗, 或訪問超時。
*/
public GetRangeResult getRange(GetRangeRequest getRangeRequest)
throws OTSException, ClientException;
示例
按照範圍讀取。
try
{
// 這裏查找uid從1-4(左閉右開)的數據
RangeRowQueryCriteria criteria = new RangeRowQueryCriteria("SampleTable");
RowPrimaryKey inclusiveStartKey = new RowPrimaryKey();
inclusiveStartKey.addPrimaryKeyColumn("pk0", PrimaryKeyValue.fromLong(1));
// 範圍的邊界需要提供完整的PK,若查詢的範圍不涉及到某一列值的範圍,則需要將該列設置為無窮大或者無窮小
inclusiveStartKey.addPrimaryKeyColumn("pk1", PrimaryKeyValue.INF_MIN);
RowPrimaryKey exclusiveEndKey = new RowPrimaryKey();
exclusiveEndKey.addPrimaryKeyColumn("pk0", PrimaryKeyValue.fromLong(4));
// 範圍的邊界需要提供完整的PK,若查詢的範圍不涉及到某一列值的範圍,則需要將該列設置為無窮大或者無窮小
exclusiveEndKey.addPrimaryKeyColumn("pk1", PrimaryKeyValue.INF_MAX);
criteria.setInclusiveStartPrimaryKey(inclusiveStartKey);
criteria.setExclusiveEndPrimaryKey(exclusiveEndKey);
// 前向查詢
criteria.setDirection(Direction.FORWARD);
// 最多返回10行
criteria.setLimit(10);
// 構造RangeRequest對象
GetRangeRequest request = new GetRangeRequest();
request.setRangeRowQueryCriteria(criteria);
// 調用getRange接口
GetRangeResult result = client.getRange(request);
// 輸出數據
List<Row> rows = result.getRows();
for (Row row : rows) {
System.out.println("col0:" + row.getColumns().get("col0"));
System.out.println("col1:" + row.getColumns().get("col1"));
System.out.println("col2:" + row.getColumns().get("col2"));
}
int consumedReadCU = result.getConsumedCapacity().getCapacityUnit().getReadCapacityUnit();
System.out.println("consumed capacityunit:" + consumedReadCU);
System.out.println("Get range succeeded");
} catch (ClientException ex) {
System.out.println("Get range failed.");
} catch (OTSException ex) {
System.out.println("Get range failed.");
}
提示:
按範圍讀也支持通過條件語句過濾。
詳細代碼:GetRange@GitHub。
迭代讀(GetByIterator)
迭代讀取數據。
接口
/**
* 從表(Table)中迭代讀取滿足條件的數據
* @param rangeIteratorParameter
* 迭代讀時的參數,包括開始,結束位置
* @return 迭代器
* @throws OTSException
* OTS訪問返回錯誤消息
* @throws ClientException
* 請求的返回結果無效, 或由於網絡原因請求失敗, 或訪問超時。
*/
public Iterator<Row> createRangeIterator(
RangeIteratorParameter rangeIteratorParameter)
throws OTSException, ClientException;
示例
迭代讀取表 SampleTable 中的數據。
// 構造迭代讀取的起始位置
RowPrimaryKey inclusiveStartKey = new RowPrimaryKey();
inclusiveStartKey.addPrimaryKeyColumn(COLUMN_GID_NAME,
PrimaryKeyValue.fromLong(1));
inclusiveStartKey.addPrimaryKeyColumn(COLUMN_UID_NAME,
PrimaryKeyValue.INF_MIN);
// 構造迭代讀取的結束位置
RowPrimaryKey exclusiveEndKey = new RowPrimaryKey();
exclusiveEndKey.addPrimaryKeyColumn(COLUMN_GID_NAME,
PrimaryKeyValue.fromLong(4));
exclusiveEndKey.addPrimaryKeyColumn(COLUMN_UID_NAME,
PrimaryKeyValue.INF_MAX);
// 構造迭代讀取的參數對象,並設置起始和結束的位置,包括起始位置的行,不包括結束位置的行
RangeIteratorParameter rangeIteratorParameter = new RangeIteratorParameter(tableName);
rangeIteratorParameter.setInclusiveStartPrimaryKey(inclusiveStartKey);
rangeIteratorParameter.setExclusiveEndPrimaryKey(exclusiveEndKey);
// 創建出迭代器,並迭代獲取每一行數據
try {
Iterator<Row> iterator = client.createRangeIterator(rangeIteratorParameter);
while (iterator.hasNext()) {
Row row = iterator.next();
System.out.println("name:" + row.getColumns().get(COLUMN_NAME_NAME));
System.out.println("address" + row.getColumns().get(COLUMN_ADDRESS_NAME));
}
System.out.println("Iterator get succeeded.");
} catch (ClientException ex) {
System.out.println("Iterator get failed.");
} catch (OTSException ex) {
System.out.println("Iterator get failed.");
}
提示:
- 詳細代碼:GetByIterator@GitHub。
最後更新:2016-11-23 16:04:09
上一篇:
單行數據操作__Java-SDK_SDK 參考_表格存儲-阿裏雲
下一篇:
條件更新__Java-SDK (NEW)_SDK 參考_表格存儲-阿裏雲
購買雲虛擬主機__產品購買_購買指導_雲虛機主機-阿裏雲
SetAccountAlias__安全設置接口_RAM API文檔_訪問控製-阿裏雲
申請流程__快速入門_雲數據庫 PetaData-阿裏雲
金融雲推薦架構__使用金融雲產品_金融雲-阿裏雲
C__客戶端連接實例_快速入門_雲數據庫 Memcache 版-阿裏雲
查詢數據庫的RDS列表__數據庫管理_開放API_分布式關係型數據庫 DRDS-阿裏雲
欠費狀態下的API行為__附錄_API 參考_雲服務器 ECS-阿裏雲
BandwidthPackageMonitorDataItemType__數據類型_API參考_專有網絡 VPC-阿裏雲
SLBSubDomainType__數據類型_API文檔_雲解析-阿裏雲
DDL語法__用戶指南_雲數據庫 PetaData-阿裏雲
相關內容
常見錯誤說明__附錄_大數據計算服務-阿裏雲
發送短信接口__API使用手冊_短信服務-阿裏雲
接口文檔__Android_安全組件教程_移動安全-阿裏雲
運營商錯誤碼(聯通)__常見問題_短信服務-阿裏雲
設置短信模板__使用手冊_短信服務-阿裏雲
OSS 權限問題及排查__常見錯誤及排除_最佳實踐_對象存儲 OSS-阿裏雲
消息通知__操作指南_批量計算-阿裏雲
設備端快速接入(MQTT)__快速開始_阿裏雲物聯網套件-阿裏雲
查詢API調用流量數據__API管理相關接口_API_API 網關-阿裏雲
使用STS訪問__JavaScript-SDK_SDK 參考_對象存儲 OSS-阿裏雲