Este tópico descreve como usar o Tablestore SDK for .NET para consultar dados ao executar instruções SQL.
Notas de uso
O Tablestore SDK for .NET V5.0.0 e versões posteriores oferecem suporte a consultas SQL. Certifique-se de que uma versão compatível do SDK esteja instalada.
Para obter mais informações, consulte Version history of Tablestore SDK for .NET .
Pré-requisitos
Inicialize um cliente. Para mais detalhes, consulte Initialize a Tablestore client.
Crie uma tabela de mapeamento para a tabela na qual você deseja consultar dados. Para obter mais informações, consulte crie uma tabela de mapeamento.
Exemplo
O exemplo a seguir mostra como usar a instrução select pk0,pk1,col0,col1,date_col,geo_col from test_table limit 20 para consultar dados na tabela test_table e retornar no máximo 20 linhas de dados. O sistema retorna o tipo de solicitação da instrução de consulta, o esquema do valor de retorno e os resultados obtidos.
/// <summary>
/// Query data.
/// </summary>
/// <param name="otsClient"></param>
public static void QueryData(OTSClient otsClient)
{
SQLQueryRequest sqlQueryRequest = new SQLQueryRequest("select pk0,pk1,col0,col1,date_col,geo_col from test_table limit 20");
SQLQueryResponse sqlQueryResponse = otsClient.SQLQuery(sqlQueryRequest);
SQLTableMeta sqlTableMeta = sqlQueryResponse.GetSQLResultSet().GetSQLTableMeta();
Console.WriteLine(JsonConvert.SerializeObject(sqlTableMeta.GetSchema()));
ISQLResultSet resultSet = sqlQueryResponse.GetSQLResultSet();
while (resultSet.HasNext())
{
ISQLRow row = resultSet.Next();
Console.WriteLine(row.GetString("pk0") + " , " + row.GetLong("pk1") + " , " + row.GetString("col0") + " , " +
row.GetLong("col1") + " , " + row.GetString("date_col") + " , " + row.GetString("geo_col"));
}
}