過濾器
在服務(wù)端對(duì)讀取結(jié)果再進(jìn)行一次過濾,根據(jù)過濾器(Filter)中的條件決定返回的行。使用過濾器后,只返回符合條件的數(shù)據(jù)行。
前提條件
已初始化Client。具體操作,請(qǐng)參見初始化OTSClient。
已創(chuàng)建數(shù)據(jù)表并寫入數(shù)據(jù)。具體操作,請(qǐng)參見創(chuàng)建數(shù)據(jù)表和寫入數(shù)據(jù)。
使用方法
在通過GetRow、BatchGetRow或GetRange接口查詢數(shù)據(jù)時(shí),可以使用過濾器只返回符合條件的數(shù)據(jù)行。
過濾器目前包括RelationalCondition和CompositeCondition。
RelationalCondition:只判斷某個(gè)參考列的列值。
CompositeCondition:根據(jù)多個(gè)參考列的列值的判斷結(jié)果進(jìn)行邏輯組合,決定是否過濾某行。
關(guān)于過濾器的更多信息,請(qǐng)參見功能介紹中的過濾器。
限制
過濾器的條件支持關(guān)系運(yùn)算(=、!=、>、>=、<、<=)和邏輯運(yùn)算(NOT、AND、OR),最多支持10個(gè)條件的組合。
過濾器中的參考列必須在讀取的結(jié)果內(nèi)。如果指定的要讀取的列中不包含參考列,則過濾器無法獲取參考列的值。
在GetRow、BatchGetRow和GetRange接口中使用過濾器不會(huì)改變接口的原生語義和限制項(xiàng)。
使用GetRange接口時(shí),一次掃描數(shù)據(jù)的行數(shù)不能超過5000行或者數(shù)據(jù)大小不能超過4 MB。
當(dāng)在該次掃描的5000行或者4 MB數(shù)據(jù)中沒有滿足過濾器條件的數(shù)據(jù)時(shí),得到的Response中的Rows為空,但是nextStartPrimaryKey可能不為空,此時(shí)需要使用nextStartPrimaryKey繼續(xù)讀取數(shù)據(jù),直到nextStartPrimaryKey為空。
參數(shù)
參數(shù) | 說明 |
ColumnName | 過濾器中參考列的名稱。 |
ColumnValue | 過濾器中參考列的對(duì)比值。 |
ComparatorType | 過濾器中的關(guān)系運(yùn)算符,類型詳情請(qǐng)參見ComparatorType。 關(guān)系運(yùn)算符包括EQUAL(=)、NOT_EQUAL(!=)、GREATER_THAN(>)、GREATER_EQUAL(>=)、LESS_THAN(<)和LESS_EQUAL(<=)。 |
LogicOperator | 過濾器中的邏輯運(yùn)算符,類型詳情請(qǐng)參見LogicalOperator。 邏輯運(yùn)算符包括NOT、AND和OR。 |
PassIfMissing | 當(dāng)參考列在某行中不存在時(shí),是否返回該行。類型為bool值,默認(rèn)值為true,表示如果參考列在某行中不存在,則返回該行。 當(dāng)設(shè)置PassIfMissing為false時(shí),如果參考列在某行中不存在,則不返回該行。 |
LatestVersionsOnly | 當(dāng)參考列存在多個(gè)版本的數(shù)據(jù)時(shí),是否只使用最新版本的值做比較。類型為bool值,默認(rèn)值為true,表示如果參考列存在多個(gè)版本的數(shù)據(jù)時(shí),則只使用該列最新版本的值進(jìn)行比較。 當(dāng)設(shè)置LatestVersionsOnly為false時(shí),如果參考列存在多個(gè)版本的數(shù)據(jù)時(shí),則會(huì)使用該列的所有版本的值進(jìn)行比較,此時(shí)只要有一個(gè)版本的值滿足條件,就返回該行。 |
示例
使用RelationalCondition過濾數(shù)據(jù)
以下示例用于讀取數(shù)據(jù)表中數(shù)據(jù),根據(jù)col0列的值過濾數(shù)據(jù)。
public void GetRowWithRelationalCondition(OTSClient otsClient)
{
//定義行的主鍵,必須與創(chuàng)建表時(shí)的TableMeta中定義的一致。
PrimaryKey primaryKey = new PrimaryKey
{
{ "pk0", new ColumnValue(0) },
{ "pk1", new ColumnValue("abc") }
};
var rowQueryCriteria = new SingleRowQueryCriteria(TableName)
{
RowPrimaryKey = primaryKey
};
//只返回col0的值等于5的行。
var filter = new RelationalCondition("col0",CompareOperator.EQUAL,new ColumnValue(5))
{
PassIfMissing = true
};
rowQueryCriteria.Filter = filter.ToFilter();
rowQueryCriteria.AddColumnsToGet("col0");
rowQueryCriteria.AddColumnsToGet("col1");
GetRowRequest request = new GetRowRequest(rowQueryCriteria);
//查詢。
GetRowResponse response = otsClient.GetRow(request);
PrimaryKey primaryKeyRead = response.PrimaryKey;
AttributeColumns attributesRead = response.Attribute;
Console.WriteLine("Primary key read: ");
foreach (KeyValuePair<string, ColumnValue> entry in primaryKeyRead)
{
Console.WriteLine(entry.Key + ":" + PrintColumnValue(entry.Value));
}
Console.WriteLine("Attributes read: ");
foreach (KeyValuePair<string, ColumnValue> entry in attributesRead)
{
Console.WriteLine(entry.Key + ":" + PrintColumnValue(entry.Value));
}
Console.WriteLine("Get row with filter succeed.");
}
使用CompositeCondition過濾數(shù)據(jù)
以下示例用于當(dāng)滿足條件col0==0 OR col1=="ff"
時(shí)讀取數(shù)據(jù)表中數(shù)據(jù)。
public void GetRowWithCompositeCondition(OTSClient otsClient)
{
//定義行的主鍵,必須與創(chuàng)建表時(shí)的TableMeta中定義的一致。
PrimaryKey primaryKey = new PrimaryKey
{
{ "pk0", new ColumnValue(0) },
{ "pk1", new ColumnValue("abc") }
};
var rowQueryCriteria = new SingleRowQueryCriteria(TableName)
{
RowPrimaryKey = primaryKey
};
//只返回col0的值等于5的行或者col1不等于ff的行。
var filter1 = new RelationalCondition("col0",
CompareOperator.EQUAL,
new ColumnValue(5));
var filter2 = new RelationalCondition("col1", CompareOperator.NOT_EQUAL, new ColumnValue("ff"));
var filter = new CompositeCondition(LogicOperator.OR);
filter.AddCondition(filter1);
filter.AddCondition(filter2);
rowQueryCriteria.Filter = filter.ToFilter();
rowQueryCriteria.AddColumnsToGet("col0");
rowQueryCriteria.AddColumnsToGet("col1");
GetRowRequest request = new GetRowRequest(rowQueryCriteria);
//查詢。
GetRowResponse response = otsClient.GetRow(request);
PrimaryKey primaryKeyRead = response.PrimaryKey;
AttributeColumns attributesRead = response.Attribute;
Console.WriteLine("Primary key read: ");
foreach (KeyValuePair<string, ColumnValue> entry in primaryKeyRead)
{
Console.WriteLine(entry.Key + ":" + PrintColumnValue(entry.Value));
}
Console.WriteLine("Attributes read: ");
foreach (KeyValuePair<string, ColumnValue> entry in attributesRead)
{
Console.WriteLine(entry.Key + ":" + PrintColumnValue(entry.Value));
}
Console.WriteLine("Get row with filter succeed.");
}
相關(guān)文檔
當(dāng)某些應(yīng)用需要使用不同屬性作為查詢條件來執(zhí)行數(shù)據(jù)查詢時(shí),您可以通過將這些屬性作為二級(jí)索引的主鍵列實(shí)現(xiàn)按照屬性快速查詢數(shù)據(jù)的需求。更多信息,請(qǐng)參見二級(jí)索引。
當(dāng)日常業(yè)務(wù)中有非主鍵列查詢、多列組合查詢、模糊查詢等多維查詢需求以及求最值、統(tǒng)計(jì)行數(shù)、數(shù)據(jù)分組等數(shù)據(jù)分析需求時(shí),您可以將這些屬性作為多元索引中的字段并使用多元索引查詢與分析數(shù)據(jù)。 更多信息,請(qǐng)參見多元索引。
您還可以通過SQL查詢與分析表中數(shù)據(jù)。更多信息,請(qǐng)參見查詢數(shù)據(jù)。