Как я могу использовать список значений с оператором IN в предложении WHERE? - PullRequest
2 голосов
/ 21 июня 2019

Я пытаюсь выполнить такой запрос, используя драйвер 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 ]

2 голосов
/ 28 июня 2019

Я воспроизвел это в той же версии: [cqlsh 3.1.8 | Кассандра 1.2.19 | CQL spec 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 spec 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']);
});

Поскольку драйвер 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?

1 голос
/ 21 июня 2019

Этот вопрос находится в 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.

...