首页 > 解决方案 > 如何使用 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

标签: javascriptnode.jsdatabasecassandracql

解决方案


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 驱动程序,每个语句末尾的分号不是强制性的。


推荐阅读