Este tópico descreve como usar o Tablestore SDK for Java para consultar dados em uma tabela executando instruções SQL.
Observações de uso
O recurso de consulta SQL é compatível com o Tablestore SDK for Java V5.13.0 e versões posteriores. Ao usar esse recurso, verifique se uma versão compatível do Tablestore SDK for Java está instalada.
Pré-requisitos
Inicialize um cliente do Tablestore. Para mais informações, consulte Inicializar um cliente do Tablestore.
Crie uma tabela de mapeamento para a tabela na qual deseja consultar os dados. Para mais detalhes, consulte Criar uma tabela de mapeamento para uma tabela.
Método
public SQLQueryResponse sqlQuery(SQLQueryRequest request) throws TableStoreException, ClientException
Código de exemplo
O código de exemplo a seguir demonstra como executar a instrução SELECT para consultar dados na tabela test_table e retornar no máximo 10 linhas.
Antes de executar o código, substitua o nome da tabela e os nomes dos campos pelas informações reais.
public static void queryDataExample(SyncClient client) {
// Specify the SQL statement.
SQLQueryRequest request = new SQLQueryRequest("select order_id, user_id, sku_id, price, num, total_price, order_status, create_time, modified_time from test_table limit 10;");
SQLQueryResponse response = client.sqlQuery(request);
// Specify the RequestId information.
System.out.println("RequestId: " + response.getRequestId());
/*// Read throughput consumption information.
System.out.println("Read throughput consumption (by table):");
for(Map.Entry<String, ConsumedCapacity> entry : response.getConsumedCapacity().entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue().getCapacityUnit().getReadCapacityUnit() + "CU");
}*/
// Obtain the SQL query results.
SQLResultSet resultSet = response.getSQLResultSet();
// Return the schema information.
System.out.println("Schema: " + response.getSQLResultSet().getSQLTableMeta().getSchema());
// Data details
System.out.println("Data details:");
while (resultSet.hasNext()) {
SQLRow row = resultSet.next();
System.out.println(row.get("order_id") + ", "
+ row.get("user_id") + ", "
+ row.get("sku_id") + ", "
+ row.get("price") + ", "
+ row.get("num") + ", "
+ row.get("total_price") + ", "
+ row.get("order_status") + ", "
+ row.get("create_time") + ", "
+ row.get("modified_time"));
}
}