假設每個值之間以空白間隔
String qryKeywords = "竹筍 豆皮 好吃 開心";
Condition 1:
查詢條件之間以"和"關係存在( The terms in a query are linked together with AND)
ArrayList<Filter> filtersArrayList = new ArrayList<Filter>();
Filter filter = null;
String [] qryKeywordsList =qryKeywords.split("\\s+");
for (int i=0; i< qryKeywordsList.length; i++){
if (!qryKeywordsList[i].equals("")){
filter= new ValueFilter(CompareFilter.CompareOp.EQUAL,new SubstringComparator(qryKeywordsList[i]));
filtersArrayList.add(filter);
}
}
Scan s = new Scan();
s.setFilter(filtersArrayList);
ResultScanner rs = table.getScanner(s);
while ((r = rs.next()) != null) {
for (KeyValue kv : r.list())
System.out.println( "row : " + new String(kv.getRow()));
System.out.println( "column : " + new String(kv.getColumn()));
System.out.println( "value : " + new String(kv.getValue()));
}
}
Condition 2:
查詢條件之間以"或"關係存在(The terms in a query are linked together with OR)
==>利用RegexStringComparator
ArrayList<Filter> filtersArrayList = new ArrayList<Filter>();
Filter filter = null;
String qryKeywordsReg =qryKeywords.replaceAll("\\s+", "|");
filter= new ValueFilter(CompareFilter.CompareOp.EQUAL,new RegexStringComparator(qryKeywordsReg));
filtersArrayList.add(filter);
Scan s = new Scan();
s.setFilter(filtersArrayList);
ResultScanner rs = table.getScanner(s);
while ((r = rs.next()) != null) {
for (KeyValue kv : r.list())
System.out.println( "row key : " + new String(kv.getRow()));
System.out.println( "column : " + new String(kv.getColumn()));
System.out.println( "value : " + new String(kv.getValue()));
}
}