Apache Cassandra Python 插入数据

Apache Cassandra Python inserting data

我正在尝试从 Cassandra 中的 json 文件插入数据,这是我的代码:

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") 

在我的 cassandra table 中,“ts”的 data_type 是一个时间戳。

我遇到了这个错误:

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)

如果我在没有 prepare 语句的情况下插入数据,它就可以工作

提前致谢

首先 - 将 session.prepare 调用移出循环。其次,真正的问题是 row['ts'] 是字符串类型,而你在数据库中有 date 类型。因此,您需要使用 datetime.strptime.

之类的方法将字符串转换为 datetimedate