首页 > 解决方案 > 有没有办法在 cassandra 中索引地图类型列

问题描述

我有一个表订阅者​​,它将包含数百万个数据

表模式在 cassandra 中如下所示 -

CREATE TABLE susbcriber (
    id int PRIMARY KEY,
    age_identifier text,
    alternate_mobile_identifier text,
    android_identifier text,
    batch_id text,
    circle text,
    city_identifier text,
    country text,
    country_identifier text,
    created_at text,
    deleted_at text,
    email_identifier text,
    gender_identifier text,
    ios_identifier text,
    list_master_id int,
    list_subscriber_id text,
    mobile_identifier text,
    operator text,
    partition_id text,
    raw_data map<text, text>,
    region_identifier text,
    unique_identifier text,
    updated_at text,
    web_push_identifier text
) WITH 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', 'max_threshold': '32', 'min_threshold': '4'}
    AND compression = {'chunk_length_in_kb': '64', 'class': 'org.apache.cassandra.io.compress.LZ4Compressor'}
    AND crc_check_chance = 1.0
    AND dclocal_read_repair_chance = 0.1
    AND default_time_to_live = 0
    AND gc_grace_seconds = 0
    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 = '99PERCENTILE';

我必须在'raw_data map<text, text>,'此列上进行过滤查询,主要包含 JSON 值和键,我如何对数据进行建模以便选择和更新必须在性能上快速

我正在尝试实现一些批量更新操作。

任何建议都受到高度赞赏。

标签: cassandradata-modelingcassandra-3.0cqlsh

解决方案


如果数据已经在地图中,您实际上也不需要将值保留在自己的列中,并且如果它只是地图的键,则在 cassandra 上更容易将其表示为集群键,而不是像这样的集合:

CREATE TABLE susbcriber_data (
    id int,
    key text,
    value text,
    PRIMARY KEY((id), key))

然后你可以通过任何 id 和 key 进行查询。如果您正在寻找特定键的值

CREATE TABLE susbcriber_data_by_value (
    id int,
    shard int,
    key text,
    value text,
    PRIMARY KEY((key, shard), value, id))

然后,当您插入时,您将 shard 设置为id % 12或某个值,以使您的分区不会变大(需要根据预期负载进行一些猜测)。然后要查看 key = value 的所有值,您需要查询所有 12 个这些分片(对每个分片进行异步调用并合并)。尽管如果您的键/值对的基数足够低,则可能不需要分片。然后,您将获得一个可以查找的 id 列表。如果您想避免查找,您可以向该表添加一个额外的键和值,但您的数据可能会爆炸很多,具体取决于您在地图中拥有的键的数量,并且保持所有内容更新将是痛苦的。

我不推荐但可用的一个选项是索引地图,即:

CREATE INDEX raw_data_idx ON susbcriber ( ENTRIES (raw_data) );

SELECT * FROM susbcriber WHERE raw_data['ios_identifier'] = 'id';

牢记二级索引的问题


推荐阅读