2

I am using the python Cassandra driver to query a database. This is the code that I ran:

from cassandra.cluster import Cluster
from cassandra.query import dict_factory

ips = [...]
cluster = Cluster(ips)
session = cluster.connect()
session.row_factory = dict_factory
session.set_keyspace("mykeyspace")
response = session.execute("SELECT * FROM myDB WHERE mykey = 'xyz';")

In the output, I get weird 'n's in front of words, where newline characters used to be.

Example:

"Over the wintry nforest, winds howl in rage nwith no leaves to blow."

Is there a way to solve this issue?

pajamas
  • 1,194
  • 1
  • 12
  • 25
  • try *response = session.execute("SELECT * FROM myDB WHERE mykey = 'xyz';").rstrip('\n')* – Joao Vitorino Nov 10 '17 at 14:03
  • does not work, because **response** is of type and I tried `for line in response: line['mycolumn'].strip('\n')` that did not wor either – pajamas Nov 10 '17 at 14:28
  • can you include your schema and how they were inserted? – Chris Lohfink Nov 10 '17 at 14:31
  • @Chris Lohfink I don't have that info. Is there a way I can get that using the python driver? – pajamas Nov 10 '17 at 14:39
  • how did you put the data into cassandra? Issue is more likely there, C* doesnt do anything to the data, just store and return it. – Chris Lohfink Nov 10 '17 at 15:03
  • 1
    Can you do a `":".join("{:02x}".format(ord(c)) for c in line['mycolumn'])` so we can check that it is a \n? – Chris Lohfink Nov 10 '17 at 15:08
  • so this is the string **die nPolitik** and this is the output **64:69:65:20:6e:50:6f:6c:69:74:69:6b** – pajamas Nov 10 '17 at 15:24
  • so thats literally "n" that was inserted, not \n. Probably whatever you were using to insert the data was escaping it first and removed the \, not rendering as the new line char. – Chris Lohfink Nov 10 '17 at 21:21
  • I tried it with the java driver, and in the output one can clearly see the correct result with "\n". So I guess the problem is with the python driver. Is there a way to tell the python driver not to escape/unescape items when it is getting the data? – pajamas Nov 11 '17 at 01:17

1 Answers1

0

The issue is in your data ingestion, or whatever is consuming response not in the drivers reading. It is the n in stored your database, not the \n char. The python driver does not escape or do anything from the raw bytes but convert to string: https://github.com/datastax/python-driver/blob/9869c2a044e5dd76309026437bcda5d01acf99c7/cassandra/cqltypes.py#L693

If you insert a \n you get one back out:

session.execute('CREATE TABLE mydb (mykey text PRIMARY KEY, mycolumn text);')
session.execute("INSERT INTO mydb (mykey, mycolumn) VALUES ('xyz', 'a\nb');")
response = session.execute("SELECT * FROM myDB WHERE mykey = 'xyz';")
for line in response:
   print line['mycolumn']

correctly outputs:

a
b

Is there something taking response and escaping it afterwards?

Chris Lohfink
  • 16,150
  • 1
  • 29
  • 38