I have a very simple table in cassandra.
Name: test
Columns :
我在cassandra有一个非常简单的表。名称:测试列:
- id ( bigint, primary key)
- id(bigint,主键)
- name (text)
- 姓名(文字)
I am trying to insert into values into this using nodejs (using cassandra-driver)
我试图使用nodejs插入值(使用cassandra-driver)
client.execute("insert into test (id, name) values (?, ?)", [123, "dskddjls"], function (err, response) {
if (err) {
console.log("An error occured, ", err)
//...
}
else {
console.log("Added to table, ", response)
//...
}
})
The insertion completes successfully but when I check my cassandra database, it appears as if I have garbage values for big int column.
插入成功完成但是当我检查我的cassandra数据库时,看起来好像我有big int列的垃圾值。
Any explanation for why this is happening ?
对于为什么会发生这种情况的任何解释?
2 个解决方案
#1
3
You need to specify the type explicitly as the third argument to execute
function:
您需要明确指定类型作为执行函数的第三个参数:
client.execute("insert into test (id, name) values (?, ?)", [123, "dskddjls"], {hints: ["bigint", null]}, function (err, response) {
...
})
The reason is that for some fields cassandra driver can't guess the type (like bigint or timestamp), so you need to hint it. For string or regular numbers it will work without a hint.
原因是对于某些字段,cassandra驱动程序无法猜测类型(如bigint或timestamp),因此您需要提示它。对于字符串或常规数字,它将在没有提示的情况下工作。
#2
1
In the CQL datatypes to JavaScript types documentation you can see that expected JavaScript type for bigint
is Long
. The JavaScript Number
type is a double-precision 64-bit binary format IEEE 754 value, used by the driver to represent Cassandra int
, float
and double
(default).
在CQL数据类型到JavaScript类型文档中,您可以看到bigint的预期JavaScript类型是Long。 JavaScript Number类型是双精度64位二进制格式IEEE 754值,驱动程序使用它来表示Cassandra int,float和double(默认值)。
In your case, if you want to insert a value that you have in your application as a number, you should use the Long.fromNumber()
method:
在您的情况下,如果要将应用程序中的值作为数字插入,则应使用Long.fromNumber()方法:
const query = "insert into test (id, name) values (?, ?)";
client.execute(query, [ Long.fromNumber(123), "my name" ], callback);
Also, for accurate mapping between a JavaScript type and a Cassandra type (along with other benefits) you should prepare your queries. In your case, if you set the prepare
flag, the driver will identify that the expected value is a Long
and do the conversion from Number
:
此外,为了准确映射JavaScript类型和Cassandra类型(以及其他好处),您应该准备您的查询。在您的情况下,如果您设置准备标志,则驱动程序将识别预期值为Long并从Number进行转换:
client.execute(query, [ 123, "my name" ], { prepare: true }, callback);
#1
3
You need to specify the type explicitly as the third argument to execute
function:
您需要明确指定类型作为执行函数的第三个参数:
client.execute("insert into test (id, name) values (?, ?)", [123, "dskddjls"], {hints: ["bigint", null]}, function (err, response) {
...
})
The reason is that for some fields cassandra driver can't guess the type (like bigint or timestamp), so you need to hint it. For string or regular numbers it will work without a hint.
原因是对于某些字段,cassandra驱动程序无法猜测类型(如bigint或timestamp),因此您需要提示它。对于字符串或常规数字,它将在没有提示的情况下工作。
#2
1
In the CQL datatypes to JavaScript types documentation you can see that expected JavaScript type for bigint
is Long
. The JavaScript Number
type is a double-precision 64-bit binary format IEEE 754 value, used by the driver to represent Cassandra int
, float
and double
(default).
在CQL数据类型到JavaScript类型文档中,您可以看到bigint的预期JavaScript类型是Long。 JavaScript Number类型是双精度64位二进制格式IEEE 754值,驱动程序使用它来表示Cassandra int,float和double(默认值)。
In your case, if you want to insert a value that you have in your application as a number, you should use the Long.fromNumber()
method:
在您的情况下,如果要将应用程序中的值作为数字插入,则应使用Long.fromNumber()方法:
const query = "insert into test (id, name) values (?, ?)";
client.execute(query, [ Long.fromNumber(123), "my name" ], callback);
Also, for accurate mapping between a JavaScript type and a Cassandra type (along with other benefits) you should prepare your queries. In your case, if you set the prepare
flag, the driver will identify that the expected value is a Long
and do the conversion from Number
:
此外,为了准确映射JavaScript类型和Cassandra类型(以及其他好处),您应该准备您的查询。在您的情况下,如果您设置准备标志,则驱动程序将识别预期值为Long并从Number进行转换:
client.execute(query, [ 123, "my name" ], { prepare: true }, callback);