hbase:根据输入值分配CompareFilter.CompareOp

时间:2016-06-09 21:34:58

标签: java hbase

我们可以根据输入值获取compareOperator值吗?例如,如果我的输入是 eq ,那么它应该选择CompareFilter.CompareOp.EQUAL,否则如果输入是 ne ,它应该选择CompareFilter.CompareOp.NOT_EQUAL

这样的东西
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 个答案:

答案 0 :(得分:1)

根据以上代码,是的,它会起作用。下面是示例代码片段,您也可以查看。其中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;
}