You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cassandra.apache.org by Sébastien Pierre <se...@gmail.com> on 2010/01/21 21:56:59 UTC

Cassandra with LongType columns

Hi !

So I moved forward using Cassandra for logging HTTP traffic, and I come up
with the following schema:

  <Keyspaces>
    <Keyspace Name="AdKit">
      <ColumnFamily CompareWith="TimeUUIDType" Name="Logs"/>
      <ColumnFamily CompareWith="LongType"     Name="Campaigns"/>
    </Keyspace>
  </Keyspaces>

Next thing, I started cassandra-cli and wanted to populate the db with some
data:

set AdKit.Campaigns['campaigns']*['0']* = '{<JSON-encoded thing>}'

however, I get the following error

Exception null
InvalidRequestException(why:A long is exactly 8 bytes)
    at
org.apache.cassandra.service.Cassandra$insert_result.read(Cassandra.java:7804)
    at
org.apache.cassandra.service.Cassandra$Client.recv_insert(Cassandra.java:391)
    at
org.apache.cassandra.service.Cassandra$Client.insert(Cassandra.java:364)
    at org.apache.cassandra.cli.CliClient.executeSet(CliClient.java:205)
    at org.apache.cassandra.cli.CliClient.executeCLIStmt(CliClient.java:60)
    at org.apache.cassandra.cli.CliMain.processCLIStmt(CliMain.java:103)
    at org.apache.cassandra.cli.CliMain.main(CliMain.java:143)

but when I try this:

set AdKit.Campaigns['campaigns']*[0]* = '{<JSON-encoded thing>}'

I get another error

line 1:33 mismatched input '0' expecting StringLiteral
Exception null
InvalidRequestException(why:column name must not be empty)
    at
org.apache.cassandra.service.Cassandra$insert_result.read(Cassandra.java:7804)
    at
org.apache.cassandra.service.Cassandra$Client.recv_insert(Cassandra.java:391)
    at
org.apache.cassandra.service.Cassandra$Client.insert(Cassandra.java:364)
    at org.apache.cassandra.cli.CliClient.executeSet(CliClient.java:205)
    at org.apache.cassandra.cli.CliClient.executeCLIStmt(CliClient.java:60)
    at org.apache.cassandra.cli.CliMain.processCLIStmt(CliMain.java:103)
    at org.apache.cassandra.cli.CliMain.main(CliMain.java:143)

I wasn't able to find example with Long or Time keys... could somebody
enlighten me ?

 -- Sébastien

Re: Cassandra with LongType columns

Posted by Jonathan Ellis <jb...@gmail.com>.
yes

On Thu, Jan 21, 2010 at 4:02 PM, kevin <ke...@gmail.com> wrote:
>     return struct.pack('>q', n) # big endian = network order
>
> is this it?
>
> On Thu, Jan 21, 2010 at 1:59 PM, Jonathan Ellis <jb...@gmail.com> wrote:
>>
>> there's a few examples in test/system/test_server.py
>>
>> On Thu, Jan 21, 2010 at 3:58 PM, kevin <ke...@gmail.com> wrote:
>> > On Thu, Jan 21, 2010 at 1:51 PM, Jonathan Ellis <jb...@gmail.com>
>> > wrote:
>> >>
>> >> in python you would use struct.pack, fwiw
>> >>
>> > can you pl. give an example of this in python? i have been trying this
>> > for a
>> > while without success!
>> >
>
>

Re: Cassandra with LongType columns

Posted by kevin <ke...@gmail.com>.
    return struct.pack('>q', n) # big endian = network order

is this it?

On Thu, Jan 21, 2010 at 1:59 PM, Jonathan Ellis <jb...@gmail.com> wrote:

> there's a few examples in test/system/test_server.py
>
> On Thu, Jan 21, 2010 at 3:58 PM, kevin <ke...@gmail.com> wrote:
> > On Thu, Jan 21, 2010 at 1:51 PM, Jonathan Ellis <jb...@gmail.com>
> wrote:
> >>
> >> in python you would use struct.pack, fwiw
> >>
> > can you pl. give an example of this in python? i have been trying this
> for a
> > while without success!
> >
>

