ติดตั้ง ScyllaDB ด้วย Docker และเชื่อมต่อด้วย Python สำหรับการทำ CRUD เบื้องต้น
การติดตั้ง ScyllaDB ด้วย Docker และเชื่อมต่อด้วย Python สำหรับการทำ CRUD เบื้องต้น มีขั้นตอนดังนี้:
1. ติดตั้ง ScyllaDB ด้วย Docker
- สร้าง
docker-compose.ymlเพื่อใช้ ScyllaDB:
version: '3'
services:
scylla:
image: scylladb/scylla
ports:
- "9042:9042"
environment:
SCYLLA_LOG_TO_STDOUT: "1"
networks:
- scylla-network
networks:
scylla-network:
driver: bridge
- รันคำสั่ง:
docker-compose up -d
2. ติดตั้ง Python Driver
ติดตั้ง cassandra-driver สำหรับเชื่อมต่อ ScyllaDB:
pip install cassandra-driver
3. เขียน Python สำหรับ CRUD
เชื่อมต่อกับ ScyllaDB:
from cassandra.cluster import Cluster
# เชื่อมต่อกับ ScyllaDB
cluster = Cluster(['localhost'])
session = cluster.connect()
# สร้าง keyspace
session.execute("""
CREATE KEYSPACE IF NOT EXISTS mykeyspace
WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '1'}
""")
session.set_keyspace('mykeyspace')
สร้าง Table:
session.execute("""
CREATE TABLE IF NOT EXISTS users (
user_id UUID PRIMARY KEY,
name TEXT,
age INT
)
""")
สร้างข้อมูล (Create):
import uuid
user_id = uuid.uuid4()
session.execute("""
INSERT INTO users (user_id, name, age) VALUES (%s, %s, %s)
""", (user_id, 'Alice', 30))
อ่านข้อมูล (Read):
rows = session.execute("SELECT * FROM users")
for row in rows:
print(row.user_id, row.name, row.age)
อัปเดตข้อมูล (Update):
session.execute("""
UPDATE users SET age = %s WHERE user_id = %s
""", (31, user_id))
ลบข้อมูล (Delete):
session.execute("""
DELETE FROM users WHERE user_id = %s
""", (user_id,))
ตารางเปรียบเทียบ ScyllaDB กับ Cassandra, PostgreSQL, MySQL, และ MongoDB มีดังนี้:
| คุณสมบัติ | ScyllaDB | Cassandra | PostgreSQL | MySQL | MongoDB |
|---|---|---|---|---|---|
| ประเภทฐานข้อมูล | NoSQL, Columnar | NoSQL, Columnar | SQL, Relational | SQL, Relational | NoSQL, Document |
| สถาปัตยกรรม | Distributed, Shared-Nothing | Distributed, Shared-Nothing | Single-instance (Cluster รองรับได้) | Single-instance (Cluster รองรับได้) | Distributed (Sharding รองรับได้) |
| การจัดเก็บข้อมูล | Column-Oriented | Column-Oriented | Row-Oriented | Row-Oriented | Document-Oriented |
| ความสามารถในการขยาย | Horizontal Scaling (auto) | Horizontal Scaling (manual) | Horizontal Scaling (Cluster) | Horizontal Scaling (manual) | Horizontal Scaling (Sharding) |
| การ Replication | Yes (Consistent) | Yes (Consistent) | Yes (Asynchronous/Sync) | Yes (Asynchronous) | Yes (Sharding, Replica Sets) |
| Consistency Level | Tunable (Strong/Eventually) | Tunable (Strong/Eventually) | Strong (ACID) | Strong (ACID) | Tunable (Eventually) |
| Query Language | CQL (Cassandra Query Lang.) | CQL (Cassandra Query Lang.) | SQL | SQL | MongoDB Query Language |
| การรองรับ ACID | Limited (Per Partition) | Limited (Per Partition) | Full ACID | Full ACID | Limited (Per Document) |
| ประสิทธิภาพ | สูง (Low Latency, ใช้ CPU ดี) | ดี (Low Latency) | ขึ้นกับขนาดของ Data | ขึ้นกับขนาดของ Data | สูง (Low Latency) |
| การรองรับ JOIN | ไม่มี | ไม่มี | มี | มี | ไม่มี |
| การรองรับ Schema | Schema-Optional | Schema-Optional | Strict Schema | Strict Schema | Schema-Less |
| การรองรับ Indexing | Secondary Indexing (จำกัด) | Secondary Indexing (จำกัด) | Advanced Indexing | Advanced Indexing | Indexing ดี แต่ไม่ซับซ้อน |
| เหมาะสำหรับ | High Throughput, Low Latency | High Throughput, Low Latency | Complex Queries, ACID Transactions | ACID Transactions, Simple Data | Big Data, Unstructured Data |
จุดเด่น:
- ScyllaDB: ออกแบบให้รองรับการทำงานระดับ distributed ขนาดใหญ่ได้ดีกว่า Cassandra โดยใช้ประโยชน์จาก CPU cores มากขึ้น ทำให้มี latency ต่ำและประสิทธิภาพสูง
- Cassandra: ระบบ distributed ที่เชื่อถือได้ ใช้กันแพร่หลายในงานที่ต้องการ high availability และ scalability แต่ต้องจัดการการขยายขนาดเอง
- PostgreSQL: ระบบฐานข้อมูล relational ที่มีความยืดหยุ่นในการทำงาน ซับซ้อนใน query แต่ขยายขนาดเป็น cluster ได้ดี
- MySQL: ฐานข้อมูล relational ที่ใช้งานง่าย เหมาะสำหรับงานทั่วไปที่ต้องการ ACID
- MongoDB: เหมาะสำหรับข้อมูลที่ไม่เป็นโครงสร้าง และต้องการความยืดหยุ่นในการจัดเก็บข้อมูลแบบ document