Problem retrieving data while using ByteBufferSerializer

151 views
Skip to first unread message

soumik

unread,
Jun 30, 2011, 2:12:52 PM6/30/11
to hector-users
Hi,
I'm trying out hector 0.7.0-30 with Cassandra 0.7.6-02 & I'm trying
to perform rangleSlicesQuery.
Problem is when I'm using ByteBufferSerializer to get the column
value(since we can expect different types of data being retrieved),
I'm getting error while converting the ByteBuffer returned to String
data.

Following is the code:
--------------------------------
RangeSlicesQuery<String, String, ByteBuffer> rangeSlicesQuery =
HFactory.createRangeSlicesQuery(keyspace, StringSerializer.get(),
StringSerializer.get(),
ByteBufferSerializer.get());
rangeSlicesQuery.setColumnFamily("Nodes");
rangeSlicesQuery.setKeys("cd982cb5-b6a5-42a0-8c56-5532bc2e68c3", "");
rangeSlicesQuery.setRange("", "", false, 5);
rangeSlicesQuery.setRowCount(1);
QueryResult<OrderedRows<String, String, ByteBuffer>> result =
rangeSlicesQuery.execute();
OrderedRows<String, String, ByteBuffer> orderedRows = result.get();
for (Row<String, String, ByteBuffer> r : orderedRows) {
System.out.println("Row:" + r);
for(HColumn<String, ByteBuffer> cols :
r.getColumnSlice().getColumns())
{
System.out.println("ColName:" + cols.getName());
System.out.println("ColVal: " + new String(cols.getValue().array()));
}
}
--------------------------------

The output I'm getting contains the same junk data for every column
value as follows
-----------------------------
Row:Row(cd982cb5-
b6a5-42a0-8c56-5532bc2e68c3,ColumnSlice([HColumn(state=java.nio.HeapByteBuffer[pos=109
lim=110 cap=216]),
HColumn(time_created=java.nio.HeapByteBuffer[pos=152 lim=160
cap=216]), HColumn(type=java.nio.HeapByteBuffer[pos=194 lim=201
cap=216])]))
ColName:state
ColVal: € get_range_slices $cd982cb5-b6a5-42a0-8c56-5532bc2e68c3
state 2 time_created t£ type NodeMgr
ColName:time_created
ColVal: € get_range_slices $cd982cb5-b6a5-42a0-8c56-5532bc2e68c3
state 2 time_created t£ type NodeMgr
ColName:type
ColVal: € get_range_slices $cd982cb5-b6a5-42a0-8c56-5532bc2e68c3
state 2 time_created t£ type NodeMgr
ColName:type
-------------------------------
I've indicated spaces for the special characters in the output above.
However, if I use StringSerializer or even BytesArraySerializer I get
the correct output. Can anyone help me out as to why this is behaving
wrong for ByteBuffer return type?Is it the way I'm getting String from
the ByteBuffer(since HeapByteBuffer seems to indicate different
ByteBuffer)??

Thanks,
Soumik

Nate McCall

unread,
Jun 30, 2011, 2:20:02 PM6/30/11
to hector...@googlegroups.com
ByteBuffers cannot be converted to strings directly for any data type.

Using a long as an example, to convert to a long, you would have to
got through the byteBuffer as above, but wrap the column.getValue()
call with LongerSerializer.fromByteBuffer.

Nate McCall

unread,
Jun 30, 2011, 2:22:44 PM6/30/11
to hector...@googlegroups.com
Take a look at the internals of some classes in the template package
for how we do this. Specifically:
https://github.com/rantav/hector/blob/master/core/src/main/java/me/prettyprint/cassandra/service/template/AbstractResultWrapper.java

We should be adding getRangeSlices and getIndexedSlices to
ColumnFamilyTemplate in the next week or so as well.

soumik

unread,
Jun 30, 2011, 2:24:44 PM6/30/11
to hector-users
Got it. Thanks, Nate.

