2016-06-09 77 views
2

我們可以根據輸入值來計算compareOperator值嗎?例如,如果我的輸入是 eq,那麼它應該選擇CompareFilter.CompareOp.EQUAL,否則如果輸入是 ne它應該選擇CompareFilter.CompareOp.NOT_EQUALhbase:根據輸入值賦值CompareFilter.CompareOp

喜歡的東西

if(input.equals("eq")) { 
    myCompareOp = EQUAL; 
}else if(input.equals("ne")) { 
    myCompareOp = NOT_EQUAL; 
} 

SingleColumnValueFilter colValFilter = new SingleColumnValueFilter(Bytes.toBytes(<CF>), Bytes.toBytes(<CQ>) 
       , myCompareOp, new BinaryComparator(Bytes.toBytes(<value>))); 

任何建議,可以理解

回答

1

基於上面的代碼,是的,它會奏效。下面是示例代碼片段,你可以看看作爲well.where ConditionType是枚舉...這樣你也可以準備自己的枚舉(不是強制性的,如果條件也很好)

private Filter buildSimpleQuery(ConditionType ct, String name, Object value) { 
    name = super.getRealPropertyName(name); 
    validatePropertyValue(name, value); 
    Class<?> clazz = getPrimitiveFieldClass(name, value.getClass()); 
    CompareOp compareOp = null; 
    boolean regexCompRequired = false; 
    switch (ct) { 
    case EQUALS: 
     compareOp = CompareOp.EQUAL; 
     regexCompRequired = String.class == clazz && value.toString().endsWith("*"); 
     break; 
    case NOT_EQUALS: 
     compareOp = CompareOp.NOT_EQUAL; 
     regexCompRequired = String.class == clazz && value.toString().endsWith("*"); 
     break; 
    case GREATER_THAN: 
     compareOp = CompareOp.GREATER; 
     break; 
    case GREATER_OR_EQUALS: 
     compareOp = CompareOp.GREATER_OR_EQUAL; 
     break; 
    case LESS_THAN: 
     compareOp = CompareOp.LESS; 
     break; 
    case LESS_OR_EQUALS: 
     compareOp = CompareOp.LESS_OR_EQUAL; 
     break; 
    default: 
     break; 
    } 
    String qualifier = name; 
    String theFamily = family != null ? family : familyMap.get(qualifier); 
    ByteArrayComparable byteArrayComparable = regexCompRequired 
     ? new RegexStringComparator(value.toString().replace("*", ".")) 
     : new BinaryComparator(value.toString().getBytes(StandardCharsets.UTF_8)); 

    Filter query = new SingleColumnValueFilter(theFamily.getBytes(StandardCharsets.UTF_8), 
               qualifier.getBytes(StandardCharsets.UTF_8), 
               compareOp, 
               byteArrayComparable); 
    return query; 
} 
+0

它的工作原理。謝謝:) –

+0

因爲它的工作原理,你很好,請標記爲「所有者接受」謝謝 –