Reputation: 4662
I'm new with Cassandra, and I want save some small file in to database, using python-cassandra
driver, but got an "Invalid STRING constant" error.
Table:
cqlsh:testkeyspace> desc table files_uuids;
CREATE TABLE files_uuids (
id uuid,
file blob,
PRIMARY KEY ((id))
) WITH
bloom_filter_fp_chance=0.010000 AND
caching='KEYS_ONLY' AND
comment='' AND
dclocal_read_repair_chance=0.100000 AND
gc_grace_seconds=864000 AND
index_interval=128 AND
read_repair_chance=0.000000 AND
replicate_on_write='true' AND
populate_io_cache_on_flush='false' AND
default_time_to_live=0 AND
speculative_retry='99.0PERCENTILE' AND
memtable_flush_period_in_ms=0 AND
compaction={'class': 'SizeTieredCompactionStrategy'} AND
compression={'sstable_compression': 'LZ4Compressor'};
And Python code:
>>> from cassandra.cluster import Cluster
>>> cluster = Cluster()
>>> session = cluster.connect('testkeyspace')
>>> import os, uuid
>>> file = os.path.join(os.getcwd(), 'file.txt')
>>> fid = uuid.uuid4()
>>> with open(file, 'rb') as f:
... data = f.read()
... session.execute("INSERT INTO files_uuids (id, file) values (%s, %s)", (fid, data))
...
Traceback (most recent call last):
File "<stdin>", line 3, in <module>
File "/usr/local/lib/python2.7/dist-packages/cassandra/cluster.py", line 1405, in execute
result = future.result(timeout)
File "/usr/local/lib/python2.7/dist-packages/cassandra/cluster.py", line 2976, in result
raise self._final_exception
cassandra.InvalidRequest: code=2200 [Invalid query] message="Invalid STRING constant (text
) for file of type blob"
What I'm doing wrong here?
Python 2.7, Cassandra 2.0.
Upvotes: 2
Views: 3240
Reputation: 4662
Got it... data = f.read()
is str()
(message="Invalid STRING constant [...]
).
So this will work:
>>> with open(file, 'rb') as f:
... data = f.read()
... res = bytearray(data)
... session.execute("INSERT INTO files_uuids (id, file) values (%s, %s)", (fid, res))
Result:
cqlsh:testkeyspace> select * from files_uuids;
id | file
--------------------------------------+--------------
6a918341-c3d9-48a1-96fe-9a4b2bc6ea51 | 0x746578740a
Upvotes: 2