【问题标题】:Querying Cassandra with UUID from NodeJS从 NodeJS 使用 UUID 查询 Cassandra
【发布时间】:2017-08-11 20:47:42
【问题描述】:

感谢您抽出宝贵时间查看我的问题。我和我的团队刚刚开始涉足 NodeJS,这是我们第一次遇到迄今为止我们无法为自己找到解决方案的问题。

简单地说,我们有一个控制器需要使用 UUID 主键查询 Cassandra。

控制器:

'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 密钥空间架构:

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' }

任何帮助将不胜感激!

【问题讨论】:

  • 请通过添加您的 cassandra 键空间架构来编辑您的帖子。
  • 将 Keyspace 架构添加到主帖中。

标签: node.js cassandra


【解决方案1】:

Uuid 类型应不带引号 (") 进行查询。

我不是 NodeJS 专家,但也许你应该使用 cassandra 类型的 uuid?

有关于 uuid 的 Datastax 文章:

http://docs.datastax.com/en/developer/nodejs-driver/latest/features/datatypes/uuids/

【讨论】:

    【解决方案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"
    };
    

    希望对其他人有所帮助!

    【讨论】:

    • 另外请查看您的架构。您使用索引。这不好,因为它可能导致全表扫描。您应该考虑使用集群列或更合适的架构!
    • 我也在使用 cassandra-orm,但是对于 uuid 字段 id,它抛出了同样的错误。在这里我无法更改查询,因为我正在使用他们的内置方法。可以做什么?
    【解决方案3】:

    我今天在使用 Casandra 编写 Express 应用程序时遇到了这个问题。解决方案是在模型对象中使用uuidFromStringdefined 方法。 你可以在这里找到更多:https://express-cassandra.readthedocs.io/en/stable/datatypes/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-03
      • 2019-08-11
      • 1970-01-01
      • 2018-08-22
      • 1970-01-01
      • 1970-01-01
      • 2016-09-06
      • 2016-11-17
      相关资源
      最近更新 更多