You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@hbase.apache.org by Chaitanya <ch...@gmail.com> on 2014/03/19 12:19:43 UTC

Filters failing to compare negative numbers (int,float,double or long)

Hi All,

I have come across an issue while using filters to get a result.

For eg.
I have created a table and its specifications are as follows :

table name --> test
column family --> cf
row keys --> rowKey1 - rowKey10 (10 different row keys)
column qualifier --> integerData

For different rowkeys, the qualifier 'integerData' contains either positive
or negative integer values (data loaded randomly).

Now, while I am trying to retrieve the data from the table based on a filter
condition, its failing to give the desired result.

For eg. say,
My table contains following data :
[-50,-40,-30,-20,-10,10,20,30,40,50]

I want to get only those values which are greater than or equal to 40.
Following is the code for the filter set on scan :

********************************************
  Scan scan = new Scan();
  scan.addColumn(Bytes.toBytes("cf"), Bytes.toBytes(" integerData"));
		
  int i = 40;
  Filter filter = new ValueFilter(CompareOp.GREATER_OR_EQUAL,new
BinaryComparator(Bytes.toBytes(i)));

  scan.setFilter(filter);
*********************************************

The result should be : 40 and 50
BUT, the actual result is : -50, -40, -30, -20, -10, 40, 50

I have read few posts which addressed this issue, and few people provided
the solution as:

1) write a custom comparator, as BinaryComparator is not meant for number
comparison
OR
2) retrieve all the values as integer and then compare 

BUT, I want to know if there is any other way to achieve this.
Because this seems to be a very basic need, i.e. comparing numbers, and I
feel HBase should have something straight forward to deal with this.
This comparison fails only when the negative numbers are involved.

I am not able to get the right way to do it.

Can anyone help me on this ?



-----
Regards,
Chaitanya
--
View this message in context: http://apache-hbase.679495.n3.nabble.com/Filters-failing-to-compare-negative-numbers-int-float-double-or-long-tp4057268.html
Sent from the HBase User mailing list archive at Nabble.com.

Re: Filters failing to compare negative numbers (int,float,double or long)

Posted by haosdent <ha...@gmail.com>.
Because the first bit of positive number is 0 while the first bit of
negative number is 1. Negative number is greater than positive number
according lexicographical order.


On Wed, Mar 19, 2014 at 7:37 PM, haosdent <ha...@gmail.com> wrote:

