如何使用Node.js Cassandra驱动程序运行连续查询?

问题描述

我正在尝试使用Node.js中的Cassandra驱动程序运行此脚本:

CREATE KEYSPACE IF NOT EXISTS "user"
WITH REPLICATION = {
'class': 'SimpleStrategy','replication_factor': 1
}
AND DURABLE_WRITES = false;

USE "user";

CREATE TYPE IF NOT EXISTS "user"."customType"(
"name" text,"isGood" boolean,);

但这会返回下一个错误

[USE]...)rror: line 8:0 mismatched input 'USE' expecting EOF (...AND DURABLE_WRITES = false;

我认为问题出在不同的查询中。但是吗?那有什么解决方案?

更新

批量查询不起作用。取得这个Invalid statement in batch: only UPDATE,INSERT and DELETE statements are allowed

解决方法

CQL驱动程序仅支持一次执行一条语句。

因此,在您的情况下,它将类似于:

async function createSchema() {
  await client.execute('CREATE KEYSPACE user ...');
  await client.execute('USE user');
  await client.execute('CREATE TYPE IF NOT EXISTS ...');
}

或者您可以在架构中包含一个大字符串,并使用JavaScript进行一些拆分:

async function createSchema(schema) {
  const queries = schema.split(';');
  for (const query of queries) {
    await client.execute(query);
  }
}

请注意,对于CQL驱动程序,每个语句末尾的分号不是必需的。