Re: pycassa : composite key and UTF8Type ,DateType

59 views
Skip to first unread message

Tyler Hobbs

unread,
Mar 7, 2013, 11:49:00 AM3/7/13
to us...@cassandra.apache.org, pycassa...@googlegroups.com

FYI, there's a mailing list that's dedicated to pycassa: https://groups.google.com/forum/?fromgroups#!forum/pycassa-discuss

On Thu, Mar 7, 2013 at 8:13 AM, Sloot, Hans-Peter <hans-pet...@atos.net> wrote:

I have a tab separatd file with a number of columns.

Columns 5 and 6 are the date as yyyy-mm-dd  and hh24:mi:ss

 

I want to add rows with a composite key that consists of an UTF8Type and the DateType (which are fields 5 and 6).

 

The column family is :

CREATE COLUMN FAMILY traffic

     WITH comparator = 'CompositeType(UTF8Type,UTF8Type)' and key_validation_class='CompositeType(UTF8Type,DateType)'

     AND column_metadata = [ {column_name: fac, validation_class: UTF8Type, index_type : 0, index_name : fac_idx }]

 

How can I insert rows with a composite key : field 0  + the combined fields 5 and 6 of the rows.

Below a small piece of the code:

 

b=cf.batch(queue_size=10000)

10000 is pretty high, I would start out with 100.
 

with open (‘xxx.csv’) as csvfile:

  spamreader=csv.reader(csvfile,delimiter='\t')

 for row in spamreader:

     b.insert(   )   ß how should the insert be done?

Build your key as described below and put the rest of the fields into a dict.  Then all you need to do is b.insert(key, my_columns_dict).
 

b.send()

 

And more in general how can I cast a string to DateType?

pycassa can accept datetime objects for DateType fields, so just use datetime.strptime to create a datetime object from the string: http://docs.python.org/2/library/datetime.html#datetime.datetime.strptime

Make sure your datetime objects have a UTC timezone, though.


--
Tyler Hobbs
DataStax

hanspet...@gmail.com

unread,
Mar 8, 2013, 6:26:20 AM3/8/13
to pycassa...@googlegroups.com, us...@cassandra.apache.org
Hi,

Ok I understand now how to deal with the DateType.
But how can I create a composite key or composite column?
I am using Pycassa 1.7.2 with Datastax Cassandra 2.1.

Regards Hans-Peter

Op donderdag 7 maart 2013 17:49:00 UTC+1 schreef Tyler Hobbs het volgende:

hanspet...@gmail.com

unread,
Mar 8, 2013, 9:14:18 AM3/8/13
to pycassa...@googlegroups.com, us...@cassandra.apache.org
Hi

I have come this far.
     x=datetime.datetime.strptime(t,'%Y-%m-%d %H:%M:%S')
     compositeKey=("nodename1",x)
     b.insert(compositeKey, { 'fac' : 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', 'sev': 'aaaa'})

But if I select the columnfamily it only shows the keys and nulls for the other keys

cqlsh:demo> select key, fac, sev from traffic2 limit 1;
 key                                                        | fac        | sev
------------------------------------------------------------+--------+--------
 \x00\x08nlvora13\x00\x00\x08\x00\x00\x01=\x10\xc1f\x98\x00 |   null |   null


The columnfamily is created as:
 CREATE COLUMN FAMILY traffic2  
  WITH comparator = 'CompositeType(UTF8Type,UTF8Type)' and key_validation_class='CompositeType(UTF8Type,DateType)'
 AND column_metadata = [
    {column_name: fac, validation_class: UTF8Type, index_type : 0, index_name : fac_idx }
    {column_name: sev, validation_class: UTF8Type, index_type : 0, index_name : sev_idx }
          ]

Any clues?


Regards Hans-Peter

Op donderdag 7 maart 2013 17:49:00 UTC+1 schreef Tyler Hobbs het volgende:
Reply all
Reply to author
Forward
0 new messages