> For your scenario, I thinks this code snippet should work for you:
>
> *********************************************
>     int i = 40;
>     FilterList filters = new FilterList();
>     ValueFilter filter = new
> ValueFilter(CompareFilter.CompareOp.GREATER_OR_EQUAL,new
>             BinaryComparator(Bytes.toBytes(i)));
>     filters.addFilter(filter);
>     filter = new ValueFilter(CompareFilter.CompareOp.LESS,new
>             BinaryComparator(Bytes.toBytes(Integer.MIN_VALUE)));
>
>     scan.setFilter(filters);
> *********************************************
>
>
>
> On Wed, Mar 19, 2014 at 7:31 PM, ramkrishna vasudevan <
> ramkrishna.s.vasudevan@gmail.com> wrote:
>
>> Which version you are using?
>> Any way you have guessed it correctly.  The best option is to write your
>> own BinaryComparator that compares negatives also.  As everything is
>> lexographically sorted you may end up in getting negatives as bigger
>> values.
>>
>> Regards
>> Ram
>>
>>
>> On Wed, Mar 19, 2014 at 4:49 PM, Chaitanya <chaitanya.ck100@gmail.com
>> >wrote:
>>
>> > Hi All,
>> >
>> > I have come across an issue while using filters to get a result.
>> >
>> > For eg.
>> > I have created a table and its specifications are as follows :
>> >
>> > table name --> test
>> > column family --> cf
>> > row keys --> rowKey1 - rowKey10 (10 different row keys)
>> > column qualifier --> integerData
>> >
>> > For different rowkeys, the qualifier 'integerData' contains either
>> positive
>> > or negative integer values (data loaded randomly).
>> >
>> > Now, while I am trying to retrieve the data from the table based on a
>> > filter
>> > condition, its failing to give the desired result.
>> >
>> > For eg. say,
>> > My table contains following data :
>> > [-50,-40,-30,-20,-10,10,20,30,40,50]
>> >
>> > I want to get only those values which are greater than or equal to 40.
>> > Following is the code for the filter set on scan :
>> >
>> > ********************************************
>> >   Scan scan = new Scan();
>> >   scan.addColumn(Bytes.toBytes("cf"), Bytes.toBytes(" integerData"));
>> >
>> >   int i = 40;
>> >   Filter filter = new ValueFilter(CompareOp.GREATER_OR_EQUAL,new
>> > BinaryComparator(Bytes.toBytes(i)));
>> >
>> >   scan.setFilter(filter);
>> > *********************************************
>> >
>> > The result should be : 40 and 50
>> > BUT, the actual result is : -50, -40, -30, -20, -10, 40, 50
>> >
>> > I have read few posts which addressed this issue, and few people
>> provided
>> > the solution as:
>> >
>> > 1) write a custom comparator, as BinaryComparator is not meant for
>> number
>> > comparison
>> > OR
>> > 2) retrieve all the values as integer and then compare
>> >
>> > BUT, I want to know if there is any other way to achieve this.
>> > Because this seems to be a very basic need, i.e. comparing numbers, and
>> I
>> > feel HBase should have something straight forward to deal with this.
>> > This comparison fails only when the negative numbers are involved.
>> >
>> > I am not able to get the right way to do it.
>> >
>> > Can anyone help me on this ?
>> >
>> >
>> >
>> > -----
>> > Regards,
>> > Chaitanya
>> > --
>> > View this message in context:
>> >
>> http://apache-hbase.679495.n3.nabble.com/Filters-failing-to-compare-negative-numbers-int-float-double-or-long-tp4057268.html
>> > Sent from the HBase User mailing list archive at Nabble.com.
>> >
>>
>
>
>
> --
> Best Regards,
> Haosdent Huang
>



-- 
Best Regards,
Haosdent Huang

Re: Filters failing to compare negative numbers (int,float,double or long)

Posted by haosdent <ha...@gmail.com>.
Hi, Chaitanya. My code snippet have some problem before. The snippet below
is my new code, it works well in my machine.

    byte[] cfBytes = Bytes.toBytes("cf");
    byte[] qBytes = Bytes.toBytes("q");
    Scan scan = new Scan(Bytes.toBytes(0), Bytes.toBytes(10));
    FilterList filterList = new FilterList();
    SingleColumnValueFilter filter = new SingleColumnValueFilter(
            cfBytes,
            qBytes,
            CompareFilter.CompareOp.GREATER_OR_EQUAL,
            Bytes.toBytes(1));
    filterList.addFilter(filter);
    filter = new SingleColumnValueFilter(
            cfBytes,
            qBytes,
            CompareFilter.CompareOp.LESS,
            Bytes.toBytes(Integer.MIN_VALUE));
    filterList.addFilter(filter);
    scan.setFilter(filterList);



On Wed, Mar 19, 2014 at 8:26 PM, Chaitanya <ch...@gmail.com>wrote:

> Hi Ramkrishna,
>
> There is one more problem, i.e. I don't have the rights to add anything to
> the hbase jar.
> The problem with writing a custom comparator is that we have to add it to
> the jar so that its available to all the hbase region servers.
> Please correct me if I am wrong.
> Thanks
>
>
>
> -----
> Regards,
> Chaitanya
> --
> View this message in context:
> http://apache-hbase.679495.n3.nabble.com/Filters-failing-to-compare-negative-numbers-int-float-double-or-long-tp4057268p4057279.html
> Sent from the HBase User mailing list archive at Nabble.com.
>



-- 
Best Regards,
Haosdent Huang

Re: Filters failing to compare negative numbers (int,float,double or long)

Posted by Chaitanya <ch...@gmail.com>.
Hi Haosdent,

Thanks for the solution.
I really appreciate your effort.

Your code works perfectly fine for the integers greater than 0, but in case
you provide an integer that is less than 0, i.e. negative numbers in the
comparator, it fails, as you have just ignored the negative numbers.




