Day: February 22, 2017

  • Cassandra #01 เริ่มต้นใช้งาน

    Cassandra เป็นระบบ Database ที่เหมาะสำหรับการ Scalability และให้ High Availability โดยไปลดประสิทธิภาพ มีความทนทานสูง (Fault Tolerance) โดยสามารถใช้งานเครื่องคอมพิวเตอร์ทั่วไปๆ (Commodity Hardware) หรือ ใช้งาน Cloud Infrastructure สำหรับงานที่มีความสำคัญได้ (Mission Critical) สามารถกระจายสำเนา (Replication) ข้าม Data Center ได้อีกด้วย [1][3]

    ในบทความนี้ จะแสดงการติดตั้งบน Ubuntu 16.04 เพียงเครื่องเดียวก่อน และให้เห็นวิธีการติดต่อตัวฐานข้อมูล การส่ง Query เบื้องต้น

    # echo "deb http://www.apache.org/dist/cassandra/debian 310x main" | sudo tee -a /etc/apt/sources.list.d/cassandra.sources.list
    # curl https://www.apache.org/dist/cassandra/KEYS | sudo apt-key add -
    # sudo apt-get update
    # sudo apt-get install cassandra

    ต่อไป วิธีการส่งคำสั่งไปยัง Cassandra จะใช้งานผ่าน cqlsh (Cassandra Query Language Shell)

    # cqlsh -C
    Connected to Test Cluster at 127.0.0.1:9042.
    [cqlsh 5.0.1 | Cassandra 3.10 | CQL spec 3.4.4 | Native protocol v4]
    Use HELP for help.
    cqlsh>

    ใน Cassandra จะเรียก Database ว่า Keyspace วิธีที่จะดูว่ามี Keyspace อะไรอยู่บ้าง ใช้คำสั่ง [2]

    cqlsh> DESCRIBE keyspaces;
    system_schema system_auth system system_distributed system_traces

    สั่งสร้าง Keyspace ชื่อ test ด้วยคำสั่งต่อไปนี้

    cqlsh> CREATE KEYSPACE test WITH REPLICATION = {'class' : 'SimpleStrategy', 'replication_factor' : 3};

    เนื่องจาก Cassandra มีลักษณะพิเศษที่ไม่เหมือนกับ Database Servers อื่นๆ ก็คือเรื่องการ Replication ตั้งแต่เริ่มต้น ในคำสั่งข้างต้น จะบอกว่า ระบบจากสร้าง 3 สำเนา เป็นหลัก (จะกล่าวรายละเอียดในตอนต่อๆไป)

    ต่อไป สั่งสร้าง Table ชื่อ table1

    csqlsh> use test;
    cqlsh:test> create table table1 (
     ... id text primary key,
     ... age int,
     ... firstname text,
     ... lastname text
     ... );
    

    คำสั่งในการ Insert ข้อมูล

    cqlsh:test> INSERT INTO table1 (id, age, firstname, lastname) VALUES ( '000001', 10, 'John', 'Doe' );

    คำสั่งในการ Select ข้อมูล

    cqlsh:test> SELECT * FROM table1;
    
     id | age | firstname | lastname
    --------+-----+-----------+----------
     000001 | 10 | John | Doe
    
    (1 rows)

    คำสั่งในการ Update ข้อมูล

    cqlsh:test> UPDATE table1
     ... SET age = 20
     ... WHERE id = '000001'
     ... ;
    cqlsh:test> SELECT * FROM table1;
    
     id | age | firstname | lastname
    --------+-----+-----------+----------
     000001 | 20 | John | Doe
    
    (1 rows)

    คำสั่งในการ Delete ข้อมูล

    cqlsh:test> DELETE FROM table1
     ... WHERE id = '000001';
    cqlsh:test> SELECT * FROM table1;
    
     id | age | firstname | lastname
    ----+-----+-----------+----------
    
    (0 rows)

    จะเห็นได้ว่า รูปแบบการใช้คำสั่งแทบจะไม่แตกต่างกับภาษา SQL ใน RDBMS ทั่วไปเลย

    Reference:

    [1] http://cassandra.apache.org/

    [2] http://docs.datastax.com/en/cql/3.1/cql/cql_using/useStartingCqlshTOC.html

    [3] http://docs.datastax.com/en/cassandra/3.0/cassandra/cassandraAbout.html