Apache Cassandra Python inserting data

Viewed 153

I'm trying to insert data from a json file in Cassandra here's my code:

with open('../test.jsonl') as f:
    data = f.readlines()

for row in data:
    row = json.loads(row)
    insert_start = session.prepare(
        "INSERT INTO  player_session.startevents  (player_id,  event, country, session_id, ts) VALUES (?, ?, ?, ?, ?)")
    insert_end = session.prepare(
        "INSERT INTO  player_session.endevents  (player_id,  event, country, session_id, ts) VALUES (?, ?, ?, ?, ?)")
    if row['event'] == "start":
        session.execute(
            insert_start,
            [row['player_id'], row['event'], row['country'], row['session_id'], row['ts']]
        )
    if row['event'] == "end":
        session.execute(
            insert_end,
            [row['player_id'], row['event'],row['country'], row['session_id'], row['ts']]
        )
f.close()
print("data import complete") 

In my cassandra table the data_type of "ts" is a timestamp.

I'm getting this error :

line 17, in insert_data
    session.execute(
  File "cassandra/cluster.py", line 2618, in cassandra.cluster.Session.execute
  File "cassandra/cluster.py", line 2661, in cassandra.cluster.Session.execute_async
  File "cassandra/cluster.py", line 2864, in cassandra.cluster.Session._create_response_future
  File "cassandra/query.py", line 500, in cassandra.query.PreparedStatement.bind
  File "cassandra/query.py", line 631, in cassandra.query.BoundStatement.bind
TypeError: Received an argument of invalid type for column "ts". Expected: <class 'cassandra.cqltypes.DateType'>, Got: <class 'str'>; (DateType arguments must be a datetime, date, or timestamp)

If I insert the data without the prepare statement it's working

Thanks in advance

1 Answers

First - move the session.prepare calls outside of the loop. Second, the real problem is that row['ts'] is having string type, and you have date type in database. So you need to convert string into datetime or date using something like datetime.strptime.

Related