| Author | 
              Topic: Apache Cassandra -- Access via CQL  |  
           
         |  
        
          
            
              
                
                	
                  
                    
                      EricJ member offline     |  
                    
                      |   |  
                    
                      
                        
                          
                            | posts: | 
                            50 |  
                          
                            | joined: | 
                            02/22/2007 |  
                          
                            | from: | 
                            CA |  
                         
                       |  
                    | 
                  | 
                
                  
                    
                       |  
                    
                       |  
                    
                      
                        
                          | Apache Cassandra -- Access via CQL |  
                        
                           You can start CQL console by entering:
 
  If you have customized your Cassandra by:
 
  you should use the following command:
 
C:\..\bin> cqlsh 10.11.12.13:9042
 
  or
 
 
C:\..\bin> cqlsh 10.11.12.13
 
  Trouble Shooting:
  You may get error while executing CQLSH “can’t detect python version cqlsh” The error indicating that, Python is not installed in your machine. Steps to follow: 1) Download & install Python 2.7.x; 2) Add "Path" environment variable for Python directory; 3) Execute command “setup.py install” under “C:\apache-cassandra-2.2.1\pylib” to install python.
 
  |  
                        
                           |  
                        |  
                    
                       |  
                    
                       |  
                    |  
                |  
        
          
            
              
                
                	
                  
                    
                      EricJ member offline     |  
                    
                      |   |  
                    
                      
                        
                          
                            | posts: | 
                            50 |  
                          
                            | joined: | 
                            02/22/2007 |  
                          
                            | from: | 
                            CA |  
                         
                       |  
                    | 
                  | 
                
                  
                    
                       |  
                    
                       |  
                    
                      
                        
                          | CQL by Example |  
                        
                          Step 1. Create a new keyspace called 'playlist'
 
cqlsh> create KEYSPACE playlist WITH replication 
       = {'class':'SimpleStrategy', 'replication_factor': 1 };
  
  Step 2. Use a keyspace  
  Step 3. Create column families or tables 
 
cqlsh:playlist> create table artists_by_first_letter (
       first_letter text, 
       artist text, 
       primary key (first_letter, artist));
cqlsh:playlist> create table track_by_artist (
       track text, 
       artist text, 
       track_id UUID, 
       track_length_in_seconds int, 
       genre text,music_file text, 
       primary key (artist, track, track_id));
cqlsh:playlist> create table track_by_genre (
       track text, artist text, 
       track_id UUID, 
       track_length_in_seconds int, 
       genre text,
       music_file text, 
       primary key (genre, artist, track, track_id));
  Step 4. Populate the tables with data from files
 
cqlsh:playlist> copy artists_by_first_letter (first_letter, artist) 
     FROM 'scripts/artists.csv' WITH DELIMITER = '|';
cqlsh:playlist> copy track_by_artist (track_id, genre, artist, track, 
        track_length_in_seconds, music_file) 
     FROM 'scripts/songs.csv' 
     WITH DELIMITER = '|'  AND HEADER=true;
cqlsh:playlist> copy track_by_genre (track_id, genre, artist, track, 
        track_length_in_seconds, music_file) 
     FROM 'scripts/songs.csv' 
     WITH DELIMITER = '|'  AND HEADER=true;
  Step 5. Export the tables to file
 
cqlsh:playlist> copy artists_by_first_letter (first_letter, artist) 
     TO 'scripts/artists_out.csv'
     WITH DELIMITER = '|' AND HEADER=true;
 
  Step 6. Check table content
 
cqlsh:playlist> select * from artists_by_first_letter;
 
  Step 7. Quit cqlsh
 
  |  
                        
                           |  
                        |  
                    
                       |  
                    
                       |  
                    |  
                |  
        
          
            
              
                
                	
                  
                    
                      EricJ member offline     |  
                    
                      |   |  
                    
                      
                        
                          
                            | posts: | 
                            50 |  
                          
                            | joined: | 
                            02/22/2007 |  
                          
                            | from: | 
                            CA |  
                         
                       |  
                    | 
                  | 
                
                  
                    
                       |  
                    
                       |  
                    
                      
                        
                          | How do I know what 'keyspace' the node currently hosts? |  
                        
                          
 
