Defining database schema

Once you have finished evaluating and refining the physical model, you’re ready to implement the schema in CQL. Here is the schema for the hotel keyspace, using CQL’s comment feature to document the query pattern supported by each table:

  1. CREATE KEYSPACE hotel WITH replication =
  2. {'class': 'SimpleStrategy', 'replication_factor' : 3};
  3. CREATE TYPE hotel.address (
  4. street text,
  5. city text,
  6. state_or_province text,
  7. postal_code text,
  8. country text );
  9. CREATE TABLE hotel.hotels_by_poi (
  10. poi_name text,
  11. hotel_id text,
  12. name text,
  13. phone text,
  14. address frozen<address>,
  15. PRIMARY KEY ((poi_name), hotel_id) )
  16. WITH comment = 'Q1. Find hotels near given poi'
  17. AND CLUSTERING ORDER BY (hotel_id ASC) ;
  18. CREATE TABLE hotel.hotels (
  19. id text PRIMARY KEY,
  20. name text,
  21. phone text,
  22. address frozen<address>,
  23. pois set<text> )
  24. WITH comment = 'Q2. Find information about a hotel';
  25. CREATE TABLE hotel.pois_by_hotel (
  26. poi_name text,
  27. hotel_id text,
  28. description text,
  29. PRIMARY KEY ((hotel_id), poi_name) )
  30. WITH comment = Q3. Find pois near a hotel';
  31. CREATE TABLE hotel.available_rooms_by_hotel_date (
  32. hotel_id text,
  33. date date,
  34. room_number smallint,
  35. is_available boolean,
  36. PRIMARY KEY ((hotel_id), date, room_number) )
  37. WITH comment = 'Q4. Find available rooms by hotel date';
  38. CREATE TABLE hotel.amenities_by_room (
  39. hotel_id text,
  40. room_number smallint,
  41. amenity_name text,
  42. description text,
  43. PRIMARY KEY ((hotel_id, room_number), amenity_name) )
  44. WITH comment = 'Q5. Find amenities for a room';

Notice that the elements of the partition key are surrounded with parentheses, even though the partition key consists of the single column poi_name. This is a best practice that makes the selection of partition key more explicit to others reading your CQL.

Similarly, here is the schema for the reservation keyspace:

  1. CREATE KEYSPACE reservation WITH replication = {'class':
  2. 'SimpleStrategy', 'replication_factor' : 3};
  3. CREATE TYPE reservation.address (
  4. street text,
  5. city text,
  6. state_or_province text,
  7. postal_code text,
  8. country text );
  9. CREATE TABLE reservation.reservations_by_confirmation (
  10. confirm_number text,
  11. hotel_id text,
  12. start_date date,
  13. end_date date,
  14. room_number smallint,
  15. guest_id uuid,
  16. PRIMARY KEY (confirm_number) )
  17. WITH comment = 'Q6. Find reservations by confirmation number';
  18. CREATE TABLE reservation.reservations_by_hotel_date (
  19. hotel_id text,
  20. start_date date,
  21. end_date date,
  22. room_number smallint,
  23. confirm_number text,
  24. guest_id uuid,
  25. PRIMARY KEY ((hotel_id, start_date), room_number) )
  26. WITH comment = 'Q7. Find reservations by hotel and date';
  27. CREATE TABLE reservation.reservations_by_guest (
  28. guest_last_name text,
  29. hotel_id text,
  30. start_date date,
  31. end_date date,
  32. room_number smallint,
  33. confirm_number text,
  34. guest_id uuid,
  35. PRIMARY KEY ((guest_last_name), hotel_id) )
  36. WITH comment = 'Q8. Find reservations by guest name';
  37. CREATE TABLE reservation.guests (
  38. guest_id uuid PRIMARY KEY,
  39. first_name text,
  40. last_name text,
  41. title text,
  42. emails set,
  43. phone_numbers list,
  44. addresses map<text,
  45. frozen<address>,
  46. confirm_number text )
  47. WITH comment = 'Q9. Find guest by ID';

You now have a complete Cassandra schema for storing data for a hotel application.

Material adapted from Cassandra, The Definitive Guide. Published by O’Reilly Media, Inc. Copyright © 2020 Jeff Carpenter, Eben Hewitt. All rights reserved. Used with permission.