Re: Cassandra with LongType columns

Posted by Jonathan Ellis <jb...@gmail.com>.
there's a few examples in test/system/test_server.py

On Thu, Jan 21, 2010 at 3:58 PM, kevin <ke...@gmail.com> wrote:
> On Thu, Jan 21, 2010 at 1:51 PM, Jonathan Ellis <jb...@gmail.com> wrote:
>>
>> in python you would use struct.pack, fwiw
>>
> can you pl. give an example of this in python? i have been trying this for a
> while without success!
>

Re: Cassandra with LongType columns

Posted by kevin <ke...@gmail.com>.
On Thu, Jan 21, 2010 at 1:51 PM, Jonathan Ellis <jb...@gmail.com> wrote:

> in python you would use struct.pack, fwiw
>
> can you pl. give an example of this in python? i have been trying this for
a while without success!

Re: Cassandra with LongType columns

Posted by Jonathan Ellis <jb...@gmail.com>.
in python you would use struct.pack, fwiw

2010/1/21 Sébastien Pierre <se...@gmail.com>:
> Ah, OK !
> It appeared to me as quite a complex task just to send data, so I thought it
> was just not supported -- coming from a Python background I think I've
> become too lazy ;)
> Thanks,
>  -- Sébastien
>
> 2010/1/21 Jonathan Ellis <jb...@gmail.com>
>>
>> 2010/1/21 Sébastien Pierre <se...@gmail.com>:
>> > Hi Jonathan,
>> > Strangely, using the Java client I have no other option than using
>> > string,
>>
>> sure you do.  it takes a byte[], there are other ways of getting a
>> byte[] than String.getBytes.
>>
>> the easiest way to get a byte[] with the contents of a long is to use
>> a ByteBuffer.
>>
>> -Jonathan
>
>

Re: Cassandra with LongType columns

Posted by Sébastien Pierre <se...@gmail.com>.
Ah, OK !

It appeared to me as quite a complex task just to send data, so I thought it
was just not supported -- coming from a Python background I think I've
become too lazy ;)

Thanks,

 -- Sébastien

2010/1/21 Jonathan Ellis <jb...@gmail.com>

> 2010/1/21 Sébastien Pierre <se...@gmail.com>:
> > Hi Jonathan,
> > Strangely, using the Java client I have no other option than using
> string,
>
> sure you do.  it takes a byte[], there are other ways of getting a
> byte[] than String.getBytes.
>
> the easiest way to get a byte[] with the contents of a long is to use
> a ByteBuffer.
>
> -Jonathan
>

Re: Cassandra with LongType columns

Posted by Sébastien Pierre <se...@gmail.com>.
Hi Ted,

I had trouble with ByteBuffer but using your snippet with Scala worked fine,
thanks !

implicit def longAsByteArray(s:Long) = {
       val bos = new java.io.ByteArrayOutputStream();
       val dos = new java.io.DataOutputStream(bos);
       dos.writeLong(s);
       dos.flush();
       bos.toByteArray();
}

2010/1/21 Ted Zlatanov <tz...@lifelogs.com>

> On Thu, 21 Jan 2010 15:42:51 -0600 Jonathan Ellis <jb...@gmail.com>
> wrote:
>    public static byte[] getLongAsBytes(long timestamp) throws IOException
>    {
>        ByteArrayOutputStream bos = new ByteArrayOutputStream();
>        DataOutputStream dos = new DataOutputStream(bos);
>        dos.writeLong(timestamp);
>        dos.flush();
>        return bos.toByteArray();
>    }
>
>    public static Long getLongFromBytes(byte[] data)
>    {
>        ByteArrayInputStream bis = new ByteArrayInputStream(data);
>        DataInputStream dis = new DataInputStream(bis);
>        try
>        {
>            return dis.readLong();
>        }
>        catch (IOException e)
>        {
>            return null;
>        }
>    }
> ...
>
>

Re: Cassandra with LongType columns

