Cassandra Datastax Ruby 插入错误

问题描述 投票:0回答:1

我在 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

to_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
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:in
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
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中的
to_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
块中' 来自/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
each'
      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
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:in
encode_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
初始化' 来自/home/florian/.rvm/gems/ruby-2.1.4/gems/cassandra-driver-1.0.0.rc.1/lib/cassandra/cluster/client.rb:190:in
new'
      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/.rvm/gems/ruby-2.1.4/gems/cassandra-driver-1.0.0.rc.1/lib/cassandra/session.rb:81:in
execute_async'
      from /home/florian/.rvm/gems/ruby-2.1.4/gems/cassandra-driver-1.0.0.rc.1/lib/cassandra/session.rb:103:in
execute' 来自 /home/florian/Projects/hadoop-ticket-server/import_cassandra.rb:60:in
block (3 levels) in <top (required)>'
      from /home/florian/.rvm/rubies/ruby-2.1.4/lib/ruby/2.1.0/benchmark.rb:279:in
measure' 来自 /home/florian/Projects/hadoop-ticket-server/import_cassandra.rb:35:in
block (2 levels) in <top (required)>'
      from /home/florian/Projects/hadoop-ticket-server/import_cassandra.rb:34:in
each' 来自/home/florian/Projects/hadoop-ticket-server/import_cassandra.rb:34:in
block in <top (required)>'
      from /home/florian/Projects/hadoop-ticket-server/import_cassandra.rb:33:in
execute' 来自/home/florian/Projects/hadoop-ticket-server/import_cassandra.rb:33:in
<top (required)>'
      from -e:1:in
load' 来自 -e:1:in `'

您对我的问题有想法吗?谢谢你

ruby cassandra insert datastax-enterprise datastax-ruby-driver
1个回答
3
投票

问题出在浮点数上。

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
  )
© www.soinside.com 2019 - 2024. All rights reserved.