卡桑德拉 "no viable alternative at input"

Cassandra "no viable alternative at input"

我正在尝试向 table 中插入一个简单的行。有人可以指出这里发生了什么吗?

CREATE TABLE recommendation_engine_poc.user_by_category (
        game_category text,
        customer_id text,
        amount double,
        game_date timestamp,
        PRIMARY KEY (game_category, customer_id)
    ) WITH CLUSTERING ORDER BY (customer_id ASC)
        AND bloom_filter_fp_chance = 0.01
        AND caching = '{"keys":"ALL", "rows_per_partition":"NONE"}'
        AND comment = ''
        AND compaction = {'class': 'org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy'}
        AND compression = {'sstable_compression': 'org.apache.cassandra.io.compress.LZ4Compressor'}
        AND dclocal_read_repair_chance = 0.1
        AND default_time_to_live = 0
        AND gc_grace_seconds = 864000
        AND max_index_interval = 2048
        AND memtable_flush_period_in_ms = 0
        AND min_index_interval = 128
        AND read_repair_chance = 0.0
        AND speculative_retry = '99.0PERCENTILE';

    cqlsh:recommendation_engine_poc> insert into user_by_category  ('game_category','customer_id') VALUES ('Goku','12') ;
    SyntaxException: <ErrorMessage code=2000 [Syntax error in CQL query] message="line 1:31 no viable alternative at input 'game_category' (insert into user_by_category  (['game_categor]...)">

语法错误。你在这里:

insert into user_by_category (game_category,customer_id) VALUES ('Goku','12');

或:

insert into user_by_category ("game_category","customer_id") VALUES ('Kakarot','12');

第二个通常用于区分大小写的列名。

这个问题有两个原因,

  1. 双引号 (") 而不是单引号 (')
  2. 在 IN 命令中的值后输入键
  3. 字符编码

所以在使用像 IN 命令这样的过滤器时
Select 列来自数据库,其中 column_name IN('xyz','yzx')
单行 IN 命令

应该是单行而不是
Select 列来自数据库,其中 column_name IN('xyz',
'yzx')
多行 IN 命令

当使用占位符(无论是为了准备语句,还是只是为了使用字典来填充值)时,可能会出现另一个问题:无论您放入字典中的任何内容都可能无法计算为像 int 这样的简单类型, str 等。对我来说,这发生在 IntEnum 上,它显示为 <MyIntEnum.CONSTANT: 4> 而不是 4,但它可能发生在任何复杂的对象上,然后看起来像 <object object at 0x7fbf08a0bdf0>

因此,如果您使用以下形式:

session.execute(
        INSERT INTO table (id, name)
        VALUES (%(id)s, %(name)s),
        {"id": 123456, "name": "example"}
)

确保字典中的所有内容实际上都是字符串、整数、浮点数等

这不是这个问题的问题,但由于它是另一个错误的编码,这是第一个 Google 命中的问题,我希望这对某人有所帮助:)