Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert map type into cassandra using cassandra-driver for python

Since, cassandra supports map type. I want to insert a python dict into cassandra. I tried this:

cql = "Insert into table_name (my_key, name, my_dict) values (%s, %s, %s)" % (my_key, name, my_dict)

session.execute(cql)

This obviously didn't work.

I already have a map type column in my column family. How do I go about it?

Error: TypeError: not all arguments converted during string formatting

like image 549
extraDarker Avatar asked Oct 04 '14 09:10

extraDarker


People also ask

What is mapping in Cassandra?

A map is a name and a pair of typed values. Using the map type, you can store timestamp-related information in user profiles. Each element of the map is internally stored as one Cassandra column that you can modify, replace, delete, and query.

Can we use Cassandra with Python?

Python module for working with Cassandra database is called Cassandra Driver. It is also developed by Apache foundation. This module contains an ORM API, as well as a core API similar in nature to DB-API for relational databases. Installation of Cassandra driver is easily done using pip utility.


1 Answers

Use parameter passing form instead of string interpolation:

cql = "Insert into table_name (my_key, name, my_dict) values (%s, %s, %s)"
session.execute(cql,  (my_key, name, my_dict))
like image 121
falsetru Avatar answered Oct 11 '22 11:10

falsetru