感谢您抽出宝贵时间来研究我的问题。我和我的团队刚刚开始涉足NodeJS,这是我们第一次遇到一个问题,到目前为止我们找不到自己的解决方案。
简单地说,我们有一个需要使用UUID主键查询Cassandra的Controller。
控制器:
'use strict';
const express = require('express');
const models = require('../dbConnection');
const router = express.Router();
var UUID = require('uuid-js');
// Store / create checklist definition.
router.post('/', function(req, res) {
});
// Show checklist definition.
router.get('/:id', function(req, res) {
let checklistId = req.params.id;
console.log(checklistId);
models.instance.ChecklistDefinition.findOne({id: checklistId}, function(err, checklist) {
if (err) {
console.log(err);
return;
}
res.json(checklist);
});
});
// Update checklist definition.
router.put('/:id', function(req, res) {
});
// Delete checklist definition.
router.delete('/:id', function(req, res) {
});
module.exports = router;
型号:
module.exports = {
fields: {
id : "uuid",
client_id : "int",
name : "text"
},
key : ["id"],
indexes: ["name"],
table_name: "checklist_definition"
};
数据库连接:
const models = require('express-cassandra');
//Tell express-cassandra to use the models-directory, and
//use bind() to load the models using cassandra configurations.
models.setDirectory( __dirname + '/models').bind(
{
clientOptions: {
contactPoints: ['127.0.0.1'],
protocolOptions: { port: 9042 },
keyspace: 'audit_checklist',
queryOptions: {consistency: models.consistencies.one}
},
ormOptions: {
//If your keyspace doesn't exist it will be created automatically
//using the default replication strategy provided here.
defaultReplicationStrategy : {
class: 'SimpleStrategy',
replication_factor: 1
},
createKeyspace: true
}
},
function(err) {
if(err) console.log(err.message);
else console.log(models.timeuuid());
}
);
module.exports = models;
Cassandra Keyspace架构:
CREATE KEYSPACE audit_checklist
WITH durable_writes = true
AND replication = {
'class' : 'SimpleStrategy',
'replication_factor' : 1
};
CREATE TABLE audit_checklist.checklist_definition (
id uuid,
client_id int,
name text,
PRIMARY KEY (id)
) WITH bloom_filter_fp_chance = 0.01
AND comment = ''
AND crc_check_chance = 1.0
AND dclocal_read_repair_chance = 0.1
AND default_time_to_live = 0
AND gc_grace_seconds = 864000
AND max_index_interval = 2048
AND memtable_flush_period_in_ms = 0
AND min_index_interval = 128
AND read_repair_chance = 0.0
AND speculative_retry = '99.0PERCENTILE'
AND caching = {
'keys' : 'ALL',
'rows_per_partition' : 'NONE'
}
AND compression = {
'chunk_length_in_kb' : 64,
'class' : 'LZ4Compressor',
'enabled' : true
}
AND compaction = {
'class' : 'SizeTieredCompactionStrategy',
'max_threshold' : 32,
'min_threshold' : 4
};
CREATE INDEX checklist_definition_name_index ON audit_checklist.checklist_definition (name);
在models.instance.ChecklistDefinition.findOne
行上,我无法弄清楚如何获取checklistId
(URL中的UUID)来正确查询Cassandra表。我想我必须先对checklistId
做一些事情才能使用它与Cassandra中此表的id
列中的内容进行比较。
我得到的错误是:
apollo.model.validator.invalidvalue: Invalid Value: "6c84fb90-12c4-11e1-840d-7b25c5ee775a" for Field: id (Type: uuid)
at f (/Users/shanejeffery/Desktop/compli-docker/audit-checklist/node_modules/express-cassandra/lib/orm/apollo_error.js:174:15)
at Function.f [as _get_db_value_expression] (/Users/shanejeffery/Desktop/compli-docker/audit-checklist/node_modules/express-cassandra/lib/orm/base_model.js:999:11)
at /Users/shanejeffery/Desktop/compli-docker/audit-checklist/node_modules/express-cassandra/lib/orm/base_model.js:1144:34
at Array.forEach (native)
at Function.f [as _create_where_clause] (/Users/shanejeffery/Desktop/compli-docker/audit-checklist/node_modules/express-cassandra/lib/orm/base_model.js:1018:28)
at Function.f [as _create_find_query] (/Users/shanejeffery/Desktop/compli-docker/audit-checklist/node_modules/express-cassandra/lib/orm/base_model.js:1196:26)
at Function.f [as find] (/Users/shanejeffery/Desktop/compli-docker/audit-checklist/node_modules/express-cassandra/lib/orm/base_model.js:1482:26)
at Function.f [as findOne] (/Users/shanejeffery/Desktop/compli-docker/audit-checklist/node_modules/express-cassandra/lib/orm/base_model.js:1541:15)
at /Users/shanejeffery/Desktop/compli-docker/audit-checklist/controllers/ChecklistDefinitionController.js:21:41
at Layer.handle [as handle_request] (/Users/shanejeffery/Desktop/compli-docker/audit-checklist/node_modules/express/lib/router/layer.js:95:5)
at next (/Users/shanejeffery/Desktop/compli-docker/audit-checklist/node_modules/express/lib/router/route.js:131:13)
at Route.dispatch (/Users/shanejeffery/Desktop/compli-docker/audit-checklist/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/Users/shanejeffery/Desktop/compli-docker/audit-checklist/node_modules/express/lib/router/layer.js:95:5)
at /Users/shanejeffery/Desktop/compli-docker/audit-checklist/node_modules/express/lib/router/index.js:277:22
at param (/Users/shanejeffery/Desktop/compli-docker/audit-checklist/node_modules/express/lib/router/index.js:349:14)
at param (/Users/shanejeffery/Desktop/compli-docker/audit-checklist/node_modules/express/lib/router/index.js:365:14) name: 'apollo.model.validator.invalidvalue' }
非常感谢任何帮助!
答案 0 :(得分:2)
@nevsv - 你是完全正确的。我觉得这应该是express-cassandra文档的一部分,但很高兴我还是想出来了。
为了帮助他人,这是模型中需要发生的事情:
module.exports = {
fields: {
id : {
type: "uuid",
default: {"$db_function": "uuid()"}
},
client_id : "int",
name : "text"
},
key : ["id"],
indexes: ["name"],
table_name: "checklist_definition"
};
希望能帮到别人!
答案 1 :(得分:1)
应该在没有引号的情况下查询Uuid类型(")。
我不是NodeJS专家,但也许你应该使用cassandra type uuid?
关于uuid的数据传输文章:
http://docs.datastax.com/en/developer/nodejs-driver/latest/features/datatypes/uuids/
答案 2 :(得分:0)
我今天在使用Casandra编写Express应用时遇到了这个问题。解决方案是使用在models对象中定义的方法uuidFromString
。
您可以在这里找到更多信息:https://express-cassandra.readthedocs.io/en/stable/datatypes/