-----
Regards,
Chaitanya
--
View this message in context: http://apache-hbase.679495.n3.nabble.com/Filters-failing-to-compare-negative-numbers-int-float-double-or-long-tp4057268p4057335.html
Sent from the HBase User mailing list archive at Nabble.com.

Re: Filters failing to compare negative numbers (int,float,double or long)

Posted by haosdent <ha...@gmail.com>.
My complete test code. It should be helpful for you.

----------------
  public static void testHBase() throws IOException {
    byte[] cfBytes = Bytes.toBytes("cf");
    byte[] qBytes = Bytes.toBytes("q");

    Configuration conf = new Configuration();
    conf.set(HConstants.ZOOKEEPER_CLIENT_PORT, "40060");
    conf.set(HConstants.ZOOKEEPER_QUORUM,
"10.232.98.94,10.232.98.72,10.232.98.40");
    conf.set(HConstants.ZOOKEEPER_ZNODE_PARENT, "/hbase-cdh4");
    HTable htable = new HTable(conf, "datax_hbasebulkwriter_target_2w");
    for (int i = 0; i < 10; i++) {
      Put put = new Put(Bytes.toBytes(i));
      if (i < 5) {
        put.add(cfBytes, qBytes, Bytes.toBytes(i));
      } else {
        put.add(cfBytes, qBytes, Bytes.toBytes(-i));
      }
      htable.put(put);
    }

    Scan scan = new Scan(Bytes.toBytes(0), Bytes.toBytes(10));
    FilterList filterList = new FilterList();
    SingleColumnValueFilter filter = new SingleColumnValueFilter(
            cfBytes,
            qBytes,
            CompareFilter.CompareOp.GREATER_OR_EQUAL,
            Bytes.toBytes(1));
    filterList.addFilter(filter);
    filter = new SingleColumnValueFilter(
            cfBytes,
            qBytes,
            CompareFilter.CompareOp.LESS,
            Bytes.toBytes(Integer.MIN_VALUE));
    filterList.addFilter(filter);
    scan.setFilter(filterList);

    ResultScanner rs = htable.getScanner(scan);
    Result r = rs.next();
    while (r != null) {
      System.out.println(Bytes.toInt(r.getColumnLatest(cfBytes,
qBytes).getValue()));
      r = rs.next();
    }

    htable.close();
  }
----------------

Console output:

1
2
3
4

Process finished with exit code 0
----------------


On Wed, Mar 19, 2014 at 8:33 PM, praveenesh kumar <pr...@gmail.com>wrote:

> I am not sure if this can helps - https://github.com/ndimiduk/orderly
>
> This API supports negative keys, as far as I know. You can give it a try,
> its simple to implement your keys using this API
>
> Regards
> Prav
>
>
> On Wed, Mar 19, 2014 at 12:26 PM, Chaitanya <chaitanya.ck100@gmail.com
> >wrote:
>
> > Hi Ramkrishna,
> >
> > There is one more problem, i.e. I don't have the rights to add anything
> to
> > the hbase jar.
> > The problem with writing a custom comparator is that we have to add it to
> > the jar so that its available to all the hbase region servers.
> > Please correct me if I am wrong.
> > Thanks
> >
> >
> >
> > -----
> > Regards,
> > Chaitanya
> > --
> > View this message in context:
> >
> http://apache-hbase.679495.n3.nabble.com/Filters-failing-to-compare-negative-numbers-int-float-double-or-long-tp4057268p4057279.html
> > Sent from the HBase User mailing list archive at Nabble.com.
> >
>



-- 
Best Regards,
Haosdent Huang

Re: Filters failing to compare negative numbers (int,float,double or long)

