cassandra - 如何在 WHERE 子句中使用带有 IN 运算符的值列表?

标签 cassandra cql

我正在尝试使用 JavaScript 驱动程序执行这样的查询:

client.execute('SELECT * FROM zhos_dev.jw_testing WHERE a IN ?', [['foo', 'foo1']], {prepare: true})

它给了我:ResponseError: line 0:-1 mismatched input '<EOF>' expecting ')' .

我的版本是:[cqlsh 3.1.8 | Cassandra 1.2.19 | CQL spec 3.0.5 | Thrift protocol 19.36.2]

该表已创建并填充了以下 CQL:

CREATE TABLE zhos_dev.jw_testing (a text PRIMARY KEY, b text);
INSERT INTO zhos_dev.jw_testing (a, b) VALUES ('foo', 'bar');
INSERT INTO zhos_dev.jw_testing (a, b) VALUES ('foo1', 'bar1');
INSERT INTO zhos_dev.jw_testing (a, b) VALUES ('foo2', 'bar2');

最佳答案

我在同一版本中重现了此内容: [cqlsh 3.1.8 | Cassandra 1.2.19 | CQL 规范 3.0.5 | Thrift协议(protocol)19.36.2]

  name: 'ResponseError',
  info: 'Represents an error message from the server',
  message: "line 0:-1 mismatched input '<EOF>' expecting ')'",
  code: 8192,
  query: 'SELECT name, color FROM keyspace1.dresses WHERE id IN ?'

“IN”子句早在 Cassandra 2.1 中就可以正常工作: [cqlsh 5.0.1 | Cassandra 2.1.21 | CQL 规范 3.2.1 |原生协议(protocol)v3]

Connected to cluster with 1 host(s): ["127.0.0.1:9042"]
Azul Dress

测试表/数据:

CREATE KEYSPACE keyspace1 WITH replication = {'class':'SimpleStrategy', 'replication_factor' : 1 };

CREATE TABLE keyspace1.dresses (id text, color text, name text, size int, PRIMARY KEY (id, color));

insert into dresses (id, color, name, size) values ('mon1', 'blue', 'Blue Dress', 12);
insert into dresses (id, color, name, size) values ('mon2', 'blue', 'Azul Dress', 12);
insert into dresses (id, color, name, size) values ('can1', 'green', 'Green Dress', 12);
insert into dresses (id, color, name, size) values ('can2', 'verde', 'Verde Dress', 12);

和代码:

const cassandra = require('cassandra-driver');

const client = new cassandra.Client({ contactPoints: ['127.0.0.1'], localDataCenter: 'datacenter1' });
client.connect(function (err) {
  if (err) return console.error(err);
  console.log('Connected to cluster with %d host(s): %j', client.hosts.length, client.hosts.keys());
});

client.execute('SELECT name, color FROM keyspace1.dresses WHERE id IN ?', [ ['mon2'] ], 
    { prepare: true}, 
    function (err, result) {
          if (err) return console.error(err);
          const row = result.first();
          console.log(row['name']);
});

由于 Node.js 驱动程序仅在 Cassandra 2.1 及更高版本 ( https://docs.datastax.com/en/developer/nodejs-driver/4.1/faq/#which-versions-of-cassandra-does-the-driver-support ) 中受支持,因此我认为 Cassandra 或驱动程序项目不会接受错误请求。您可以升级到至少 2.1 吗?

关于cassandra - 如何在 WHERE 子句中使用带有 IN 运算符的值列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56694545/

相关文章:

linux - 使用 chkservd 的 cassandra nosql 自动重启服务

java - 使用 Cassandra 和 Solr 的网站最有效的架构?

cassandra - CQL SELECT 无法处理复合列的 "in"

cassandra - 将空值插入cassandra

database - 根据Cassandra中的查询结果创建新表

cassandra - CQL3现在是否需要Cassandra的架构?

database - CQL 相当于 : select SYSTIMESTAMP from dual;?

java - 无法以编程方式将 Spark 应用程序(使用 Cassandra 连接器)从远程客户端提交到集群

java - 使用datastax java驱动程序异步写入cassandra的有效方法?

Cassandra : Cannot replace address with a node that is already bootstrapped