> I assume I'm doing something wrong in the select.  Am I incorrectly using
> the ResultSet?
>

You're incorrectly using the returned ByteBuffer. But you should not feel
bad, that API kinda
sucks.

The short version is that .array() returns the backing array of the
ByteBuffer. But there is no
guarantee that you'll have a one-to-one correspondence between the valid
content of the
ByteBuffer and the backing array, the backing array can be bigger in
particular (long story short,
this allows multiple ByteBuffer to share the same backing array, which can
avoid doing copies).

I also note that there is no guarantee that .array() will work unless
you've called .hasArray().

Anyway, what you could do is:
ByteBuffer bb = resultSet.one().getBytes("data");
byte[] data = new byte[bb.remaining()];
bb.get(data);

Alternatively, you can use the result of .array(), but you should only
consider the bb.remaining()
bytes starting at bb.arrayOffset() + bb.position() (where bb is the
returned ByteBuffer).

--
Sylvain



>
> -brian
>
> On Thu, Apr 11, 2013 at 9:09 AM, Brian O'Neill <b...@alumni.brown.edu>wrote:
>
>> Yep, it worked like a charm.  (PreparedStatement avoided the hex
>> conversion)
>>
>> But now, I'm seeing a few extra bytes come back in the select….
>> (I'll keep digging, but maybe you have some insight?)
>>
>> I see this:
>>
>> ERROR [2013-04-11 13:05:03,461] com.skookle.dao.RepositoryDao:
>> repository.add() byte.length()=[259804]
>>
>> ERROR [2013-04-11 13:08:08,487] com.skookle.dao.RepositoryDao:
>> repository.get() [foo.jpeg] byte.length()=[259861]
>>
>> (Notice the length's don't match up)
>>
>> Using this code:
>>
>>     public void addContent(String key, byte[] data)
>>
>>             throws NoHostAvailableException {
>>
>>         LOG.error("repository.add() byte.length()=[" + data.length + "]"
>> );
>>
>>         String statement = "INSERT INTO " + KEYSPACE + "." + TABLE + "(key,
>> data) VALUES (?, ?)";
>>
>>         PreparedStatement ps = session.prepare(statement);
>>
>>         BoundStatement bs = ps.bind(key, ByteBuffer.wrap(data));
>>
>>         session.execute(bs);
>>
>>     }
>>
>>
>>     public byte[] getContent(String key) throws NoHostAvailableException
>> {
>>
>>         Query select = select("data").from(KEYSPACE, TABLE).where(eq(
>> "key", key));
>>
>>         ResultSet resultSet = session.execute(select);
>>
>>         byte[] data = resultSet.one().getBytes("data").array();
>>
>>         LOG.error("repository.get() [" + key + "] byte.length()=[" +
>> data.length + "]");
>>
>>         return data;
>>
>>     }
>>
>> ---
>>
>> Brian O'Neill
>>
>> Lead Architect, Software Development
>>
>> *Health Market Science*
>>
>> *The Science of Better Results*
>>
>> 2700 Horizon Drive • King of Prussia, PA • 19406****
>>
>> M: 215.588.6024 • @boneill42 <http://www.twitter.com/boneill42>  •
>>
>> healthmarketscience.com
>>
>>
>> This information transmitted in this email message is for the intended
>> recipient only and may contain confidential and/or privileged material. If
>> you received this email in error and are not the intended recipient, or the
>> person responsible to deliver it to the intended recipient, please contact
>> the sender at the email above and delete this email and any attachments and
>> destroy any copies thereof. Any review, retransmission, dissemination,
>> copying or other use of, or taking any action in reliance upon, this
>> information by persons or entities other than the intended recipient is
>> strictly prohibited.****
>>
>> ** **
>>
>>
>> From: Sylvain Lebresne <sylv...@datastax.com>
>> Reply-To: <user@cassandra.apache.org>
>> Date: Thursday, April 11, 2013 8:48 AM
>> To: "user@cassandra.apache.org" <user@cassandra.apache.org>
>> Cc: Gabriel Ciuloaica <gciuloa...@gmail.com>
>> Subject: Re: Blobs in CQL?
>>
>>
>> Hopefully, the prepared statement doesn't do the conversion.
>>>
>>
>> It does not.
>>
>>
>>> (I'm not sure if it is a limitation of the CQL protocol itself)
>>>
>>> thanks again,
>>> -brian
>>>
>>>
>>>
>>> ---
>>> Brian O'Neill
>>> Lead Architect, Software Development
>>> Health Market Science
>>> The Science of Better Results
>>> 2700 Horizon Drive • King of Prussia, PA • 19406
>>> M: 215.588.6024 • @boneill42 <http://www.twitter.com/boneill42>  •
>>> healthmarketscience.com
>>>
>>> This information transmitted in this email message is for the intended
>>> recipient only and may contain confidential and/or privileged material.
>>> If
>>> you received this email in error and are not the intended recipient, or
>>> the person responsible to deliver it to the intended recipient, please
>>> contact the sender at the email above and delete this email and any
>>> attachments and destroy any copies thereof. Any review, retransmission,
>>> dissemination, copying or other use of, or taking any action in reliance
>>> upon, this information by persons or entities other than the intended
>>> recipient is strictly prohibited.
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>> On 4/11/13 8:34 AM, "Gabriel Ciuloaica" <gciuloa...@gmail.com> wrote:
>>>
>>> >I'm not using the query builder but the PreparedStatement.
>>> >
>>> >Here is the sample code: https://gist.github.com/devsprint/5363023
>>> >
>>> >Gabi
>>> >On 4/11/13 3:27 PM, Brian O'Neill wrote:
>>> >> Great!
>>> >>
>>> >> Thanks Gabriel.  Do you have an example? (are using QueryBuilder?)
>>> >> I couldn't find the part of  the API that allowed you to pass in the
>>> >>byte
>>> >> array.
>>> >>
>>> >> -brian
>>> >>
>>> >> ---
>>> >> Brian O'Neill
>>> >> Lead Architect, Software Development
>>> >> Health Market Science
>>> >> The Science of Better Results
>>> >> 2700 Horizon Drive € King of Prussia, PA € 19406
>>> >> M: 215.588.6024 € @boneill42 <http://www.twitter.com/boneill42>  €
>>> >> healthmarketscience.com
>>> >>
>>> >> This information transmitted in this email message is for the intended
>>> >> recipient only and may contain confidential and/or privileged
>>> material.
>>> >>If
>>> >> you received this email in error and are not the intended recipient,
>>> or
>>> >> the person responsible to deliver it to the intended recipient, please
>>> >> contact the sender at the email above and delete this email and any
>>> >> attachments and destroy any copies thereof. Any review,
>>> retransmission,
>>> >> dissemination, copying or other use of, or taking any action in
>>> reliance
>>> >> upon, this information by persons or entities other than the intended
>>> >> recipient is strictly prohibited.
>>> >>
>>> >>
>>> >>
>>> >>
>>> >>
>>> >>
>>> >>
>>> >> On 4/11/13 8:25 AM, "Gabriel Ciuloaica" <gciuloa...@gmail.com> wrote:
>>> >>
>>> >>> Hi Brian,
>>> >>>
>>> >>> I'm using the blobs to store images in cassandra(1.2.3) using the
>>> >>> java-driver version 1.0.0-beta1.
>>> >>> There is no need to convert a byte array into hex.
>>> >>>
>>> >>> Br,
>>> >>> Gabi
>>> >>>
>>> >>> On 4/11/13 3:21 PM, Brian O'Neill wrote:
>>> >>>> I started playing around with the CQL driver.
>>> >>>> Has anyone used blobs with it yet?
>>> >>>>
>>> >>>> Are you forced to convert a byte[] to hex?
>>> >>>> (e.g. I have a photo that I want to store in C* using the
>>> java-driver
>>> >>>> API)
>>> >>>>
>>> >>>> -brian
>>> >>>>
>>> >>>> --
>>> >>>> Brian ONeill
>>> >>>> Lead Architect, Health Market Science (
>>> http://healthmarketscience.com)
>>> >>>> mobile:215.588.6024
>>> >>>> blog: http://brianoneill.blogspot.com/
>>> >>>> twitter: @boneill42
>>> >>
>>> >
>>>
>>>
>>>
>>
>
>
> --
> Brian ONeill
> Lead Architect, Health Market Science (http://healthmarketscience.com)
>
> mobile:215.588.6024
> blog: http://brianoneill.blogspot.com/
> twitter: @boneill42
>

Reply via email to