我在 ruby 中使用 cassandra 进行插入查询时遇到问题,
这是我的桌子:
CREATE TABLE testkeyspace.ticket (
id int,
uid text,
annule boolean,
avoir decimal,
caisse int,
clotureid int,
couverts decimal,
creation_iso timestamp,
modif_iso timestamp,
montantencaisse decimal,
montantttc decimal,
nb_articles int,
numero int,
remise decimal,
remise_montant decimal,
remise_type text,
remise_valeur decimal,
rendu decimal,
stats_iso timestamp,
PRIMARY KEY (id, uid)
)
在 ruby 中我做了一个准备声明:
insert_table_ticket = session.prepare("INSERT INTO ticket(id, uid, annule, avoir, caisse, clotureid, couverts,creation_iso, modif_iso, montantencaisse, montantttc, nb_articles, numero, remise,remise_montant, remise_type, remise_valeur, rendu,stats_iso) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
我将这些值用于测试:
session.execute(insert_table_ticket,
1,
"test",
true,
1.1,
1,
1,
1.0,
1415350203,
1415350203,
1.1,
1.1,
1,
1,
1.1,
1.1,
"tests",
1.1,
1.1,
1415350203
)
我收到此错误:
/home/florian/.rvm/gems/ruby-2.1.4/gems/cassandra-driver-1.0.0.rc.1/lib/cassandra/protocol/cql_byte_buffer.rb:275:in
append_decimal' 来自/home/florian/.rvm/gems/ruby-2.1.4/gems/cassandra-driver-1.0.0.rc.1/lib/cassandra/protocol/type_converter.rb:287:into_s': wrong number of arguments (1 for 0) (ArgumentError) from /home/florian/.rvm/gems/ruby-2.1.4/gems/cassandra-driver-1.0.0.rc.1/lib/cassandra/protocol/cql_byte_buffer.rb:275:in
call' 来自/home/florian/.rvm/gems/ruby-2.1.4/gems/cassandra-driver-1.0.0.rc.1/lib/cassandra/protocol/type_converter.rb:85:在encode_values中的decimal_to_bytes' from /home/florian/.rvm/gems/ruby-2.1.4/gems/cassandra-driver-1.0.0.rc.1/lib/cassandra/protocol/type_converter.rb:85:in
块中' 来自/home/florian/.rvm/gems/ruby-2.1.4/gems/cassandra-driver-1.0.0.rc.1/lib/cassandra/protocol/requests/execute_request.rb:92:into_bytes' from /home/florian/.rvm/gems/ruby-2.1.4/gems/cassandra-driver-1.0.0.rc.1/lib/cassandra/protocol/requests/execute_request.rb:93:in
each_with_index ' 来自/home/florian/.rvm/gems/ruby-2.1.4/gems/cassandra-driver-1.0.0.rc.1/lib/cassandra/protocol/requests/execute_request.rb:92:ineach' from /home/florian/.rvm/gems/ruby-2.1.4/gems/cassandra-driver-1.0.0.rc.1/lib/cassandra/protocol/requests/execute_request.rb:92:in
初始化' 来自/home/florian/.rvm/gems/ruby-2.1.4/gems/cassandra-driver-1.0.0.rc.1/lib/cassandra/cluster/client.rb:190:inencode_values' from /home/florian/.rvm/gems/ruby-2.1.4/gems/cassandra-driver-1.0.0.rc.1/lib/cassandra/protocol/requests/execute_request.rb:39:in
execute' 来自/home/florian/.rvm/gems/ruby-2.1.4/gems/cassandra-driver-1.0.0.rc.1/lib/cassandra/session.rb:81:innew' from /home/florian/.rvm/gems/ruby-2.1.4/gems/cassandra-driver-1.0.0.rc.1/lib/cassandra/cluster/client.rb:190:in
execute' 来自 /home/florian/Projects/hadoop-ticket-server/import_cassandra.rb:60:inexecute_async' from /home/florian/.rvm/gems/ruby-2.1.4/gems/cassandra-driver-1.0.0.rc.1/lib/cassandra/session.rb:103:in
measure' 来自 /home/florian/Projects/hadoop-ticket-server/import_cassandra.rb:35:inblock (3 levels) in <top (required)>' from /home/florian/.rvm/rubies/ruby-2.1.4/lib/ruby/2.1.0/benchmark.rb:279:in
each' 来自/home/florian/Projects/hadoop-ticket-server/import_cassandra.rb:34:inblock (2 levels) in <top (required)>' from /home/florian/Projects/hadoop-ticket-server/import_cassandra.rb:34:in
execute' 来自/home/florian/Projects/hadoop-ticket-server/import_cassandra.rb:33:inblock in <top (required)>' from /home/florian/Projects/hadoop-ticket-server/import_cassandra.rb:33:in
load' 来自 -e:1:in `'<top (required)>' from -e:1:in
您对我的问题有想法吗?谢谢你
问题出在浮点数上。
float 中的 Ruby 被解释为 cassandra 中的 double 或 float。 要在 cassandra 中放入小数,我们必须在 Ruby 中放入 BigDecimal。
来源:“http://datastax.github.io/ruby-driver/features/basics/”
解决方案:
session.execute(insert_table_ticket,
1,
"test",
true,
BigDecimal.new('1.1'),
1,
1,
BigDecimal.new('1.0'),
1415350203,
1415350203,
BigDecimal.new('1.1'),
BigDecimal.new('1.1'),
1,
1,
BigDecimal.new('1.1'),
BigDecimal.new('1.1'),
"tests",
BigDecimal.new('1.1'),
BigDecimal.new('1.1'),
1415350203
)