hbase-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Stuart Smith <stu24m...@yahoo.com>
Subject Re: Different value of integer on querying in hbase!
Date Fri, 16 Dec 2011 22:06:55 GMT

 Glad I could help!
The simplest way to access a hbase is using the HTable object:


Create a Get() object: 


And then issue a get() on the HTable object using the Get() object you just created;


Once you have that working, then I would move on to using a Scanner:


Which accepts a Scan() object to that defines how the scan is performed:


___But before you do any of that, I would take a look at the example they have up on the Hbase


which covers how to use both the Get()/Put interface an the Scanner interface().

Some more notes..

I've ended up storing almost everything in my database as a string.. which I then write out
in bytes to the DB.

So an Int would be something like:

Put.add( [family], [qualifier], Bytes.toBytes( Integer.toString( my_int ) ) );

This means everything is readable using the hbase shell, and if I say, decided to change a
field from an int to a long, it's OK, no worries about making sure I re-interpret everything
correctly, and read the right number of bytes, etc..

In general, it just makes thing super easy for me, because everything is readable, and everything
is the same type - which is especially handy when reading in from other languages and platforms
( say C# on Windows ), because pretty much every decent programming environment out there
has support for decoding UTF-8 strings. I don't have to worry about a java integer being so
many bytes and Big Endian when I read it back from a C# program on windows.

Since Hbase compresses all it's data (assuming you have LZO enabled - you should!), you don't
really waste too much space or anything (text generally compresses fairly well).

Of course, the "real" solution is to use something like Avro:


and encode you data using that... but I'm lazy (efficient?), and strings work just fine, so
it's all good.

Take care,

 From: neuron005 <neuron005@gmail.com>
To: hbase-user@hadoop.apache.org 
Sent: Thursday, December 15, 2011 9:52 PM
Subject: Re: Different value of integer on querying in hbase!

Stuart Smith-8 wrote:
> Hello,
>   How did you query base via a statement object? Are you using Hive? 
> Or is this some new interface I don't know about.. I always had to use
> Get() or Scan().
> And hbase stores everything as bytes, not strings.. unlike C, in java,
> there is a difference ;)
> Take care,
>  -stu
> Thanks for your reply Stu
> I am a newbie and thanks to you , you saved me from a big trouble.Hbase
> stores as bytes. So, If I store an integer value, I will have to cast it
> from byte to int. I was using getInt("String") method to read from hbase.
> Or Should I use Scanner ?
> Please tell
> ________________________________
>  From: neuron005 <neuron005@gmail.com>
> To: hbase-user@hadoop.apache.org 
> Sent: Wednesday, December 14, 2011 3:26 AM
> Subject: Different value of integer on querying in hbase!
> Hii there,
> I just created a simple hbase table from my java program, Inserted value
> in
> it. But I got into an issue that whenever I store an integer value in
> habse
> and retrieve it by my java program , It gives a different value.
> For eg. 
> I inserted an int value in my hbase table '1234'
> When I queried from this using my java code -It showed a value  892745528. 
> Though hbase stores everything as string but I casted my result as 
> ResultSet rs=stmt.execute("select * from mapping_name");
> int val2=rs.getInt("val2");//val2 of type string
> Please help me out. Thanks in advance.
> Cheers:working:
> -- 
> View this message in context:
> http://old.nabble.com/Different-value-of-integer-on-querying-in-hbase%21-tp32974072p32974072.html
> Sent from the HBase User mailing list archive at Nabble.com.

View this message in context: http://old.nabble.com/Different-value-of-integer-on-querying-in-hbase%21-tp32974072p32985019.html
Sent from the HBase User mailing list archive at Nabble.com.
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message