gpt4 book ai didi

node.js - 如何在 cassandra 中执行查询后立即插入行?

转载 作者:搜寻专家 更新时间:2023-11-01 00:16:20 24 4
gpt4 key购买 nike

我试图在 cassandra 中获取最后插入的行,但我得到的是 undefined

下面是代码查找insert的方式:

const cassandra = require('cassandra-driver');
const client = new cassandra.Client({ contactPoints: ['h1', 'h2'], keyspace: 'ks1' });

let query = "INSERT INTO users (id, name, email ) values (uuid(), ?, ?)";

client.execute(query, [ 'badis', 'badis@badis.com' ])
.then(result => console.log('User with id %s', result.rows[0].id));

最佳答案

请记住,您正在处理 NoSQL(“非关系型”)

If I were to run a SELECT * FROM users LIMIT 1 on this table, my result set would contain a single row. That row would be the one containing the lowest hashed value of username (my partition key), because that's how Cassandra stores data in the cluster. I would have no way of knowing if it was the last one added or not, so this wouldn't be terribly useful to you.

The workings are very different from those that you might be used to in MySQL.

如果您的系统需要获取最后插入行的信息,那么您必须在插入前了解它。

Generating UUID

const Uuid = require('cassandra-driver').types.Uuid;
const id = Uuid.random();

然后您可以使用 id 作为准备好的插入查询的另一个值

const cassandra = require('cassandra-driver');
const client = new cassandra.Client({ contactPoints: ['h1', 'h2'], keyspace: 'ks1' });

let query = "INSERT INTO users (id, name, email ) values (?, ?, ?)";

client.execute(query, [id, 'badis', 'badis@badis.com' ])
.then(result => console.log('User with id %s', id));

关于node.js - 如何在 cassandra 中执行查询后立即插入行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50161460/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com