Posted by Ted Zlatanov <tz...@lifelogs.com>.
On Thu, 21 Jan 2010 15:42:51 -0600 Jonathan Ellis <jb...@gmail.com> wrote: 

JE> 2010/1/21 Sébastien Pierre <se...@gmail.com>:
>> Hi Jonathan,
>> Strangely, using the Java client I have no other option than using string,

JE> sure you do.  it takes a byte[], there are other ways of getting a
JE> byte[] than String.getBytes.

JE> the easiest way to get a byte[] with the contents of a long is to use
JE> a ByteBuffer.

This is what I use in a wider utility class.  I don't know if it's the
best way but it works well for me.

Ted

...
    public static byte[] getLongAsBytes(long timestamp) throws IOException
    {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(bos);
        dos.writeLong(timestamp);
        dos.flush();
        return bos.toByteArray();
    }

    public static Long getLongFromBytes(byte[] data)
    {
        ByteArrayInputStream bis = new ByteArrayInputStream(data);
        DataInputStream dis = new DataInputStream(bis);
        try
        {
            return dis.readLong();
        }
        catch (IOException e)
        {
            return null;
        }
    }
...


Re: Cassandra with LongType columns

Posted by Jonathan Ellis <jb...@gmail.com>.
2010/1/21 Sébastien Pierre <se...@gmail.com>:
> Hi Jonathan,
> Strangely, using the Java client I have no other option than using string,

sure you do.  it takes a byte[], there are other ways of getting a
byte[] than String.getBytes.

the easiest way to get a byte[] with the contents of a long is to use
a ByteBuffer.

-Jonathan

Re: Cassandra with LongType columns

Posted by Sébastien Pierre <se...@gmail.com>.
Hi Jonathan,

Strangely, using the Java client I have no other option than using string,
and I end up with the same error:

Cassandra exception at put:InvalidRequestException(why:A long is exactly 8
bytes)

with the following (scala) code, where "campaign.id" is a Long:

client.insert("AdKit",
 "campaigns",
 new ColumnPath("Campaigns", null, (""+*campaign.id*).getBytes()),
 campaign.toJSON().getBytes("UTF-8"),
 System.currentTimeMillis(),
 ConsistencyLevel.ONE
 )

if I try to pass the long directly, here is what happens:

[ERROR]example.scala:107: error: type mismatch;
 found   : Long
 required: Array[Byte]
 new ColumnPath("Campaigns", null, campaign.id),

The cassandra service Client.insert method has no override for lon values,
which are only byte[] -- so I don't really know what to do.

 -- Sébastien



2010/1/21 Jonathan Ellis <jb...@gmail.com>

> cassandra-cli makes no attempt to deal with non-BytesType data types,
> you will have to use a "real" thrift client.
>
> 2010/1/21 Sébastien Pierre <se...@gmail.com>:
> > Hi !
> > So I moved forward using Cassandra for logging HTTP traffic, and I come
> up
> > with the following schema:
> >
> >   <Keyspaces>
> >     <Keyspace Name="AdKit">
> >       <ColumnFamily CompareWith="TimeUUIDType" Name="Logs"/>
> >       <ColumnFamily CompareWith="LongType"     Name="Campaigns"/>
> >     </Keyspace>
> >   </Keyspaces>
> >
> > Next thing, I started cassandra-cli and wanted to populate the db with
> some
> > data:
> >
> > set AdKit.Campaigns['campaigns']['0'] = '{<JSON-encoded thing>}'
> >
> > however, I get the following error
> >
> > Exception null
> > InvalidRequestException(why:A long is exactly 8 bytes)
> >     at
> >
> org.apache.cassandra.service.Cassandra$insert_result.read(Cassandra.java:7804)
> >     at
> >
> org.apache.cassandra.service.Cassandra$Client.recv_insert(Cassandra.java:391)
> >     at
> > org.apache.cassandra.service.Cassandra$Client.insert(Cassandra.java:364)
> >     at org.apache.cassandra.cli.CliClient.executeSet(CliClient.java:205)
> >     at
> org.apache.cassandra.cli.CliClient.executeCLIStmt(CliClient.java:60)
> >     at org.apache.cassandra.cli.CliMain.processCLIStmt(CliMain.java:103)
> >     at org.apache.cassandra.cli.CliMain.main(CliMain.java:143)
> >
> > but when I try this:
> >
> > set AdKit.Campaigns['campaigns'][0] = '{<JSON-encoded thing>}'
> >
> > I get another error
> >
> > line 1:33 mismatched input '0' expecting StringLiteral
> > Exception null
> > InvalidRequestException(why:column name must not be empty)
> >     at
> >
> org.apache.cassandra.service.Cassandra$insert_result.read(Cassandra.java:7804)
> >     at
> >
> org.apache.cassandra.service.Cassandra$Client.recv_insert(Cassandra.java:391)
> >     at
> > org.apache.cassandra.service.Cassandra$Client.insert(Cassandra.java:364)
> >     at org.apache.cassandra.cli.CliClient.executeSet(CliClient.java:205)
> >     at
> org.apache.cassandra.cli.CliClient.executeCLIStmt(CliClient.java:60)
> >     at org.apache.cassandra.cli.CliMain.processCLIStmt(CliMain.java:103)
> >     at org.apache.cassandra.cli.CliMain.main(CliMain.java:143)
> >
> > I wasn't able to find example with Long or Time keys... could somebody
> > enlighten me ?
> >
> >  -- Sébastien
> >
>

