Category Archives: NoSQL

Cassandra Basic Failover Test

[root@cassandra1 /]# nodetool status Datacenter: 70 ============== Status=Up/Down |/ State=Normal/Leaving/Joining/Moving — Address Load Tokens Owns (effective) Host ID Rack UN 10.70.0.136 66.55 KB 256 50.2% eab68af1-4c3a-448a-b64a-89432abbe13f 0 DN 10.70.0.137 96.67 KB 1 0.3% ca11bb3d-a171-49c9-a39b-d1b802c5a9d8 0 DN 10.70.0.138 80.98 KB 256 49.5% ab54aa7d-ad0a-4bf7-99e4-2d4465c64706 0 [default@unknown] use demodb1; Authenticated to keyspace: demodb1 [default@demodb1] GET users[utf8(‘bobbyjo’)][utf8(‘full_name’)]; => (name=full_name, value=Robert Jones, timestamp=1382458230649000) Elapsed time: 56 msec(s). Let’s turn down node 1 and 2 (.136 and .137). In this case connect to node 3 and you can’t get data on node 1:

# cassandra-cli -host 10.70.0.138 -port 9160
[default@unknown] use demodb1;
Authenticated to keyspace: demodb1
[default@demodb1] GET users[utf8('bobbyjo')][utf8('full_name')];
null
UnavailableException()
at org.apache.cassandra.thrift.Cassandra$get_result.read(Cassandra.java:6608)
at org.apache.thrift.TServiceClient.receiveBase(TServiceClient.java:78)
at org.apache.cassandra.thrift.Cassandra$Client.recv_get(Cassandra.java:556)
at org.apache.cassandra.thrift.Cassandra$Client.get(Cassandra.java:541)
at org.apache.cassandra.cli.CliClient.executeGet(CliClient.java:729)
at org.apache.cassandra.cli.CliClient.executeCLIStatement(CliClient.java:216)
at org.apache.cassandra.cli.CliMain.processStatementInteractive(CliMain.java:213)
at org.apache.cassandra.cli.CliMain.main(CliMain.java:339)
[default@demodb1] GET users[utf8('bobbyjo')][utf8('full_name')];
null
Let’s add data on node 3 (.138):
SET users['capache']['full_name']='Cassandra Apache';
SET users['capache']['email']='capache@gmail.com';
SET users['capache']['state']='HS';
SET users['capache']['gender']='F';
SET users['capache']['birth_year']='1970';
GET users[utf8('capache')][utf8('full_name')];
=> (name=full_name, value=Cassandra Apache, timestamp=1382527239265000)
Elapsed time: 58 msec(s).
Turn node 1(.136) back on:
# cassandra-cli
Connected to: "My Cassandra Cluster" on 127.0.0.1/9160
Welcome to Cassandra CLI version 1.2.10
[default@unknown] use demodb1;
Authenticated to keyspace: demodb1
[default@demodb1] GET users[utf8('bobbyjo')][utf8('full_name')];
=> (name=full_name, value=Robert Jones, timestamp=1382458230649000)
Elapsed time: 94 msec(s).
[default@demodb1] GET users[utf8('capache')][utf8('full_name')];
=> (name=full_name, value=Cassandra Apache, timestamp=1382527239265000)
Elapsed time: 8.11 msec(s).
]]>

Cassandra 1.2.10 on RHEL 6.4

  • Install Java
  • Setup DataStax repo
  • yum install dsc12
  • Examples: 3 nodes: 10.70.0.136 (seed), 10.70.0.137 and 10.70.0.138
    Stop Cassandra:
    $ sudo service cassandra stop
    Clear the data (not necessary for first run):
    $ sudo rm -rf /var/lib/cassandra/*
    vi /etc/cassandra/conf/cassandra.yaml
    cluster_name: 'My Cassandra Cluster'
    num_tokens: 256
    seed_provider:
    - class_name: org.apache.cassandra.locator.SimpleSeedProvider
    parameters:
    - seeds: "10.70.0.136"
    listen_address: 10.70.0.136
    rpc_address: 0.0.0.0
    endpoint_snitch: RackInferringSnitch
    Listen_address is the IP address of the node, change listen_address with 10.70.0.137 and 10.70.0.138 for node 2 and 3. On each server, run one by one:
    service cassandra start
    Check status:
    root@cassandra1 /]# nodetool status
    Datacenter: 70
    ==============
    Status=Up/Down
    |/ State=Normal/Leaving/Joining/Moving
    -- Address Load Tokens Owns Host ID Rack
    UN 10.70.0.137 120.53 KB 1 0.3% ca11bb3d-a171-49c9-a39b-d1b802c5a9d8 0
    UN 10.70.0.138 48.45 KB 256 49.5% ab54aa7d-ad0a-4bf7-99e4-2d4465c64706 0
    UN 10.70.0.136 33.99 KB 256 50.2% eab68af1-4c3a-448a-b64a-89432abbe13f 0
    To use the database, reference: http://www.datastax.com/docs/0.8/dml/using_cli
    $ cassandra-cli -host localhost -port 9160
    or just:
    # cassandra-cli
    Connected to: "My Cassandra Cluster" on 127.0.0.1/9160
    Welcome to Cassandra CLI version 1.2.10
    
    Type 'help;' or '?' for help.
    Type 'quit;' or 'exit;' to quit.
    
    [default@unknown] CREATE KEYSPACE demodb1
    ... with placement_strategy = 'org.apache.cassandra.locator.SimpleStrategy'
    ... and strategy_options = [{replication_factor:1}];
    WARNING: [{}] strategy_options syntax is deprecated, please use {}
    49b9ea34-770f-3f98-ad2c-bf6b40814ab8
    
    [default@unknown] use demodb1;
    
    CREATE COLUMN FAMILY users
    WITH comparator = UTF8Type
    AND key_validation_class=UTF8Type
    AND column_metadata = [
    {column_name: full_name, validation_class: UTF8Type}
    {column_name: email, validation_class: UTF8Type}
    {column_name: state, validation_class: UTF8Type}
    {column_name: gender, validation_class: UTF8Type}
    {column_name: birth_year, validation_class: LongType}
    ];
    
    SET users['bobbyjo']['full_name']='Robert Jones';
    SET users['bobbyjo']['email']='bobjones@gmail.com';
    SET users['bobbyjo']['state']='TX';
    SET users['bobbyjo']['gender']='M';
    SET users['bobbyjo']['birth_year']='1975';
    
    [default@demodb1] GET users[utf8('bobbyjo')][utf8('full_name')];
    => (name=full_name, value=Robert Jones, timestamp=1382458230649000)
    The easiest way to use Cassandra, is using cqlsh, example:
    cqlsh> use demodb1;
    cqlsh:demodb1>  select * from users;
    
     key     | birth_year | email              | full_name        | gender | state
    ---------+------------+--------------------+------------------+--------+-------
     capache |       1970 |  capache@gmail.com | Cassandra Apache |      F |    HS
     bobbyjo |       1975 | bobjones@gmail.com |     Robert Jones |      M |    TX
    ]]>