javascript - 如何使用 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 驱动程序,每个语句末尾的分号不是强制性的。
推荐阅读
- php - 在php中将CSS字符串转换为数组
- javascript - 如何在 Vue js 中解析 json 以在模板中使用它
- pandas - 如何使用熊猫中的列表对数据框进行排序
- javascript - 如何使用 echarts 库在每个条的左侧设置 x 轴刻度?
- c++ - 在 C++ 中加速读取/处理日志文件
- node.js - {URL} 处的 net::ERR_ADDRESS_UNREACHABLE
- json - SwiftUI - 来自 GitHub REST API 的 JSON 无法解析
- python - 为 python 安装 dlib 模块时出现问题?
- javascript - 当表单保存在php中时检查数据库中的表
- php - 像在文件资源管理器中一样获取文件夹和文件