2

Can we take the compareOperator value based on input value? For example if my input is eq, then it should pick CompareFilter.CompareOp.EQUAL, else if input is ne it should pick CompareFilter.CompareOp.NOT_EQUAL.

Something like

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>)));

Any suggestions will be appreciated

Ram Ghadiyaram
  • 28,239
  • 13
  • 95
  • 121

1 Answers1

1

Based on above code , Yes it will work. Below is the example code snippet, you can have a look as well.where ConditionType is enum... like this you can also prepare your own enum(not mandatory if condition is also fine)

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;
}
Ram Ghadiyaram
  • 28,239
  • 13
  • 95
  • 121