我正在尝试获取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));
Run Code Online (Sandbox Code Playgroud)
请记住,您正在处理NoSQL(“非关系”)
If I were to run a
SELECT * FROM users LIMIT 1on 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.
If obtaining the last inserted row's information is needed for your system, then you will have to know it before you insert it.
const Uuid = require('cassandra-driver').types.Uuid;
const id = Uuid.random();
Run Code Online (Sandbox Code Playgroud)
Then you can use the id as another value for the prepared insert query
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));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
275 次 |
| 最近记录: |