Posted by James Taylor <jt...@salesforce.com>.
Another option is to use Apache Phoenix (
http://phoenix.incubator.apache.org/) as it takes care of all these details
for you automatically.

Cheers,
James


On Wed, Mar 19, 2014 at 7:49 AM, Ted Yu <yu...@gmail.com> wrote:

> In 0.96+, extensible data type API is provided.
> Please take a look
> at
> hbase-common/src/main/java/org/apache/hadoop/hbase/types/package-info.java
>
> Specifically the OrderedInt16, OrderedInt32 and OrderedInt64 would be of
> interest to you.
>
> Cheers
>
>
>
> On Wed, Mar 19, 2014 at 5:33 AM, praveenesh kumar <praveenesh@gmail.com
> >wrote:
>
> > I am not sure if this can helps - https://github.com/ndimiduk/orderly
> >
> > This API supports negative keys, as far as I know. You can give it a try,
> > its simple to implement your keys using this API
> >
> > Regards
> > Prav
> >
> >
> > On Wed, Mar 19, 2014 at 12:26 PM, Chaitanya <chaitanya.ck100@gmail.com
> > >wrote:
> >
> > > Hi Ramkrishna,
> > >
> > > There is one more problem, i.e. I don't have the rights to add anything
> > to
> > > the hbase jar.
> > > The problem with writing a custom comparator is that we have to add it
> to
> > > the jar so that its available to all the hbase region servers.
> > > Please correct me if I am wrong.
> > > Thanks
> > >
> > >
> > >
> > > -----
> > > Regards,
> > > Chaitanya
> > > --
> > > View this message in context:
> > >
> >
> http://apache-hbase.679495.n3.nabble.com/Filters-failing-to-compare-negative-numbers-int-float-double-or-long-tp4057268p4057279.html
> > > Sent from the HBase User mailing list archive at Nabble.com.
> > >
> >
>

Re: Filters failing to compare negative numbers (int,float,double or long)

Posted by Chaitanya <ch...@gmail.com>.
Hi Ted Yu,

Thanks for the valuable information.
Its really good to know about this API.

Though, it won't help me at present.
My version is 0.94.2 and I am not allowed to use the higher version at the
moment.



-----
Regards,
Chaitanya
--
View this message in context: http://apache-hbase.679495.n3.nabble.com/Filters-failing-to-compare-negative-numbers-int-float-double-or-long-tp4057268p4057336.html
Sent from the HBase User mailing list archive at Nabble.com.

Re: Filters failing to compare negative numbers (int,float,double or long)

Posted by Ted Yu <yu...@gmail.com>.
In 0.96+, extensible data type API is provided.
Please take a look
at hbase-common/src/main/java/org/apache/hadoop/hbase/types/package-info.java

Specifically the OrderedInt16, OrderedInt32 and OrderedInt64 would be of
interest to you.

Cheers



On Wed, Mar 19, 2014 at 5:33 AM, praveenesh kumar <pr...@gmail.com>wrote:

> I am not sure if this can helps - https://github.com/ndimiduk/orderly
>
> This API supports negative keys, as far as I know. You can give it a try,
> its simple to implement your keys using this API
>
> Regards
> Prav
>
>
> On Wed, Mar 19, 2014 at 12:26 PM, Chaitanya <chaitanya.ck100@gmail.com
> >wrote:
>
> > Hi Ramkrishna,
> >
> > There is one more problem, i.e. I don't have the rights to add anything
> to
> > the hbase jar.
> > The problem with writing a custom comparator is that we have to add it to
> > the jar so that its available to all the hbase region servers.
> > Please correct me if I am wrong.
> > Thanks
> >
> >
> >
> > -----
> > Regards,
> > Chaitanya
> > --
> > View this message in context:
> >
> http://apache-hbase.679495.n3.nabble.com/Filters-failing-to-compare-negative-numbers-int-float-double-or-long-tp4057268p4057279.html
> > Sent from the HBase User mailing list archive at Nabble.com.
> >
>

Re: Filters failing to compare negative numbers (int,float,double or long)

Posted by praveenesh kumar <pr...@gmail.com>.
Orderly API has examples in it. Just download the API, understand the
examples.. create your own keys (including negative keys). Create the
similar scenario, it shouldn't be that hard.


On Tue, Mar 25, 2014 at 12:41 PM, Chaitanya <ch...@gmail.com>wrote:

> Thanks for your valuable time.
>
> I have tried to use the Orderly API, but I am not able to find how is this
> going to work with filter.
> I am still not able to extract data from my table based on the condition
> that compares int data.
>
> can you please give a hint on how to use this API properly to achieve my
> goal.
>
>
>
> -----
> Regards,
> Chaitanya
> --
> View this message in context:
> http://apache-hbase.679495.n3.nabble.com/Filters-failing-to-compare-negative-numbers-int-float-double-or-long-tp4057268p4057487.html
> Sent from the HBase User mailing list archive at Nabble.com.
>

Re: Filters failing to compare negative numbers (int,float,double or long)

Posted by Chaitanya <ch...@gmail.com>.
Thanks for your valuable time.

I have tried to use the Orderly API, but I am not able to find how is this
going to work with filter.
I am still not able to extract data from my table based on the condition
that compares int data.

can you please give a hint on how to use this API properly to achieve my
goal.



-----
Regards,
Chaitanya
--
View this message in context: http://apache-hbase.679495.n3.nabble.com/Filters-failing-to-compare-negative-numbers-int-float-double-or-long-tp4057268p4057487.html
Sent from the HBase User mailing list archive at Nabble.com.

Re: Filters failing to compare negative numbers (int,float,double or long)

Posted by praveenesh kumar <pr...@gmail.com>.
If you don't want to upgrade Hbase, you can use this API as third party
library.. and use it by just making changes in your application.


On Thu, Mar 20, 2014 at 10:38 AM, Chaitanya <ch...@gmail.com>wrote:

> Hi Praveenesh,
>
> Thanks for the valuable information.
> Its really good to know about this API.
>
> Though, it probably won't help me at present.
> What I have came across, says this API is provided for higher versions of
> HBase.
> My version is 0.94.2 and I am not allowed to use the higher version at the
> moment.
>
>
>
> -----
> Regards,
> Chaitanya
> --
> View this message in context:
> http://apache-hbase.679495.n3.nabble.com/Filters-failing-to-compare-negative-numbers-int-float-double-or-long-tp4057268p4057337.html
> Sent from the HBase User mailing list archive at Nabble.com.
>

Re: Filters failing to compare negative numbers (int,float,double or long)

Posted by Chaitanya <ch...@gmail.com>.
Hi Praveenesh,

Thanks for the valuable information.
Its really good to know about this API.

Though, it probably won't help me at present.
What I have came across, says this API is provided for higher versions of
HBase.
My version is 0.94.2 and I am not allowed to use the higher version at the
moment. 



-----
Regards,
Chaitanya
--
View this message in context: http://apache-hbase.679495.n3.nabble.com/Filters-failing-to-compare-negative-numbers-int-float-double-or-long-tp4057268p4057337.html
Sent from the HBase User mailing list archive at Nabble.com.

Re: Filters failing to compare negative numbers (int,float,double or long)

Posted by praveenesh kumar <pr...@gmail.com>.
I am not sure if this can helps - https://github.com/ndimiduk/orderly

This API supports negative keys, as far as I know. You can give it a try,
its simple to implement your keys using this API

Regards
Prav


On Wed, Mar 19, 2014 at 12:26 PM, Chaitanya <ch...@gmail.com>wrote:

> Hi Ramkrishna,
>
> There is one more problem, i.e. I don't have the rights to add anything to
> the hbase jar.
> The problem with writing a custom comparator is that we have to add it to
> the jar so that its available to all the hbase region servers.
> Please correct me if I am wrong.
> Thanks
>
>
>
> -----
> Regards,
> Chaitanya
> --
> View this message in context:
> http://apache-hbase.679495.n3.nabble.com/Filters-failing-to-compare-negative-numbers-int-float-double-or-long-tp4057268p4057279.html
> Sent from the HBase User mailing list archive at Nabble.com.
>

Re: Filters failing to compare negative numbers (int,float,double or long)

Posted by Chaitanya <ch...@gmail.com>.
Hi Ramkrishna,

There is one more problem, i.e. I don't have the rights to add anything to
the hbase jar.
The problem with writing a custom comparator is that we have to add it to
the jar so that its available to all the hbase region servers.
Please correct me if I am wrong.
Thanks



-----
Regards,
Chaitanya
--
View this message in context: http://apache-hbase.679495.n3.nabble.com/Filters-failing-to-compare-negative-numbers-int-float-double-or-long-tp4057268p4057279.html
Sent from the HBase User mailing list archive at Nabble.com.

Re: Filters failing to compare negative numbers (int,float,double or long)

Posted by ramkrishna vasudevan <ra...@gmail.com>.
The straight forward soln is to have a custom comparator.  Otherwise you
may have to do some inversion in your logic while storing negative numbers
and use the reversion of that inversion while reading back.

Regards
Ram


On Wed, Mar 19, 2014 at 5:40 PM, Chaitanya <ch...@gmail.com>wrote:

> Hi Haosdent Huang,
>
> Before anything, thanks for your reply.
>
> I tried your solution but its not working for me.
> I think the problem is with the BinaryComparator.
> It fails to compare the numbers as it compares bits by bits.
>
> I hope there should be some straight forward solution to this problem.
>
> Thanks again
>
>
>
> -----
> Regards,
> Chaitanya
> --
> View this message in context:
> http://apache-hbase.679495.n3.nabble.com/Filters-failing-to-compare-negative-numbers-int-float-double-or-long-tp4057268p4057276.html
> Sent from the HBase User mailing list archive at Nabble.com.
>

Re: Filters failing to compare negative numbers (int,float,double or long)

Posted by Chaitanya <ch...@gmail.com>.
Hi Haosdent Huang,

Before anything, thanks for your reply.

I tried your solution but its not working for me.
I think the problem is with the BinaryComparator.
It fails to compare the numbers as it compares bits by bits.

I hope there should be some straight forward solution to this problem.

Thanks again



-----
Regards,
Chaitanya
--
View this message in context: http://apache-hbase.679495.n3.nabble.com/Filters-failing-to-compare-negative-numbers-int-float-double-or-long-tp4057268p4057276.html
Sent from the HBase User mailing list archive at Nabble.com.

Re: Filters failing to compare negative numbers (int,float,double or long)

Posted by haosdent <ha...@gmail.com>.
For your scenario, I thinks this code snippet should work for you:

*********************************************
    int i = 40;
    FilterList filters = new FilterList();
    ValueFilter filter = new
ValueFilter(CompareFilter.CompareOp.GREATER_OR_EQUAL,new
            BinaryComparator(Bytes.toBytes(i)));
    filters.addFilter(filter);
    filter = new ValueFilter(CompareFilter.CompareOp.LESS,new
            BinaryComparator(Bytes.toBytes(Integer.MIN_VALUE)));

    scan.setFilter(filters);
*********************************************



On Wed, Mar 19, 2014 at 7:31 PM, ramkrishna vasudevan <
ramkrishna.s.vasudevan@gmail.com> wrote:

> Which version you are using?
> Any way you have guessed it correctly.  The best option is to write your
> own BinaryComparator that compares negatives also.  As everything is
> lexographically sorted you may end up in getting negatives as bigger
> values.
>
> Regards
> Ram
>
>
> On Wed, Mar 19, 2014 at 4:49 PM, Chaitanya <chaitanya.ck100@gmail.com
> >wrote:
>
> > Hi All,
> >
> > I have come across an issue while using filters to get a result.
> >
> > For eg.
> > I have created a table and its specifications are as follows :
> >
> > table name --> test
> > column family --> cf
> > row keys --> rowKey1 - rowKey10 (10 different row keys)
> > column qualifier --> integerData
> >
> > For different rowkeys, the qualifier 'integerData' contains either
> positive
> > or negative integer values (data loaded randomly).
> >
> > Now, while I am trying to retrieve the data from the table based on a
> > filter
> > condition, its failing to give the desired result.
> >
> > For eg. say,
> > My table contains following data :
> > [-50,-40,-30,-20,-10,10,20,30,40,50]
> >
> > I want to get only those values which are greater than or equal to 40.
> > Following is the code for the filter set on scan :
> >
> > ********************************************
> >   Scan scan = new Scan();
> >   scan.addColumn(Bytes.toBytes("cf"), Bytes.toBytes(" integerData"));
> >
> >   int i = 40;
> >   Filter filter = new ValueFilter(CompareOp.GREATER_OR_EQUAL,new
> > BinaryComparator(Bytes.toBytes(i)));
> >
> >   scan.setFilter(filter);
> > *********************************************
> >
> > The result should be : 40 and 50
> > BUT, the actual result is : -50, -40, -30, -20, -10, 40, 50
> >
> > I have read few posts which addressed this issue, and few people provided
> > the solution as:
> >
> > 1) write a custom comparator, as BinaryComparator is not meant for number
> > comparison
> > OR
> > 2) retrieve all the values as integer and then compare
> >
> > BUT, I want to know if there is any other way to achieve this.
> > Because this seems to be a very basic need, i.e. comparing numbers, and I
> > feel HBase should have something straight forward to deal with this.
> > This comparison fails only when the negative numbers are involved.
> >
> > I am not able to get the right way to do it.
> >
> > Can anyone help me on this ?
> >
> >
> >
> > -----
> > Regards,
> > Chaitanya
> > --
> > View this message in context:
> >
> http://apache-hbase.679495.n3.nabble.com/Filters-failing-to-compare-negative-numbers-int-float-double-or-long-tp4057268.html
> > Sent from the HBase User mailing list archive at Nabble.com.
> >
>



