Good Examples using Cequel

550 views
Skip to first unread message

Jerry Lin

unread,
Jul 16, 2014, 4:58:58 PM7/16/14
to ceq...@googlegroups.com
I'm new to both cassandra and ruby and am trying to find a good example I can view to not only familiarize myself with both (and with Cequel) but to learn best practices in how code is organized and how data is modeled.

Anyone have any projects that can point me to? Typically twissandra is cited but that's all in python.

Elad Ossadon

unread,
Jul 16, 2014, 6:08:13 PM7/16/14
to ceq...@googlegroups.com

Jerry Lin

unread,
Jul 16, 2014, 6:59:42 PM7/16/14
to ceq...@googlegroups.com
Thanks!

Just to clarify, this example does the data model in CQL, but Cequel has a process built into for data modeling, right? He basically replaced the rake cequel:keyspace:create step by piping in the CQL file into cqlsh and then running his seeder. Since I have a cequel.yaml, I would do this very differently?

Elad Ossadon

unread,
Jul 16, 2014, 8:41:03 PM7/16/14
to ceq...@googlegroups.com
Yes.

1. Put your models (with include Cequel::Record) under app/models, make sure the file names match the class names.
2. Run rake cequel:keyspace:create to create the keyspace (if doesn't exist yet)
3. Run rake cequel:migrate
4. cqlsh to your cassandra instance and run DESCRIBE yourkeyspace to see the current schema.
5. Some changes you make to models, like adding new columns or indexes, can be migrated with rake cequel:migrate. Changes like removing columns or changing types can't be migrated.

Jerry Lin

unread,
Jul 16, 2014, 10:36:24 PM7/16/14
to ceq...@googlegroups.com
I actually tried this but oddly, the keyspace gets created, but the tables do not. It is empty.

I wrote a model on my own and used the cequel generator to make the second. Neither of them show up

Is there a way I can trace it to figure out why?

Here is exactly what I am doing
cqlsh> DROP KEYSPACE jerry_test_development;

(also tried just cequel:keyspace:create)
$ rake cequel:init 
Created keyspace jerry_test_development
$ rake cequel:migrate

cqlsh> USE jerry_test_development;
cqlsh:jerry_test_development> DESC TABLES;

<empty>

Jerry Lin

unread,
Jul 16, 2014, 10:38:01 PM7/16/14
to ceq...@googlegroups.com
It may also be important to note, each of my models are within a module, and the class name is capitalized and the file name is lowercase. I assume this is ok because I saw the module in the twissandra-rb example, though they don't use cequel:init, and the generator also made the class name capitalized and filename lowercase.

Elad Ossadon

unread,
Jul 17, 2014, 2:32:50 PM7/17/14
to ceq...@googlegroups.com
twissandra-rb didn't use the cequel migrations.

If your file tree is like:

- app
  - models
    - user.rb

and on user.rb you have

class User
  include Cequel::Record
  ...
end

it should work. If it doesn't, I'd debug the rake task with byebug or throwing some puts statements to see it fetches the right path.

Jerry Lin

unread,
Jul 17, 2014, 8:22:24 PM7/17/14
to ceq...@googlegroups.com
Ah...

So I guess it doesn't seem to like my model being place inside of module.

My file structure is just as you specified above. I commented out the module lines like so:

#module Test
  class User
    include Cequel::Record

    key :email, :text
    column :encrypted_password, :text
    column :first_name, :text
    column :last_name, :text
    column :date_of_birth, :timestamp
    #timestamps
  end
#end

and it worked! When I put module back it doesn't. This something easily supported by cequel in the future?

Thanks again!

Elad Ossadon

unread,
Jul 17, 2014, 10:27:57 PM7/17/14
to ceq...@googlegroups.com
The current rake code does what ActiveRecord does - it infers the class name from the file. If you use module as a namespace, the file should be nested under a directory.

In your example, app/models/test/user.rb

Jerry Lin

unread,
Jul 19, 2014, 3:16:22 PM7/19/14
to ceq...@googlegroups.com
Ohhhh... Gotcha. Thanks!

I'm new to both Ruby/Rails and Cassandra. I did not know thats how ActiveRecord worked. Thanks for clearing that up. It should at least say something when there is a mismatch between the file location and the class name?

Anyway... when I tried moving it to the subfolder and adding the module back, this happens when I try to rake cequel:migrate --trace

** Invoke cequel:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute cequel:migrate
rake aborted!
Cql::QueryError: line 1:22 no viable alternative at character 'g'
/Users/jerrylin/.rvm/gems/ruby-2.1.2/gems/cql-rb-2.0.3/lib/cql/client/client.rb:545:in `execute'
/Users/jerrylin/.rvm/gems/ruby-2.1.2/gems/cequel-1.4.1/lib/cequel/metal/keyspace.rb:178:in `block in execute_with_consistency'
/Users/jerrylin/.rvm/gems/ruby-2.1.2/gems/cequel-1.4.1/lib/cequel/metal/request_logger.rb:34:in `block in log'
/Users/jerrylin/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.4/lib/active_support/core_ext/benchmark.rb:12:in `block in ms'
/Users/jerrylin/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/benchmark.rb:294:in `realtime'
/Users/jerrylin/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.4/lib/active_support/core_ext/benchmark.rb:12:in `ms'
/Users/jerrylin/.rvm/gems/ruby-2.1.2/gems/cequel-1.4.1/lib/cequel/metal/request_logger.rb:34:in `log'
/Users/jerrylin/.rvm/gems/ruby-2.1.2/gems/cequel-1.4.1/lib/cequel/metal/keyspace.rb:177:in `execute_with_consistency'
/Users/jerrylin/.rvm/gems/ruby-2.1.2/gems/cequel-1.4.1/lib/cequel/metal/keyspace.rb:163:in `execute'
/Users/jerrylin/.rvm/gems/ruby-2.1.2/gems/cequel-1.4.1/lib/cequel/schema/table_writer.rb:37:in `apply'
/Users/jerrylin/.rvm/gems/ruby-2.1.2/gems/cequel-1.4.1/lib/cequel/schema/table_writer.rb:16:in `apply'
/Users/jerrylin/.rvm/gems/ruby-2.1.2/gems/cequel-1.4.1/lib/cequel/schema/table_synchronizer.rb:35:in `apply'
/Users/jerrylin/.rvm/gems/ruby-2.1.2/gems/cequel-1.4.1/lib/cequel/record/schema.rb:84:in `synchronize_schema'
/Users/jerrylin/.rvm/gems/ruby-2.1.2/gems/cequel-1.4.1/lib/cequel/record/tasks.rb:45:in `block (4 levels) in <top (required)>'
/Users/jerrylin/.rvm/gems/ruby-2.1.2/gems/cequel-1.4.1/lib/cequel/record/tasks.rb:38:in `each'
/Users/jerrylin/.rvm/gems/ruby-2.1.2/gems/cequel-1.4.1/lib/cequel/record/tasks.rb:38:in `block (3 levels) in <top (required)>'
/Users/jerrylin/.rvm/gems/ruby-2.1.2/gems/cequel-1.4.1/lib/cequel/record/tasks.rb:25:in `each'
/Users/jerrylin/.rvm/gems/ruby-2.1.2/gems/cequel-1.4.1/lib/cequel/record/tasks.rb:25:in `block (2 levels) in <top (required)>'
/Users/jerrylin/.rvm/gems/ruby-2.1.2/gems/rake-10.3.2/lib/rake/task.rb:240:in `call'
/Users/jerrylin/.rvm/gems/ruby-2.1.2/gems/rake-10.3.2/lib/rake/task.rb:240:in `block in execute'
/Users/jerrylin/.rvm/gems/ruby-2.1.2/gems/rake-10.3.2/lib/rake/task.rb:235:in `each'
/Users/jerrylin/.rvm/gems/ruby-2.1.2/gems/rake-10.3.2/lib/rake/task.rb:235:in `execute'
/Users/jerrylin/.rvm/gems/ruby-2.1.2/gems/rake-10.3.2/lib/rake/task.rb:179:in `block in invoke_with_call_chain'
/Users/jerrylin/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/monitor.rb:211:in `mon_synchronize'
/Users/jerrylin/.rvm/gems/ruby-2.1.2/gems/rake-10.3.2/lib/rake/task.rb:172:in `invoke_with_call_chain'
/Users/jerrylin/.rvm/gems/ruby-2.1.2/gems/rake-10.3.2/lib/rake/task.rb:165:in `invoke'
/Users/jerrylin/.rvm/gems/ruby-2.1.2/gems/rake-10.3.2/lib/rake/application.rb:150:in `invoke_task'
/Users/jerrylin/.rvm/gems/ruby-2.1.2/gems/rake-10.3.2/lib/rake/application.rb:106:in `block (2 levels) in top_level'
/Users/jerrylin/.rvm/gems/ruby-2.1.2/gems/rake-10.3.2/lib/rake/application.rb:106:in `each'
/Users/jerrylin/.rvm/gems/ruby-2.1.2/gems/rake-10.3.2/lib/rake/application.rb:106:in `block in top_level'
/Users/jerrylin/.rvm/gems/ruby-2.1.2/gems/rake-10.3.2/lib/rake/application.rb:115:in `run_with_threads'
/Users/jerrylin/.rvm/gems/ruby-2.1.2/gems/rake-10.3.2/lib/rake/application.rb:100:in `top_level'
/Users/jerrylin/.rvm/gems/ruby-2.1.2/gems/rake-10.3.2/lib/rake/application.rb:78:in `block in run'
/Users/jerrylin/.rvm/gems/ruby-2.1.2/gems/rake-10.3.2/lib/rake/application.rb:176:in `standard_exception_handling'
/Users/jerrylin/.rvm/gems/ruby-2.1.2/gems/rake-10.3.2/lib/rake/application.rb:75:in `run'
/Users/jerrylin/.rvm/gems/ruby-2.1.2/gems/rake-10.3.2/bin/rake:33:in `<top (required)>'
/Users/jerrylin/.rvm/gems/ruby-2.1.2/bin/rake:23:in `load'
/Users/jerrylin/.rvm/gems/ruby-2.1.2/bin/rake:23:in `<main>'
/Users/jerrylin/.rvm/gems/ruby-2.1.2/bin/ruby_executable_hooks:15:in `eval'
/Users/jerrylin/.rvm/gems/ruby-2.1.2/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => cequel:migrate

When I move it back up 1 folder in the structure and remove the module declaration, it works fine.

Viral Gandhi

unread,
Aug 26, 2015, 7:20:58 AM8/26/15
to Cequel
Hello,

I'm trying for very basic example and I am not able to run rake cequel:keyspace:create and getting below error
Can you please help in understanding and resolving this error?

** Invoke cequel:keyspace:create (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute cequel:keyspace:create
rake aborted!
Errno::EBADF: Bad file descriptor
C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/ione-1.2.1/lib/ione/io/io_r
eactor.rb:393:in `write_nonblock'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/ione-1.2.1/lib/ione/io/io_r
eactor.rb:393:in `unblock'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/ione-1.2.1/lib/ione/io/io_r
eactor.rb:235:in `connect'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/cassandra-driver-1.2.0/lib/
cassandra/cluster/connector.rb:119:in `do_connect'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/cassandra-driver-1.2.0/lib/
cassandra/cluster/connector.rb:47:in `connect'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/cassandra-driver-1.2.0/lib/
cassandra/cluster/control_connection.rb:633:in `connect_to_host'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/cassandra-driver-1.2.0/lib/
cassandra/cluster/control_connection.rb:575:in `connect_to_first_available'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/cassandra-driver-1.2.0/lib/
cassandra/cluster/control_connection.rb:61:in `block in connect_async'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/ione-1.2.1/lib/ione/future.
rb:413:in `call'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/ione-1.2.1/lib/ione/future.
rb:413:in `block in flat_map'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/ione-1.2.1/lib/ione/future.
rb:745:in `call'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/ione-1.2.1/lib/ione/future.
rb:745:in `call_listener'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/ione-1.2.1/lib/ione/future.
rb:784:in `block in resolve'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/ione-1.2.1/lib/ione/future.
rb:783:in `each'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/ione-1.2.1/lib/ione/future.
rb:783:in `resolve'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/ione-1.2.1/lib/ione/future.
rb:30:in `fulfill'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/ione-1.2.1/lib/ione/io/io_r
eactor.rb:155:in `block in start'
Tasks: TOP => cequel:keyspace:create


I have single model created manually  called post.rb 
path- blog\app\models

class Post
  include Cequel::Record

  key :id, :timeuuid, auto: true
  column :title, :text
  column :body, :text

  timestamps
end

Viral Gandhi

unread,
Aug 26, 2015, 7:20:58 AM8/26/15
to Cequel


On Thursday, 17 July 2014 06:11:03 UTC+5:30, Elad Ossadon wrote:
Reply all
Reply to author
Forward
0 new messages