在Cassandra的两个表上选择 [英] selecting on two tables in Cassandra
问题描述
我在项目中使用 Cassandra
,这是我的第一个项目。
,而我试图在两个表上做一个简单的请求,但这不起作用...
I use Cassandra
for a project, and it's my first project.
, and I'm trying to do a simple request on two tables, but that doesn't work...
我想做类似的事情:
从table1,table2中选择*,其中table1.test = test和table2.test2 = 123;
是否可以在 Cassandra
中的两个表上进行请求?我该怎么办呢?
Is it possible to request on two tables in Cassandra
? And how can I do that?
谢谢
推荐答案
I' m试图在两个表上做一个简单的请求
I'm trying to do a simple request on two tables
您要执行的操作被称为分布式联接和Cassandra
What you're trying to do is known as a "distributed join" and Cassandra is specifically designed to prevent you from doing this.
解决此类问题的方法是使用称为 denormalization 的过程。假设您有两个简单的表 carMake
和 carModel
:
The way to solve these types of problems, is with a process called denormalization. Let's say you have simple two tables carMake
and carModel
:
makeid | make
--------+--------
1 | Chevy
2 | Dodge
3 | Ford
modelid | makeid | model
---------+--------+---------
15 | 3 | Focus
11 | 3 | Mustang
32 | 2 | Charger
82 | 3 | Fusion
现在,在传统的RDBMS中,如果我想选择所有带有福特字样的车型我将执行一个JOIN查询。但是,对于Cassandra来说,其想法是在建模阶段通过构建一个支持查询汽车制造商和车型的表格来解决这个问题:
Now, in a traditional RDBMS if I wanted to SELECT all car models with a make of "Ford" I would execute a JOIN query. But with Cassandra, the idea is to solve this problem at the modeling stage, by building a table which supports the ability to query make and model of a car at the same time:
CREATE TABLE carMakeModel (
carid int,
make text,
model text,
PRIMARY KEY (make,carid));
aploetz@cqlsh:stackoverflow> SELECT * FROM carMakeModel WHERE make='Ford';
make | carid | model
------+-------+---------
Ford | 1 | Mustang
Ford | 2 | Focus
Ford | 3 | Fusion
(3 rows)
此处需要注意的一些关键点:
Some key points to note here:
-
make
被重复多次。您会注意到,结果集中指定了3次福特。如果您有13种福特汽车的数据,则将福特的值存储13次。 - Cassandra中的主键是唯一的。我在主键中添加了
carid
以确保每个模型
的唯一性,否则为每个make
会覆盖自己。
make
is duplicated as much as is necessary. You'll notice that "Ford" is specified 3 times in the result set. If you had data for 13 models of Fords, you would store the value of "Ford" 13 times.- PRIMARY KEYs in Cassandra are unique. I have
carid
added as a part of the PRIMARY KEY to ensure uniqueness for eachmodel
, otherwise an INSERT for eachmake
would overwrite itself.
这篇关于在Cassandra的两个表上选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!