On Jun 30, 11:20 pm, Nate McCall <n...@datastax.com> wrote:
> ByteBuffers cannot be converted to strings directly for any data type.
>
> Using a long as an example, to convert to a long, you would have to
> got through the byteBuffer as above, but wrap the column.getValue()
> call with LongerSerializer.fromByteBuffer.
>
>
>
> On Thu, Jun 30, 2011 at 1:12 PM, soumik <soum...@gmail.com> wrote:
> > Hi,
> >  I'm trying out hector 0.7.0-30 with Cassandra 0.7.6-02 & I'm trying
> > to perform rangleSlicesQuery.
> > Problem is when I'm usingByteBufferSerializerto get the column
> > b6a5-42a0-8c56-5532bc2e68c3,ColumnSlice([HColumn(state=java.nio.HeapByteBuf­fer[pos=109
> > lim=110 cap=216]),
> > HColumn(time_created=java.nio.HeapByteBuffer[pos=152 lim=160
> > cap=216]), HColumn(type=java.nio.HeapByteBuffer[pos=194 lim=201
> > cap=216])]))
> > ColName:state
> > ColVal: €   get_range_slices   $cd982cb5-b6a5-42a0-8c56-5532bc2e68c3
> > state           2       time_created            t£      type     NodeMgr
> > ColName:time_created
> > ColVal: €   get_range_slices   $cd982cb5-b6a5-42a0-8c56-5532bc2e68c3
> > state           2       time_created            t£      type     NodeMgr
> > ColName:type
> > ColVal: €   get_range_slices   $cd982cb5-b6a5-42a0-8c56-5532bc2e68c3
> > state           2       time_created            t£      type     NodeMgr
> > ColName:type
> > -------------------------------
> > I've indicated spaces for the special characters in the output above.
> > However, if I use StringSerializer or even BytesArraySerializer I get
> > the correct output. Can anyone help me out as to why this is behaving
> > wrong for ByteBuffer return type?Is it the way I'm getting String from
> > the ByteBuffer(since HeapByteBuffer seems to indicate different
> > ByteBuffer)??
>
> > Thanks,
> > Soumik- Hide quoted text -
>
> - Show quoted text -

soumik

unread,
Jun 30, 2011, 2:32:13 PM6/30/11
to hector-users
Nate,
A follow up question on this:
So if I want to return multiple columns containing different data
types(as a wrapper method over Hector), what data type do I return
from my wrapper method?? I obviously won't know which column will have
which data type & I can't return ByteBuffer as a generic column value
type. Do I have to convert to byte[]??

- Soumik

On Jun 30, 11:22 pm, Nate McCall <n...@datastax.com> wrote:
> Take a look at the internals of some classes in the template package
> for how we do this. Specifically:https://github.com/rantav/hector/blob/master/core/src/main/java/me/pr...
>
> We should be adding getRangeSlices and getIndexedSlices to
> ColumnFamilyTemplate in the next week or so as well.
>
>
>
> On Thu, Jun 30, 2011 at 1:20 PM, Nate McCall <n...@datastax.com> wrote:
> > ByteBuffers cannot be converted to strings directly for any data type.
>
> > Using a long as an example, to convert to a long, you would have to
> > got through the byteBuffer as above, but wrap the column.getValue()
> > call with LongerSerializer.fromByteBuffer.
>
> > On Thu, Jun 30, 2011 at 1:12 PM, soumik <soum...@gmail.com> wrote:
> >> Hi,
> >>  I'm trying out hector 0.7.0-30 with Cassandra 0.7.6-02 & I'm trying
> >> to perform rangleSlicesQuery.
> >> Problem is when I'm usingByteBufferSerializerto get the column
> >> b6a5-42a0-8c56-5532bc2e68c3,ColumnSlice([HColumn(state=java.nio.HeapByteBuf­fer[pos=109
> >> lim=110 cap=216]),
> >> HColumn(time_created=java.nio.HeapByteBuffer[pos=152 lim=160
> >> cap=216]), HColumn(type=java.nio.HeapByteBuffer[pos=194 lim=201
> >> cap=216])]))
> >> ColName:state
> >> ColVal: €   get_range_slices   $cd982cb5-b6a5-42a0-8c56-5532bc2e68c3
> >> state           2       time_created            t£      type     NodeMgr
> >> ColName:time_created
> >> ColVal: €   get_range_slices   $cd982cb5-b6a5-42a0-8c56-5532bc2e68c3
> >> state           2       time_created            t£      type     NodeMgr
> >> ColName:type
> >> ColVal: €   get_range_slices   $cd982cb5-b6a5-42a0-8c56-5532bc2e68c3
> >> state           2       time_created            t£      type     NodeMgr
> >> ColName:type
> >> -------------------------------
> >> I've indicated spaces for the special characters in the output above.
> >> However, if I use StringSerializer or even BytesArraySerializer I get
> >> the correct output. Can anyone help me out as to why this is behaving
> >> wrong for ByteBuffer return type?Is it the way I'm getting String from
> >> the ByteBuffer(since HeapByteBuffer seems to indicate different
> >> ByteBuffer)??
>
> >> Thanks,

Nate McCall

unread,
Jun 30, 2011, 2:43:26 PM6/30/11
to hector...@googlegroups.com
If you don't know what the data type is going to be, there are two options:
- treating it as a byte[]
- storing a marker byte to denote type in the column value (or column
name with DynamicComparator).

Here is an example of this approach:
https://github.com/riptano/Cassandra-EZ-Client/blob/master/src/main/java/com/datastax/cassandra/Row.java

(Note: the above project is not ready for primetime and is more an
ease-of-use experiment. Regard it as for demonstration purposes only
for now).

Ashu

unread,
May 9, 2013, 3:26:59 AM5/9/13
to hector...@googlegroups.com
Hi Soumik..
I am facing the same problem. Can you send me your complete code after
solving the issue of random data types?

Reply all
Reply to author
Forward
0 new messages