cqlsh> select * from system.schema_keyspaces;
 keyspace_name      | durable_writes | strategy_class  | strategy_options
--------------------+----------------+---------------------------------------------
        system_auth |           True | SimpleStrategy | {"replication_factor":"1"}
           playlist |           True | SimpleStrategy | {"replication_factor":"1"}
 system_distributed |           True | SimpleStrategy | {"replication_factor":"3"}
             system |           True | LocalStrategy  |                       {}
      system_traces |           True | SimpleStrategy | {"replication_factor":"2"}
(5 rows)
  Here, the keyspace 'playlist' is created by user, others are system's.
 
  |  
                        
                           |  
                        |  
                    
                       |  
                    
                       |  
                    |  
                |  
        
          
            
              
                
                	
                  
                    
                      EricJ member offline     |  
                    
                      |   |  
                    
                      
                        
                          
                            | posts: | 
                            50 |  
                          
                            | joined: | 
                            02/22/2007 |  
                          
                            | from: | 
                            CA |  
                         
                       |  
                    | 
                  | 
                
                  
                    
                       |  
                    
                       |  
                    
                      
                        
                          | How do I know what 'table' the keyspace currently holds? |  
                        
                          
 
 
  |  
                        
                           |  
                        |  
                    
                       |  
                    
                       |  
                    |  
                |  
        
          
            
              
                
                	
                  
                    
                      EricJ member offline     |  
                    
                      |   |  
                    
                      
                        
                          
                            | posts: | 
                            50 |  
                          
                            | joined: | 
                            02/22/2007 |  
                          
                            | from: | 
                            CA |  
                         
                       |  
                    | 
                  | 
                
                  
                    
                       |  
                    
                       |  
                    
                      
                        
                          | How do I know what 'column' the table currently holds? |  
                        
                          
 
cqlsh> desc playlist.artists_by_first_letter;
CREATE TABLE playlist.artists_by_first_letter (
    first_letter text,
    artist text,
    PRIMARY KEY (first_letter, artist)
) WITH CLUSTERING ORDER BY (artist ASC)
    AND bloom_filter_fp_chance = 0.01
    AND caching = '{"keys":"ALL", "rows_per_partition":"NONE"}'
    AND comment = ''
    AND compaction = {'class': 'org.apache.cassandra.db.compaction.SizeTieredCom
pactionStrategy'}
    AND compression = {'sstable_compression': 'org.apache.cassandra.io.compress.
LZ4Compressor'}
    AND dclocal_read_repair_chance = 0.1
    AND default_time_to_live = 0
    AND gc_grace_seconds = 864000
    AND max_index_interval = 2048
    AND memtable_flush_period_in_ms = 0
    AND min_index_interval = 128
    AND read_repair_chance = 0.0
    AND speculative_retry = '99.0PERCENTILE';
 
 
  |  
                        
                           |  
                        |  
                    
                       |  
                    
                       |  
                    |  
                |  
        
          
            
              
                
                	
                  
                    
                      EricJ member offline     |  
                    
                      |   |  
                    
                      
                        
                          
                            | posts: | 
                            50 |  
                          
                            | joined: | 
                            02/22/2007 |  
                          
                            | from: | 
                            CA |  
                         
                       |  
                    | 
                  | 
                
                  
                    
                       |  
                    
                       |  
                    
                      
                        
                          | References |  
                        
                            1. http://docs.datastax.com/en/playlist/doc/java/playlistPreface.html
   2. http://www.devjavasource.com/cassandra/apache-cassandra-quick-start/ |  
                        
                           |  
                        |  
                    
                       |  
                    
                       |  
                    |  
                |  
      |