Re: Cassandra with LongType columns

Posted by Jonathan Ellis <jb...@gmail.com>.
cassandra-cli makes no attempt to deal with non-BytesType data types,
you will have to use a "real" thrift client.

2010/1/21 Sébastien Pierre <se...@gmail.com>:
> Hi !
> So I moved forward using Cassandra for logging HTTP traffic, and I come up
> with the following schema:
>
>   <Keyspaces>
>     <Keyspace Name="AdKit">
>       <ColumnFamily CompareWith="TimeUUIDType" Name="Logs"/>
>       <ColumnFamily CompareWith="LongType"     Name="Campaigns"/>
>     </Keyspace>
>   </Keyspaces>
>
> Next thing, I started cassandra-cli and wanted to populate the db with some
> data:
>
> set AdKit.Campaigns['campaigns']['0'] = '{<JSON-encoded thing>}'
>
> however, I get the following error
>
> Exception null
> InvalidRequestException(why:A long is exactly 8 bytes)
>     at
> org.apache.cassandra.service.Cassandra$insert_result.read(Cassandra.java:7804)
>     at
> org.apache.cassandra.service.Cassandra$Client.recv_insert(Cassandra.java:391)
>     at
> org.apache.cassandra.service.Cassandra$Client.insert(Cassandra.java:364)
>     at org.apache.cassandra.cli.CliClient.executeSet(CliClient.java:205)
>     at org.apache.cassandra.cli.CliClient.executeCLIStmt(CliClient.java:60)
>     at org.apache.cassandra.cli.CliMain.processCLIStmt(CliMain.java:103)
>     at org.apache.cassandra.cli.CliMain.main(CliMain.java:143)
>
> but when I try this:
>
> set AdKit.Campaigns['campaigns'][0] = '{<JSON-encoded thing>}'
>
> I get another error
>
> line 1:33 mismatched input '0' expecting StringLiteral
> Exception null
> InvalidRequestException(why:column name must not be empty)
>     at
> org.apache.cassandra.service.Cassandra$insert_result.read(Cassandra.java:7804)
>     at
> org.apache.cassandra.service.Cassandra$Client.recv_insert(Cassandra.java:391)
>     at
> org.apache.cassandra.service.Cassandra$Client.insert(Cassandra.java:364)
>     at org.apache.cassandra.cli.CliClient.executeSet(CliClient.java:205)
>     at org.apache.cassandra.cli.CliClient.executeCLIStmt(CliClient.java:60)
>     at org.apache.cassandra.cli.CliMain.processCLIStmt(CliMain.java:103)
>     at org.apache.cassandra.cli.CliMain.main(CliMain.java:143)
>
> I wasn't able to find example with Long or Time keys... could somebody
> enlighten me ?
>
>  -- Sébastien
>