데이터베이스 스키마 정의

데이터베이스 스키마 정의 (Defining database schema)

물리적 모델을 평가하고 다듬는 작업을 마치면 CQL로 스키마를 구현할 준비가 된 거예요. 다음은 hotel 키스페이스의 스키마로, 각 테이블이 지원하는 쿼리 패턴을 문서화하기 위해 CQL의 comment 기능을 사용했어요.

출처: Defining database schema

본문

CREATE KEYSPACE hotel WITH replication =
  {'class': 'SimpleStrategy', 'replication_factor' : 3};

CREATE TYPE hotel.address (
  street text,
  city text,
  state_or_province text,
  postal_code text,
  country text );

CREATE TABLE hotel.hotels_by_poi (
  poi_name text,
  hotel_id text,
  name text,
  phone text,
  address frozen<address>,
  PRIMARY KEY ((poi_name), hotel_id) )
  WITH comment = 'Q1. Find hotels near given poi'
  AND CLUSTERING ORDER BY (hotel_id ASC) ;

CREATE TABLE hotel.hotels (
  id text PRIMARY KEY,
  name text,
  phone text,
  address frozen<address>,
  pois set<text> )
  WITH comment = 'Q2. Find information about a hotel';

CREATE TABLE hotel.pois_by_hotel (
  poi_name text,
  hotel_id text,
  description text,
  PRIMARY KEY ((hotel_id), poi_name) )
  WITH comment = Q3. Find pois near a hotel';

CREATE TABLE hotel.available_rooms_by_hotel_date (
  hotel_id text,
  date date,
  room_number smallint,
  is_available boolean,
  PRIMARY KEY ((hotel_id), date, room_number) )
  WITH comment = 'Q4. Find available rooms by hotel date';

CREATE TABLE hotel.amenities_by_room (
  hotel_id text,
  room_number smallint,
  amenity_name text,
  description text,
  PRIMARY KEY ((hotel_id, room_number), amenity_name) )
  WITH comment = 'Q5. Find amenities for a room';

파티션 키가 단일 컬럼인 poi_name으로만 구성됨에도 불구하고, 파티션 키의 요소가 괄호로 감싸져 있는 것에 주목하세요. 이는 다른 사람이 여러분의 CQL을 읽을 때 파티션 키 선택을 더 명확하게 만드는 모범 사례예요.

마찬가지로 다음은 reservation 키스페이스의 스키마예요.

CREATE KEYSPACE reservation WITH replication = {'class':
  'SimpleStrategy', 'replication_factor' : 3};

CREATE TYPE reservation.address (
  street text,
  city text,
  state_or_province text,
  postal_code text,
  country text );

CREATE TABLE reservation.reservations_by_confirmation (
  confirm_number text,
  hotel_id text,
  start_date date,
  end_date date,
  room_number smallint,
  guest_id uuid,
  PRIMARY KEY (confirm_number) )
  WITH comment = 'Q6. Find reservations by confirmation number';

CREATE TABLE reservation.reservations_by_hotel_date (
  hotel_id text,
  start_date date,
  end_date date,
  room_number smallint,
  confirm_number text,
  guest_id uuid,
  PRIMARY KEY ((hotel_id, start_date), room_number) )
  WITH comment = 'Q7. Find reservations by hotel and date';

CREATE TABLE reservation.reservations_by_guest (
  guest_last_name text,
  hotel_id text,
  start_date date,
  end_date date,
  room_number smallint,
  confirm_number text,
  guest_id uuid,
  PRIMARY KEY ((guest_last_name), hotel_id) )
  WITH comment = 'Q8. Find reservations by guest name';

CREATE TABLE reservation.guests (
  guest_id uuid PRIMARY KEY,
  first_name text,
  last_name text,
  title text,
  emails set<text>,
  phone_numbers list<text>,
  addresses map<text, frozen<address>>,
  confirm_number text
) WITH comment = 'Q9. Find guest by ID';

이제 호텔 애플리케이션의 데이터를 저장하기 위한 완전한 Cassandra 스키마가 준비됐어요.

이 자료는 Cassandra, The Definitive Guide에서 각색한 내용이에요. O'Reilly Media, Inc. 발행. Copyright © 2020 Jeff Carpenter, Eben Hewitt. All rights reserved. 허가를 받아 사용했어요.

더 알아보기 (Learn more)