Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pycassa TypeError: A str or unicode, unable to do cassandra insert

My scripting is python and cassandra is data stax community edition.

TypeError: A str or unicode value was expected, but int was received instead (3902503)

this is the error I'm getting while trying to insert into a cassandra column family.

the code is like:

for x in feed:
    cf.insert(uuid.uuid4(), x)

x is a simple array in the form of "{key:value}"

The error log suggests:

    Traceback (most recent call last):
      File "C:\Users\me\Desktop\pro1\src\pro1.py", line 73, in <module>
        str("swf"): str("aws")
      File "c:\Python27\lib\site-packages\pycassa\columnfamily.py", line 969, in insert
        mut_list = self._make_mutation_list(columns, timestamp, ttl)
      File "c:\Python27\lib\site-packages\pycassa\columnfamily.py", line 504, in _make_mutation_list
        columns.iteritems())
      File "c:\Python27\lib\site-packages\pycassa\columnfamily.py", line 503, in <lambda>
        return map(lambda (c, v): Mutation(self._make_cosc(_pack_name(c), _pack_value(v, c), timestamp, ttl)),
      File "c:\Python27\lib\site-packages\pycassa\columnfamily.py", line 462, in _pack_value
        return packer(value)
      File "c:\Python27\lib\site-packages\pycassa\marshal.py", line 231, in pack_bytes
        % (v.__class__.__name__, str(v)))
    TypeError: A str or unicode value was expected, but int was received instead (3902503)        

There seems to be something very minute I'm missing here... well thats why I came here to ask experts!

like image 554
yuvrajm Avatar asked Oct 06 '22 15:10

yuvrajm


1 Answers

Make sure your values match your column family type. It appears your column family either is a BytesType or has no type associated with it, so pycassa will only accept string values. You can map all your values to str with a list comprehension with a nested dict comprehension (the latter requires python 2.7 and up):

cf.insert(uuid.uuid4(), [{k: str(v) for k, v in d.iteritems()} for d in x])
like image 143
Martijn Pieters Avatar answered Oct 13 '22 10:10

Martijn Pieters