Tablestore では、フィルターを使用して、指定した条件に基づいてサーバー側でクエリ結果を絞り込むことができます。このトピックでは、.NET SDK でフィルターを使用する方法について説明します。
前提条件
フィルターの種類
Tablestore では、次の 2 種類のフィルターが提供されています。
関係条件:単一の属性列の値が指定された条件を満たすかどうかを判定します。
複合条件:複数の条件を組み合わせてデータをフィルタリングします。
関係条件
public class RelationalCondition : IColumnCondition例
この例では、プライマリキーが [row1, row3) の範囲にある行データに対して範囲クエリを実行します。次に、フィルターを使用して、col1 属性列が val1 である行のみを返します。
try
{
// クエリの開始プライマリキーを設定します。
PrimaryKey inclusiveStartPrimaryKey = new PrimaryKey()
{
{ "id", new ColumnValue("row1") }
};
// クエリの終了プライマリキーを設定します。終了キーは範囲に含まれません。
PrimaryKey exclusiveEndPrimaryKey = new PrimaryKey()
{
{ "id", new ColumnValue("row3") }
};
// 関係条件 (col1 == "val1") を作成します。
RelationalCondition relationalCondition = new RelationalCondition("col1", CompareOperator.EQUAL, new ColumnValue("val1"));
// GetRange メソッドを呼び出して行データを読み取ります。
GetRangeRequest getRangeRequest = new GetRangeRequest("test_table", GetRangeDirection.Forward, inclusiveStartPrimaryKey, exclusiveEndPrimaryKey, null, null, relationalCondition);
GetRangeResponse getRangeResponse = client.GetRange(getRangeRequest);
// レスポンスを処理します。
Console.WriteLine($"RequestId: {getRangeResponse.RequestID}");
Console.WriteLine($"Read CU Cost: {getRangeResponse.ConsumedCapacityUnit.Read}");
Console.WriteLine($"Write CU Cost: {getRangeResponse.ConsumedCapacityUnit.Write}");
Console.WriteLine("Row Data: ");
foreach (Row row in getRangeResponse.RowDataList)
{
Console.WriteLine(row);
}
}
catch (Exception ex)
{
Console.WriteLine($"Get Range failed, exception: {ex.Message}");
}指定した属性列が存在しない行を除外するには、
PassIfMissingをfalseに設定します。relationalCondition.PassIfMissing = false;
複合条件
最大 32 個の条件を組み合わせることができます。
public class CompositeCondition : IColumnCondition例
この例では、プライマリキーが [row1, row3) の範囲にある行データに対して範囲クエリを実行する方法を示します。次に、複合条件 を使用して結果をフィルタリングします。
try
{
// クエリの開始プライマリキーを設定します。
PrimaryKey inclusiveStartPrimaryKey = new PrimaryKey()
{
{ "id", new ColumnValue("row1") }
};
// クエリの終了プライマリキーを設定します。終了キーは範囲に含まれません。
PrimaryKey exclusiveEndPrimaryKey = new PrimaryKey()
{
{ "id", new ColumnValue("row3") }
};
// 関係条件 1 を作成します:col1 == "val1"
RelationalCondition relationalCondition1 = new RelationalCondition("col1", CompareOperator.EQUAL, new ColumnValue("val1"));
// 関係条件 2 を作成します:col2 == "val2"
RelationalCondition relationalCondition2 = new RelationalCondition("col2", CompareOperator.EQUAL, new ColumnValue("val2"));
// 複合条件 1 を作成します:col1 == "val1" OR col2 == "val2"
CompositeCondition compositeCondition1 = new CompositeCondition(LogicOperator.OR);
compositeCondition1.AddCondition(relationalCondition1);
compositeCondition1.AddCondition(relationalCondition2);
// 関係条件 3 を作成します:col3 == "val3"
RelationalCondition relationalCondition3 = new RelationalCondition("col3", CompareOperator.EQUAL, new ColumnValue("val3"));
// 複合条件 2 を作成し、前の条件を組み合わせます:(col1 == "val1" OR col2 == "val2") AND col3 == "val3"
CompositeCondition compositeCondition2 = new CompositeCondition(LogicOperator.AND);
compositeCondition2.AddCondition(compositeCondition1);
compositeCondition2.AddCondition(relationalCondition3);
// GetRange メソッドを呼び出して行データを読み取ります。
GetRangeRequest getRangeRequest = new GetRangeRequest("test_table", GetRangeDirection.Forward, inclusiveStartPrimaryKey, exclusiveEndPrimaryKey, null, null, compositeCondition2);
GetRangeResponse getRangeResponse = client.GetRange(getRangeRequest);
// レスポンスを処理します。
Console.WriteLine($"RequestId: {getRangeResponse.RequestID}");
Console.WriteLine($"Read CU Cost: {getRangeResponse.ConsumedCapacityUnit.Read}");
Console.WriteLine($"Write CU Cost: {getRangeResponse.ConsumedCapacityUnit.Write}");
Console.WriteLine("Row Data: ");
foreach (Row row in getRangeResponse.RowDataList)
{
Console.WriteLine(row);
}
}
catch (Exception ex)
{
Console.WriteLine($"Get Range failed, exception: {ex.Message}");
}