-- 
Best Regards,
Haosdent Huang

Re: Filters failing to compare negative numbers (int,float,double or long)

Posted by Chaitanya <ch...@gmail.com>.
Hi Ramkrishna,

Thanks for the reply.
I am using HBase 0.94.2

Do you know if any higher version has provided with anything that addresses
this issue ?



-----
Regards,
Chaitanya
--
View this message in context: http://apache-hbase.679495.n3.nabble.com/Filters-failing-to-compare-negative-numbers-int-float-double-or-long-tp4057268p4057278.html
Sent from the HBase User mailing list archive at Nabble.com.

Re: Filters failing to compare negative numbers (int,float,double or long)

Posted by ramkrishna vasudevan <ra...@gmail.com>.
Which version you are using?
Any way you have guessed it correctly.  The best option is to write your
own BinaryComparator that compares negatives also.  As everything is
lexographically sorted you may end up in getting negatives as bigger values.

Regards
Ram


On Wed, Mar 19, 2014 at 4:49 PM, Chaitanya <ch...@gmail.com>wrote:

> Hi All,
>
> I have come across an issue while using filters to get a result.
>
> For eg.
> I have created a table and its specifications are as follows :
>
> table name --> test
> column family --> cf
> row keys --> rowKey1 - rowKey10 (10 different row keys)
> column qualifier --> integerData
>
> For different rowkeys, the qualifier 'integerData' contains either positive
> or negative integer values (data loaded randomly).
>
> Now, while I am trying to retrieve the data from the table based on a
> filter
> condition, its failing to give the desired result.
>
> For eg. say,
> My table contains following data :
> [-50,-40,-30,-20,-10,10,20,30,40,50]
>
> I want to get only those values which are greater than or equal to 40.
> Following is the code for the filter set on scan :
>
> ********************************************
>   Scan scan = new Scan();
>   scan.addColumn(Bytes.toBytes("cf"), Bytes.toBytes(" integerData"));
>
>   int i = 40;
>   Filter filter = new ValueFilter(CompareOp.GREATER_OR_EQUAL,new
> BinaryComparator(Bytes.toBytes(i)));
>
>   scan.setFilter(filter);
> *********************************************
>
> The result should be : 40 and 50
> BUT, the actual result is : -50, -40, -30, -20, -10, 40, 50
>
> I have read few posts which addressed this issue, and few people provided
> the solution as:
>
> 1) write a custom comparator, as BinaryComparator is not meant for number
> comparison
> OR
> 2) retrieve all the values as integer and then compare
>
> BUT, I want to know if there is any other way to achieve this.
> Because this seems to be a very basic need, i.e. comparing numbers, and I
> feel HBase should have something straight forward to deal with this.
> This comparison fails only when the negative numbers are involved.
>
> I am not able to get the right way to do it.
>
> Can anyone help me on this ?
>
>
>
> -----
> Regards,
> Chaitanya
> --
> View this message in context:
> http://apache-hbase.679495.n3.nabble.com/Filters-failing-to-compare-negative-numbers-int-float-double-or-long-tp4057268.html
> Sent from the HBase User mailing list archive at Nabble.com.
>