写了一些hbase thrift C++客户端的简单测试,还有很多需要再学习。
相关的参考资料与链接:
http://blog.csdn.net/yangyangghy/article/details/39010365
http://blog.csdn.net/guxch/article/details/12163047
http://hbase.apache.org/book.html#thrift
http://stackoverflow.com/questions/11380897/how-to-use-method-scanneropenwithscan-and-filter-string-in-thrifts-c-to-a
http://hbase.apache.org/0.94/book/thrift.html
主要一些filter格式:
SingleColumnValueFilter('<family>', '<qualifier>', <compare operator>, '<comparator>', <filterIfColumnMissing_boolean>, <latest_version_boolean>)
RowFilter(<compare operator>,<comparator>)
-
KeyOnlyFilter
Description: This filter doesn’t take any arguments. It returns only the key component of each key-value.
Syntax: KeyOnlyFilter ()
Example: "KeyOnlyFilter ()"
-
FirstKeyOnlyFilter
Description: This filter doesn’t take any arguments. It returns only the first key-value from each row.
Syntax: FirstKeyOnlyFilter ()
Example: "FirstKeyOnlyFilter ()"
-
PrefixFilter
Description: This filter takes one argument – a prefix of a row key. It returns only those key-values present in a row that starts with the specified row prefix
Syntax: PrefixFilter (‘<row_prefix>’)
Example: "PrefixFilter (‘Row’)"
-
ColumnPrefixFilter
Description: This filter takes one argument – a column prefix. It returns only those key-values present in a column that starts with the specified column prefix. The column prefix must be of the form:
“qualifier”
Syntax:ColumnPrefixFilter(‘<column_prefix>’)
Example: "ColumnPrefixFilter(‘Col’)"
-
MultipleColumnPrefixFilter
Description: This filter takes a list of column prefixes. It returns key-values that are present in a column that starts with any of the specified column prefixes. Each of the column prefixes must be of the form:
“qualifier”
Syntax:MultipleColumnPrefixFilter(‘<column_prefix>’, ‘<column_prefix>’, …, ‘<column_prefix>’)
Example: "MultipleColumnPrefixFilter(‘Col1’, ‘Col2’)"
-
ColumnCountGetFilter
Description: This filter takes one argument – a limit. It returns the first limit number of columns in the table
Syntax: ColumnCountGetFilter (‘<limit>’)
Example: "ColumnCountGetFilter (4)"
-
PageFilter
Description: This filter takes one argument – a page size. It returns page size number of rows from the table.
Syntax: PageFilter (‘<page_size>’)
Example: "PageFilter (2)"
-
ColumnPaginationFilter
Description: This filter takes two arguments – a limit and offset. It returns limit number of columns after offset number of columns. It does this for all the rows
Syntax: ColumnPaginationFilter(‘<limit>’, ‘<offest>’)
Example: "ColumnPaginationFilter (3, 5)"
-
InclusiveStopFilter
Description: This filter takes one argument – a row key on which to stop scanning. It returns all key-values present in rows up to and including the specified row
Syntax: InclusiveStopFilter(‘<stop_row_key>’)
Example: "InclusiveStopFilter ('Row2')"
-
TimeStampsFilter
Description: This filter takes a list of timestamps. It returns those key-values whose timestamps matches any of the specified timestamps
Syntax: TimeStampsFilter (<timestamp>, <timestamp>, ... ,<timestamp>)
Example: "TimeStampsFilter (5985489, 48895495, 58489845945)"
-
RowFilter
Description: This filter takes a compare operator and a comparator. It compares each row key with the comparator using the compare operator and if the comparison returns true, it returns all the key-values in that row
Syntax: RowFilter (<compareOp>, ‘<row_comparator>’)
Example: "RowFilter (<=, ‘xyz)"
-
Family Filter
Description: This filter takes a compare operator and a comparator. It compares each qualifier name with the comparator using the compare operator and if the comparison returns true, it returns all the key-values in that column
Syntax: QualifierFilter (<compareOp>, ‘<qualifier_comparator>’)
Example: "QualifierFilter (=, ‘Column1’)"
-
QualifierFilter
Description: This filter takes a compare operator and a comparator. It compares each qualifier name with the comparator using the compare operator and if the comparison returns true, it returns all the key-values in that column
Syntax: QualifierFilter (<compareOp>,‘<qualifier_comparator>’)
Example: "QualifierFilter (=,‘Column1’)"
-
ValueFilter
Description: This filter takes a compare operator and a comparator. It compares each value with the comparator using the compare operator and if the comparison returns true, it returns that key-value
Syntax: ValueFilter (<compareOp>,‘<value_comparator>’)
Example: "ValueFilter (!=, ‘Value’)"
-
DependentColumnFilter
Description: This filter takes two arguments – a family and a qualifier. It tries to locate this column in each row and returns all key-values in that row that have the same timestamp. If the row doesn’t contain the specified column – none of the key-values in that row will be returned.
The filter can also take an optional boolean argument – dropDependentColumn. If set to true, the column we were depending on doesn’t get returned.
The filter can also take two more additional optional arguments – a compare operator and a value comparator, which are further checks in addition to the family and qualifier. If the dependent column is found, its value should also pass the value check and then only is its timestamp taken into consideration
Syntax: DependentColumnFilter (‘<family>’, ‘<qualifier>’, <boolean>, <compare operator>, ‘<value comparator’)
Syntax: DependentColumnFilter (‘<family>’, ‘<qualifier>’, <boolean>)
Syntax: DependentColumnFilter (‘<family>’, ‘<qualifier>’)
Example: "DependentColumnFilter (‘conf’, ‘blacklist’, false, >=, ‘zebra’)"
Example: "DependentColumnFilter (‘conf’, 'blacklist', true)"
Example: "DependentColumnFilter (‘conf’, 'blacklist')"
-
SingleColumnValueFilter
Description: This filter takes a column family, a qualifier, a compare operator and a comparator. If the specified column is not found – all the columns of that row will be emitted. If the column is found and the comparison with the comparator returns true, all the columns of the row will be emitted. If the condition fails, the row will not be emitted.
This filter also takes two additional optional boolean arguments – filterIfColumnMissing and setLatestVersionOnly
If the filterIfColumnMissing flag is set to true the columns of the row will not be emitted if the specified column to check is not found in the row. The default value is false.
If the setLatestVersionOnly flag is set to false, it will test previous versions (timestamps) too. The default value is true.
These flags are optional and if you must set neither or both
Syntax: SingleColumnValueFilter(<compare operator>, ‘<comparator>’, ‘<family>’, ‘<qualifier>’,<filterIfColumnMissing_boolean>, <latest_version_boolean>)
Syntax: SingleColumnValueFilter(<compare operator>, ‘<comparator>’, ‘<family>’, ‘<qualifier>)
Example: "SingleColumnValueFilter (<=, ‘abc’,‘FamilyA’, ‘Column1’, true, false)"
Example: "SingleColumnValueFilter (<=, ‘abc’,‘FamilyA’, ‘Column1’)"
-
SingleColumnValueExcludeFilter
Description: This filter takes the same arguments and behaves same as SingleColumnValueFilter – however, if the column is found and the condition passes, all the columns of the row will be emitted except for the tested column value.
Syntax: SingleColumnValueExcludeFilter(<compare operator>, '<comparator>', '<family>', '<qualifier>',<latest_version_boolean>, <filterIfColumnMissing_boolean>)
Syntax: SingleColumnValueExcludeFilter(<compare operator>, '<comparator>', '<family>', '<qualifier>')
Example: "SingleColumnValueExcludeFilter (‘<=’, ‘abc’,‘FamilyA’, ‘Column1’, ‘false’, ‘true’)"
Example: "SingleColumnValueExcludeFilter (‘<=’, ‘abc’, ‘FamilyA’, ‘Column1’)"
-
ColumnRangeFilter
Description: This filter is used for selecting only those keys with columns that are between minColumn and maxColumn. It also takes two boolean variables to indicate whether to include the minColumn and maxColumn or not.
If you don’t want to set the minColumn or the maxColumn – you can pass in an empty argument.
Syntax: ColumnRangeFilter (‘<minColumn>’, <minColumnInclusive_bool>, ‘<maxColumn>’, <maxColumnInclusive_bool>)
Example: "ColumnRangeFilter (‘abc’, true, ‘xyz’, false)"
/* 数据
hbase(main):010:0> scan 'test'
ROW COLUMN+CELL
row1 column=col1:, timestamp=1456372547448, value=1
row1 column=col1:qua, timestamp=1456802575635, value=testfilter
row2 column=col1:qua, timestamp=1456380851726, value=2
row3 column=col1:abc, timestamp=1456386394813, value=3
row4 column=col1:qua, timestamp=1456814453511, value=testfilterandmore
*/
以下是写的一些简单的测试程序:
void test_get()
{
try
{
hbasePool::getInstance()->init("192.168.0.3",9090,10);
tHBClient client(hbasePool::getInstance()->popClient());
if (client.isClientOpen())
{
TGet tget;
{
tget.row = "0003d05c-8bf5-4775-a323-9d19a26456ac-28d1ea37-deba-11e5-991a-ecb1d773614c";
TColumn col;
col.family = "businessInfo";
//col.qualifier = "result";
tget.columns.push_back(col);
}
TResult result;
client.getClient()->get(result, "friend_history", tget);
const int iCount = result.columnValues.size();
for(int i = 0; iCount > i; ++i)
{
std::cout << "qualifier :" << result.columnValues[i].qualifier << " ,value :" << result.columnValues[i].value
<<" ,timestamp :"<<result.columnValues[i].timestamp<< std::endl;
}
}
}
catch(apache::thrift::TApplicationException& x)
{
std::cout << " Hbase thrift fail : " << x.what();
}
catch(...)
{
std::cout << " Hbase thrift fail ...";
}
}
void test_put()
{
hbasePool::getInstance()->init("192.168.0.3",9090,10);
try
{
tHBClient client(hbasePool::getInstance()->popClient());
if (client.isClientOpen())
{
/*TPut tput;
tput.row = "justtest";
for (int i = 0;i<5;i++)
{
TColumnValue tcolumn;
tcolumn.family = "famly1";
tcolumn.qualifier = "qua"+boost::lexical_cast<std::string>(i);
tcolumn.value = boost::lexical_cast<std::string>(i);
tput.columnValues.push_back(tcolumn);
}
client.getClient()->put("test_zheng",tput);*/
TPut tput;
tput.row = "row4";
TColumnValue tcolum;
tcolum.family = "col1";
tcolum.qualifier = "qua";
tcolum.value = "testfilterandmore";
tput.columnValues.push_back(tcolum);
client.getClient()->put("test",tput);
}
}
catch(apache::thrift::TApplicationException& x)
{
std::cout << " Hbase thrift fail : " << x.what();
}
catch(...)
{
std::cout << " Hbase thrift fail ...";
}
}
void test_scan()
{
std::cout <<"enter test scan!"<<std::endl;
hbasePool::getInstance()->init("192.168.0.3",9090,10);
try
{
tHBClient client(hbasePool::getInstance()->popClient());
if (client.isClientOpen())
{
std::vector<TResult> tResult;
TScan tScan;
tScan.__set_startRow("row1");
tScan.__set_stopRow("row3");
tScan.__set_caching(100);
tScan.__set_batchSize(100);
TColumn tColumn;
tColumn.__set_family("col1");
//tColumn.__set_qualifier("abc");
std::vector<TColumn> v;
v.push_back(tColumn);
tScan.__set_columns(v);
client.getClient()->getScannerResults(tResult, "test", tScan, 1000);
std::cout<< "tResult.size = "<<tResult.size()<<std::endl;
for(auto ittResult = tResult.begin(); ittResult != tResult.end(); ++ittResult)
{
TResult result = *ittResult;
const int iCount = result.columnValues.size();
std::cout << "row key :"<< result.row<<" ,";
for(int i = 0; iCount > i; ++i)
{
std::cout << "qualifier :" << result.columnValues[i].qualifier << " ,value :" << result.columnValues[i].value
<<" ,timestamp :"<<result.columnValues[i].timestamp<< std::endl;
}
}
}
}
catch(apache::thrift::TApplicationException& x)
{
std::cout << " Hbase thrift fail : " << x.what();
}
catch(...)
{
std::cout << " Hbase thrift fail ...";
}
}
void test_scan_filter()
{
std::cout <<"enter test scan!"<<std::endl;
hbasePool::getInstance()->init("192.168.0.3",9090,10);
try
{
tHBClient client(hbasePool::getInstance()->popClient());
if (client.isClientOpen())
{
std::vector<TResult> tResult;
TScan tScan;
/* tScan.__set_startRow("row1");
tScan.__set_stopRow("row3");
tScan.__set_caching(100);
tScan.__set_batchSize(100);*/
std::string filterStr;
//SingleColumnValueFilter('<family>', '<qualifier>', <compare operator>, '<comparator>', <filterIfColumnMissing_boolean>, <latest_version_boolean>)
filterStr = "SingleColumnValueFilter('col1', 'qua', =, 'substring:testfilter', true, true)";
tScan.__set_filterString(filterStr);
/*TColumn tColumn;
tColumn.__set_family("col1");
//tColumn.__set_qualifier("abc");
std::vector<TColumn> v;
v.push_back(tColumn);
tScan.__set_columns(v); */
client.getClient()->getScannerResults(tResult, "test", tScan, 1000);
std::cout<< "tResult.size = "<<tResult.size()<<std::endl;
for(auto ittResult = tResult.begin(); ittResult != tResult.end(); ++ittResult)
{
TResult result = *ittResult;
const int iCount = result.columnValues.size();
std::cout << "row key :"<< result.row<<" ,";
for(int i = 0; iCount > i; ++i)
{
std::cout << "qualifier :" << result.columnValues[i].qualifier << " ,value :" << result.columnValues[i].value
<<" ,timestamp :"<<result.columnValues[i].timestamp<< std::endl;
}
}
}
}
catch(apache::thrift::TApplicationException& x)
{
std::cout << " Hbase thrift fail : " << x.what();
}
catch(...)
{
std::cout << " Hbase thrift fail ...";
}
}
//匹配正则表达式
void test_regex_filter()
{
std::cout <<"enter test scan!"<<std::endl;
hbasePool::getInstance()->init("192.168.0.3",9090,10);
try
{
tHBClient client(hbasePool::getInstance()->popClient());
if (client.isClientOpen())
{
std::vector<TResult> tResult;
TScan tScan;
std::string filterStr;
//SingleColumnValueFilter('<family>', '<qualifier>', <compare operator>, '<comparator>', <filterIfColumnMissing_boolean>, <latest_version_boolean>)
filterStr = "SingleColumnValueFilter('col1', 'qua', =, 'regexstring:tes(.*)er', true, false)";
tScan.__set_filterString(filterStr);
client.getClient()->getScannerResults(tResult, "test", tScan, 1000);
std::cout<< "tResult.size = "<<tResult.size()<<std::endl;
for(auto ittResult = tResult.begin(); ittResult != tResult.end(); ++ittResult)
{
TResult result = *ittResult;
const int iCount = result.columnValues.size();
std::cout << "row key :"<< result.row<<" ,";
for(int i = 0; iCount > i; ++i)
{
std::cout << "qualifier :" << result.columnValues[i].qualifier << " ,value :" << result.columnValues[i].value
<<" ,timestamp :"<<result.columnValues[i].timestamp<< std::endl;
}
}
}
}
catch(apache::thrift::TApplicationException& x)
{
std::cout << " Hbase thrift fail : " << x.what();
}
catch(...)
{
std::cout << " Hbase thrift fail ...";
}
}
//
void test_complex_filter()
{
std::cout <<"enter test scan!"<<std::endl;
hbasePool::getInstance()->init("192.168.0.3",9090,10);
try
{
tHBClient client(hbasePool::getInstance()->popClient());
if (client.isClientOpen())
{
std::vector<TResult> tResult;
TScan tScan;
std::string filterStr,filterStr1,filterStr2;
//SingleColumnValueFilter('<family>', '<qualifier>', <compare operator>, '<comparator>', <filterIfColumnMissing_boolean>, <latest_version_boolean>)
filterStr1 = "SingleColumnValueFilter('col1', 'qua', =, 'regexstring:tes(.*)er', true, false)";
filterStr2 = "SingleColumnValueFilter('col1', 'qua', =, 'substring:andmore', true, false)";
filterStr = filterStr1 + " AND " + filterStr2;
tScan.__set_filterString(filterStr);
client.getClient()->getScannerResults(tResult, "test", tScan, 1000);
std::cout<< "tResult.size = "<<tResult.size()<<std::endl;
for(auto ittResult = tResult.begin(); ittResult != tResult.end(); ++ittResult)
{
TResult result = *ittResult;
const int iCount = result.columnValues.size();
std::cout << "row key :"<< result.row<<" ,";
for(int i = 0; iCount > i; ++i)
{
std::cout << "qualifier :" << result.columnValues[i].qualifier << " ,value :" << result.columnValues[i].value
<<" ,timestamp :"<<result.columnValues[i].timestamp<< std::endl;
}
}
}
}
catch(apache::thrift::TApplicationException& x)
{
std::cout << " Hbase thrift fail : " << x.what();
}
catch(...)
{
std::cout << " Hbase thrift fail ...";
}
}
//不同的filter
void test_diff_filter()
{
std::cout <<"enter test scan!"<<std::endl;
hbasePool::getInstance()->init("192.168.0.3",9090,10);
try
{
tHBClient client(hbasePool::getInstance()->popClient());
if (client.isClientOpen())
{
std::vector<TResult> tResult;
TScan tScan;
std::string filterStr,filterStr1,filterStr2;
//SingleColumnValueFilter('<family>', '<qualifier>', <compare operator>, '<comparator>', <filterIfColumnMissing_boolean>, <latest_version_boolean>)
filterStr1 = "SingleColumnValueFilter('col1', 'qua', =, 'regexstring:tes(.*)er', true, false)";
//RowFilter(<compare operator>,<comparator>)
filterStr2 = "RowFilter(=, 'substring:w4')";
filterStr = filterStr1 + " AND " + filterStr2;
tScan.__set_filterString(filterStr);
client.getClient()->getScannerResults(tResult, "test", tScan, 1000);
std::cout<< "tResult.size = "<<tResult.size()<<std::endl;
for(auto ittResult = tResult.begin(); ittResult != tResult.end(); ++ittResult)
{
TResult result = *ittResult;
const int iCount = result.columnValues.size();
std::cout << "row key :"<< result.row<<" ,";
for(int i = 0; iCount > i; ++i)
{
std::cout << "qualifier :" << result.columnValues[i].qualifier << " ,value :" << result.columnValues[i].value
<<" ,timestamp :"<<result.columnValues[i].timestamp<< std::endl;
}
}
}
}
catch(apache::thrift::TApplicationException& x)
{
std::cout << " Hbase thrift fail : " << x.what();
}
catch(...)
{
std::cout << " Hbase thrift fail ...";
}
}
int main()
{
//test_get();
//test_put();
//test_scan();
//test_scan_filter();
//test_regex_filter();
//test_complex_filter();
test_diff_filter();
return 0;
}