zoukankan      html  css  js  c++  java
  • cassandra cqlsh 和 python客户端

    Keyspaces

    A cluster is a container for keyspaces. A keyspace is the outermost container for data in Cassandra, corresponding closely to a schema in a relational database. The keyspace can include operational elements, such as replication factor and data center awareness. Let's create a keyspace:

    -- create a keyspace 'my_keyspace'
    create keyspace my_keyspace with replication={'class':'SimpleStrategy', 'replication_factor':1};
    
    -- use the keyspace
    use my_keyspace;
    
    -- drop the keyspace
    drop keyspace my_keyspace;
    

    Creating a table

    To create a table type the following in cqlsh, note that you must first create a keyspace and then use that keyspace:

    CREATE TABLE users (
     firstname text, 
     lastname text, 
     age int, 
     email text, 
     city text, 
     PRIMARY KEY (lastname)
    );
    

    Describe a table

    To see detail information about a table type:

    DESCRIBE TABLE users;
    

    Insert records

    To insert records in the table type:

    INSERT INTO users (firstname, lastname, age, email, city) VALUES ('John', 'Smith', 46, 'johnsmith@email.com', 'Sacramento'); 
    INSERT INTO users (firstname, lastname, age, email, city) VALUES ('Jane', 'Doe', 36, 'janedoe@email.com', 'Beverly Hills'); 
    INSERT INTO users (firstname, lastname, age, email, city) VALUES ('Rob', 'Byrne', 24, 'robbyrne@email.com', 'San Diego');
    

    Querying a table

    To query a table type the following:

    SELECT * FROM users;
    
     lastname | age | city          | email               | firstname
    ----------+-----+---------------+---------------------+-----------
          Doe |  36 | Beverly Hills |   janedoe@email.com |      Jane
        Byrne |  24 |     San Diego |  robbyrne@email.com |       Rob
        Smith |  46 |    Sacramento | johnsmith@email.com |      John
    
    (3 rows)
    

    We can filter the result by using a predicate:

    SELECT * FROM users WHERE lastname= 'Doe';
    
     lastname | age | city          | email             | firstname
    ----------+-----+---------------+-------------------+-----------
          Doe |  36 | Beverly Hills | janedoe@email.com |      Jane
    
    (1 rows)
    

    Updating records

    To update a record in a table type the following:

    UPDATE users SET city= 'San Jose' WHERE lastname= 'Doe';
    

    The update should be available almost instantly (remember that cassandra is eventually consistent):

    SELECT * FROM users where lastname= 'Doe';
    
     lastname | age | city     | email             | firstname
    ----------+-----+----------+-------------------+-----------
          Doe |  36 | San Jose | janedoe@email.com |      Jane
    
    (1 rows)
    

    Deleting records

    To delete a record type:

    DELETE from users WHERE lastname = 'Doe';
    

    which should result in:

    SELECT * FROM users where lastname= 'Doe';
    
     lastname | age | city | email | firstname
    ----------+-----+------+-------+-----------
    
    (0 rows)
    
    SELECT * from users;
    
     lastname | age | city       | email               | firstname
    ----------+-----+------------+---------------------+-----------
        Byrne |  24 |  San Diego |  robbyrne@email.com |       Rob
        Smith |  46 | Sacramento | johnsmith@email.com |      John
    
    (2 rows)

    python cassandra客户端操作:

    from cassandra.cluster import Cluster
    cluster = Cluster(["10.178.204.225"])
    session = cluster.connect('my_keyspace')
    session.execute("""
    insert into users (lastname, age, city, email, firstname) values ('Jones', 35, 'Austin', 'bob@example.com', 'Bob')
    """)
    result = session.execute("select * from users where lastname='Jones' ")[0]
    print result.firstname, result.age
    
    session.execute("update users set age = 36 where lastname = 'Jones'")
    result = session.execute("select * from users where lastname='Jones' ")[0]
    print result.firstname, result.age
    
    
    session.execute("delete from users where lastname = 'Jones'")
    result = session.execute("select * from users")
    for x in result:
        print x.age

    参考:

    https://academy.datastax.com/resources/getting-started-apache-cassandra-and-python-part-i?unit=getting-started-apache-cassandra-and-python-part-i

    https://github.com/dnvriend/apache-cassandra-test/blob/master/readme.md

     
  • 相关阅读:
    Git ignore file for Xcode projects
    How can I create a zip archive of a whole directory via terminal without hidden files?
    What is a bare git repository?
    How to tell if UIViewController's view is visible
    Adding A Shadow To UIView
    Properties
    iOS中nil,Nil,NULL之间的区别
    FMDB的简单使用
    iOS 加载图片选择imageNamed 方法还是 imageWithContentsOfFile?
    对retain 和 assign的理解
  • 原文地址:https://www.cnblogs.com/bonelee/p/6339696.html
Copyright © 2011-2022 走看看