如何在WHERE子句中通过IN运算符使用值列表?

时间:2019-06-20 22:14:35

标签: 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');

2 个答案:

答案 0 :(得分:2)

我以相同版本复制了此内容: [cqlsh 3.1.8 |卡桑德拉1.2.19 | CQL规范3.0.5 |节俭协议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 |卡桑德拉2.1.21 | CQL规范3.2.1 |原生协议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']);
});

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

答案 1 :(得分:1)

这个问题在FAQ中,而不是在实际调用的文档中,而且我在StackOverflow上找不到它很容易,所以我想在这里复制它:

引用https://docs.datastax.com/en/developer/nodejs-driver/4.1/faq/#how-can-i-use-a-list-of-values-with-the-in-operator-in-a-where-clause

  

在查询中使用IN运算符,后接问号占位符,且不带括号。包含值列表的参数应该是Array的实例。