You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@hbase.apache.org by Nicolas Seyvet <ni...@ericsson.com> on 2013/05/03 15:23:01 UTC

HBase thrift API - Mutation store a float in a column

Hi,

Using the HBase thrift API, and Java as a programming language, it seems that when setting a value in a Mutation :

Mutation mutation = new Mutation();
mutation.setIsDelete(false);
 mutation.setColumn(StringUtils.bytes("data latitude"));

ByteBuffer buf = ByteBuffer.allocate(8);
mutation.setValue(buf.putFloat(12f));

The resulting value in HBase (as seen from the shell) is always:

1234##2013-05-123 03:49:13                            column=data:latitude, timestamp=1367587243318, value=\x00\x00\x00\x00

What am I doing wrong?


RE: HBase thrift API - Mutation store a float in a column

Posted by Nicolas Seyvet <ni...@ericsson.com>.
Cool.

Thanks a lot.

-----Original Message-----
From: Ted Yu [mailto:yuzhihong@gmail.com] 
Sent: Friday, May 03, 2013 4:13 PM
To: user@hbase.apache.org
Subject: Re: HBase thrift API - Mutation store a float in a column

Nicolas:
I think Java client should be able to read this float - after conversion.

Cheers

On Fri, May 3, 2013 at 6:58 AM, Nicolas Seyvet
<ni...@ericsson.com>wrote:

> Hi,
>
> Thanks Ted.
>
> I think you mean to use flip() in order to pre-dispose the buffer for 
> read (ie a get). if I use flip() after generating the ByteBuffer then 
> my data gets to be stored in the row.
>
> ByteBuffer buf = ByteBuffer.allocate(8);
>
> buf.putFloat(12f)
> buf.flip();
> mutation.setValue(buf);
>
> works...
>
> Is this value readable by something else than another Thrift client? 
> For example can it be read by a Java Native client as a float?
>

Re: HBase thrift API - Mutation store a float in a column

Posted by Ted Yu <yu...@gmail.com>.
Nicolas:
I think Java client should be able to read this float - after conversion.

Cheers

On Fri, May 3, 2013 at 6:58 AM, Nicolas Seyvet
<ni...@ericsson.com>wrote:

> Hi,
>
> Thanks Ted.
>
> I think you mean to use flip() in order to pre-dispose the buffer for read
> (ie a get). if I use flip() after generating the ByteBuffer then my data
> gets to be stored in the row.
>
> ByteBuffer buf = ByteBuffer.allocate(8);
>
> buf.putFloat(12f)
> buf.flip();
> mutation.setValue(buf);
>
> works...
>
> Is this value readable by something else than another Thrift client? For
> example can it be read by a Java Native client as a float?
>

RE: HBase thrift API - Mutation store a float in a column

Posted by Nicolas Seyvet <ni...@ericsson.com>.
Hi,

Thanks Ted. 

I think you mean to use flip() in order to pre-dispose the buffer for read (ie a get). if I use flip() after generating the ByteBuffer then my data gets to be stored in the row.

ByteBuffer buf = ByteBuffer.allocate(8); 

buf.putFloat(12f)
buf.flip();
mutation.setValue(buf);

works...

Is this value readable by something else than another Thrift client? For example can it be read by a Java Native client as a float?

Re: HBase thrift API - Mutation store a float in a column

Posted by Ted Yu <yu...@gmail.com>.
Looking at
http://docs.oracle.com/javase/6/docs/api/java/nio/ByteBuffer.html#putFloat(float):

... into this buffer at the current position, and then increments the
position by four.

In TBaseHelper:

  public static int byteBufferToByteArray(ByteBuffer byteBuffer, byte[]
target, int offset) {

    int remaining = byteBuffer.remaining();

    System.arraycopy(byteBuffer.array(),

        byteBuffer.arrayOffset() + byteBuffer.position(),
I think you need to rewind before calling mutation.setValue().

On Fri, May 3, 2013 at 6:35 AM, Nicolas Seyvet
<ni...@ericsson.com>wrote:

> The initial value is 12.0, the resulting is always =\x00\x00\x00\x00
>
> Even if I put something bigger or smaller.
>
> -----Original Message-----
> From: manoj p [mailto:eorstvz@gmail.com]
> Sent: Friday, May 03, 2013 3:31 PM
> To: user@hbase.apache.org
> Subject: Re: HBase thrift API - Mutation store a float in a column
>
> HBase stores values as byte arrays. When a float value is converted to
> byte, there is a possibility of data truncation.
>
> I guess the precision loss that happens due to data truncation might be
> the reason for getting the same value 0 repeatedly.
>
> BR/Manoj
>

RE: HBase thrift API - Mutation store a float in a column

Posted by Nicolas Seyvet <ni...@ericsson.com>.
The initial value is 12.0, the resulting is always =\x00\x00\x00\x00

Even if I put something bigger or smaller.

-----Original Message-----
From: manoj p [mailto:eorstvz@gmail.com] 
Sent: Friday, May 03, 2013 3:31 PM
To: user@hbase.apache.org
Subject: Re: HBase thrift API - Mutation store a float in a column

HBase stores values as byte arrays. When a float value is converted to byte, there is a possibility of data truncation.

I guess the precision loss that happens due to data truncation might be the reason for getting the same value 0 repeatedly.

BR/Manoj

Re: HBase thrift API - Mutation store a float in a column

Posted by manoj p <eo...@gmail.com>.
HBase stores values as byte arrays. When a float value is converted to
byte, there is a possibility of data truncation.

I guess the precision loss that happens due to data truncation might be the
reason for getting the same value 0 repeatedly.

BR/Manoj