Search code examples
rubycassandrainsertdatastax-enterprisedatastax-ruby-driver

Cassandra Datastax Ruby Insert error


i have a problem with an insert query in ruby with cassandra,

This is my table :

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)
) 

In ruby i made a prepare statement :

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 (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")

And i put these value for testing :

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
      )

I got this error:

/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:inappend_decimal' from /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:incall' 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 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:inblock 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: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:ineach_with_index' 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 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:ininitialize' from /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:inexecute' from /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:inexecute' from /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:inmeasure' from /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:ineach' from /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:inexecute' from /home/florian/Projects/hadoop-ticket-server/import_cassandra.rb:33:in <top (required)>' from -e:1:inload' from -e:1:in `'

Did you have an idea about my problem ? Thanks you


Solution

  • The problem was with the float number.

    Ruby in float is interpreted as double or float in cassandra. To put decimal in cassandra we have to put BigDecimal in Ruby.

    source : "http://datastax.github.io/ruby-driver/features/basics/"

    solution :

    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
      )