Python3和Cassandra Insert使用UDT,KeyError:0

dba5bblo  于 2023-11-18  发布在  Cassandra
关注(0)|答案(1)|浏览(123)

我正在开发一个应用程序与Python 3和Apache Cassandra数据库为我的后端,但使用UDT的Cassandra,我有一个问题,下面的代码,所以我尝试了一切,并试图修补的东西,但它没有做得很好,但说KeyError: 0位于dbsession.execute(insert_stat, insert_var) .所以我print()queryinsert_statinsert_var,但我没有看到任何问题,也没有在日志中提到它如何导致KeyError。
Python代码:

query = "INSERT INTO accounts (id, displayname, username, email, joined, status, password, bio, pinnedupdate, interest, notice, details, connection, purge) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);"

insert_stat = dbsession.prepare(query)

insert_var = (
 userid,
 limit_string_length(req['username'], 20),
 limit_string_length(username_string(req['username']), 20),
 [req['email']],
 int(datetime.now().timestamp())*1000,
 'public',
 str(bcrypt.hashpw(password_bytes, bcrypt.gensalt(12)).decode('utf-8')),
 '',
 None,
 {
 'fields': [],
 'tags': [],
 'topics': []
 },
 {
 'acknowledged': True,
 'code': 0,
 'customproflock': False,
 'dmlock': False,
 'suspendreason': '',
 'suspended': False,
 'updatephotolock': False,
 'updatelock': False,
 'updatevideolock': False
 },
 {
 'emailapproved': False,
 'labels': [],
 'likesprivate': False,
 'private': False,
 'pronouns': '',
 'verified': False,
 'website': ''
 },
 [],
 {
 'status': 'public',
 'reason': '',
 'schedule': int(datetime.now().timestamp())*1000
 }
)

dbsession.execute(insert_stat, insert_var)

字符串
Cassandra结构:

CREATE TYPE account_interest (
  topics list<text>,
  fields list<text>,
  tags list<text>,
);

CREATE TYPE account_notice (
  code int,
  updatelock boolean,
  updatephotolock boolean,
  updatevideolock boolean,
  customproflock boolean,
  dmlock boolean,
  suspended boolean,
  suspendreason text,
  acknowledged boolean,
);

CREATE TYPE account_detail (
  verified boolean,
  private boolean,
  website text,
  emailapproved boolean,
  pronouns text,
  labels list<text>,
  likesprivate boolean,
);

CREATE TYPE connection (
  application text,
  tokens list<text>,
);

CREATE TYPE purge (
  status text,
  reason text,
  schedule timestamp,
);

CREATE TABLE accounts (
  id uuid,
  username varchar,
  displayname text,
  email set<text>,
  status varchar,
  joined timestamp,
  bio text,
  password varchar,
  pinnedupdate uuid,
  interest FROZEN<account_interest>,
  notice FROZEN<account_notice>,
  details FROZEN<account_detail>,
  connection set<FROZEN<connection>>,
  purge FROZEN<purge>,
  PRIMARY KEY (id)
);

hfyxw5xn

hfyxw5xn1#

我相信Erick Ramirez之前已经回答了这个问题,答案是使用一个简单的类,它的结构与UDT相同,你可以在这里读到:Batch insert fails for table with a CQL map and nested UDT如果这不能让你继续下去,请联系。

相关问题