| <!DOCTYPE html> |
| <html> |
| |
| |
| |
| |
| <head> |
| <meta charset="utf-8"> |
| <meta http-equiv="X-UA-Compatible" content="IE=edge"> |
| <meta name="viewport" content="width=device-width, initial-scale=1"> |
| <meta name="description" content="The Apache Cassandra database is the right choice when you need scalability and high availability without compromising performance. Linear scalability and proven fault-tolerance on commodity hardware or cloud infrastructure make it the perfect platform for mission-critical data. Cassandra's support for replicating across multiple datacenters is best-in-class, providing lower latency for your users and the peace of mind of knowing that you can survive regional outages. |
| "> |
| <meta name="keywords" content="cassandra, apache, apache cassandra, distributed storage, key value store, scalability, bigtable, dynamo" /> |
| <meta name="robots" content="index,follow" /> |
| <meta name="language" content="en" /> |
| |
| <title>Documentation</title> |
| |
| <link rel="canonical" href="http://cassandra.apache.org/doc/latest/data_modeling/data_modeling_logical.html"> |
| |
| <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous"> |
| <link rel="stylesheet" href="./../../../css/style.css"> |
| |
| <link rel="stylesheet" href="./../../../css/sphinx.css"> |
| |
| |
| <link rel="top" title="Apache Cassandra Documentation v4.0-alpha4" href="../index.html"/> <link rel="up" title="Data Modeling" href="index.html"/> <link rel="next" title="Physical Data Modeling" href="data_modeling_physical.html"/> <link rel="prev" title="Defining Application Queries" href="data_modeling_queries.html"/> |
| <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.2.0/css/all.css" integrity="sha384-hWVjflwFxL6sNzntih27bfxkr27PmbbK/iSvJ+a4+0owXq79v+lsFkW54bOGbiDQ" crossorigin="anonymous"> |
| |
| <link type="application/atom+xml" rel="alternate" href="http://cassandra.apache.org/feed.xml" title="Apache Cassandra Website" /> |
| </head> |
| |
| <body> |
| <!-- breadcrumbs --> |
| <div class="topnav"> |
| <div class="container breadcrumb-container"> |
| <ul class="breadcrumb"> |
| <li> |
| <div class="dropdown"> |
| <img class="asf-logo" src="./../../../img/asf_feather.png" /> |
| <a data-toggle="dropdown" href="#">Apache Software Foundation <span class="caret"></span></a> |
| <ul class="dropdown-menu" role="menu" aria-labelledby="dLabel"> |
| <li><a href="http://www.apache.org">Apache Homepage</a></li> |
| <li><a href="http://www.apache.org/licenses/">License</a></li> |
| <li><a href="http://www.apache.org/foundation/sponsorship.html">Sponsorship</a></li> |
| <li><a href="http://www.apache.org/foundation/thanks.html">Thanks</a></li> |
| <li><a href="http://www.apache.org/security/">Security</a></li> |
| </ul> |
| </div> |
| </li> |
| |
| |
| <li><a href="./../../../">Apache Cassandra</a></li> |
| |
| |
| |
| |
| <li><a href="./../../../doc/latest/">Documentation</a></li> |
| |
| |
| |
| |
| <li><a href="./">Data Modeling</a></li> |
| |
| |
| |
| <li>Logical Data Modeling</li> |
| |
| </ul> |
| </div> |
| |
| <!-- navbar --> |
| <nav class="navbar navbar-default navbar-static-top" role="navigation"> |
| <div class="container"> |
| <div class="navbar-header"> |
| <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#cassandra-menu" aria-expanded="false"> |
| <span class="sr-only">Toggle navigation</span> |
| <span class="icon-bar"></span> |
| <span class="icon-bar"></span> |
| <span class="icon-bar"></span> |
| </button> |
| <a class="navbar-brand" href="./../../../"><img src="./../../../img/cassandra_logo.png" alt="Apache Cassandra logo" /></a> |
| </div><!-- /.navbar-header --> |
| |
| <div id="cassandra-menu" class="collapse navbar-collapse"> |
| <ul class="nav navbar-nav navbar-right"> |
| <li><a href="./../../../">Home</a></li> |
| <li><a href="./../../../download/">Download</a></li> |
| <li><a href="./../../../doc/latest/">Documentation</a></li> |
| <li><a href="./../../../community/">Community</a></li> |
| <li> |
| <a href="./../../../blog/">Blog</a> |
| </li> |
| </ul> |
| </div><!-- /#cassandra-menu --> |
| |
| |
| </div> |
| </nav><!-- /.navbar --> |
| </div><!-- /.topnav --> |
| |
| <div class="container-fluid"> |
| <div class="row"> |
| <div class="col-md-3"> |
| <div class="doc-navigation"> |
| <div class="doc-menu" role="navigation"> |
| <div class="navbar-header"> |
| <button type="button" class="pull-left navbar-toggle" data-toggle="collapse" data-target=".sidebar-navbar-collapse"> |
| <span class="sr-only">Toggle navigation</span> |
| <span class="icon-bar"></span> |
| <span class="icon-bar"></span> |
| <span class="icon-bar"></span> |
| </button> |
| </div> |
| <div class="navbar-collapse collapse sidebar-navbar-collapse"> |
| <form id="doc-search-form" class="navbar-form" action="../search.html" method="get" role="search"> |
| <div class="form-group"> |
| <input type="text" size="30" class="form-control input-sm" name="q" placeholder="Search docs"> |
| <input type="hidden" name="check_keywords" value="yes" /> |
| <input type="hidden" name="area" value="default" /> |
| </div> |
| </form> |
| |
| |
| |
| <ul class="current"> |
| <li class="toctree-l1"><a class="reference internal" href="../getting_started/index.html">Getting Started</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="../new/index.html">New Features in Apache Cassandra 4.0</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="../architecture/index.html">Architecture</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="../cql/index.html">The Cassandra Query Language (CQL)</a></li> |
| <li class="toctree-l1 current"><a class="reference internal" href="index.html">Data Modeling</a><ul class="current"> |
| <li class="toctree-l2"><a class="reference internal" href="intro.html">Introduction</a></li> |
| <li class="toctree-l2"><a class="reference internal" href="data_modeling_conceptual.html">Conceptual Data Modeling</a></li> |
| <li class="toctree-l2"><a class="reference internal" href="data_modeling_rdbms.html">RDBMS Design</a></li> |
| <li class="toctree-l2"><a class="reference internal" href="data_modeling_queries.html">Defining Application Queries</a></li> |
| <li class="toctree-l2 current"><a class="current reference internal" href="#">Logical Data Modeling</a><ul> |
| <li class="toctree-l3"><a class="reference internal" href="#hotel-logical-data-model">Hotel Logical Data Model</a></li> |
| <li class="toctree-l3"><a class="reference internal" href="#reservation-logical-data-model">Reservation Logical Data Model</a></li> |
| <li class="toctree-l3"><a class="reference internal" href="#patterns-and-anti-patterns">Patterns and Anti-Patterns</a></li> |
| </ul> |
| </li> |
| <li class="toctree-l2"><a class="reference internal" href="data_modeling_physical.html">Physical Data Modeling</a></li> |
| <li class="toctree-l2"><a class="reference internal" href="data_modeling_refining.html">Evaluating and Refining Data Models</a></li> |
| <li class="toctree-l2"><a class="reference internal" href="data_modeling_schema.html">Defining Database Schema</a></li> |
| <li class="toctree-l2"><a class="reference internal" href="data_modeling_tools.html">Cassandra Data Modeling Tools</a></li> |
| </ul> |
| </li> |
| <li class="toctree-l1"><a class="reference internal" href="../configuration/index.html">Configuring Cassandra</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="../operating/index.html">Operating Cassandra</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="../tools/index.html">Cassandra Tools</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="../troubleshooting/index.html">Troubleshooting</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="../development/index.html">Contributing to Cassandra</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="../faq/index.html">Frequently Asked Questions</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="../plugins/index.html">Third-Party Plugins</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="../bugs.html">Reporting Bugs</a></li> |
| <li class="toctree-l1"><a class="reference internal" href="../contactus.html">Contact us</a></li> |
| </ul> |
| |
| |
| |
| </div><!--/.nav-collapse --> |
| </div> |
| </div> |
| </div> |
| <div class="col-md-8"> |
| <div class="content doc-content"> |
| <div class="content-container"> |
| |
| <div class="section" id="logical-data-modeling"> |
| <h1>Logical Data Modeling<a class="headerlink" href="#logical-data-modeling" title="Permalink to this headline">¶</a></h1> |
| <p>Now that you have defined your queries, you’re ready to begin designing |
| Cassandra tables. First, create a logical model containing a table |
| for each query, capturing entities and relationships from the conceptual |
| model.</p> |
| <p>To name each table, you’ll identify the primary entity type for which you |
| are querying and use that to start the entity name. If you are querying |
| by attributes of other related entities, append those to the table |
| name, separated with <code class="docutils literal notranslate"><span class="pre">_by_</span></code>. For example, <code class="docutils literal notranslate"><span class="pre">hotels_by_poi</span></code>.</p> |
| <p>Next, you identify the primary key for the table, adding partition key |
| columns based on the required query attributes, and clustering columns |
| in order to guarantee uniqueness and support desired sort ordering.</p> |
| <p>The design of the primary key is extremely important, as it will |
| determine how much data will be stored in each partition and how that |
| data is organized on disk, which in turn will affect how quickly |
| Cassandra processes reads.</p> |
| <p>Complete each table by adding any additional attributes identified by |
| the query. If any of these additional attributes are the same for every |
| instance of the partition key, mark the column as static.</p> |
| <p>Now that was a pretty quick description of a fairly involved process, so |
| it will be worthwhile to work through a detailed example. First, |
| let’s introduce a notation that you can use to represent logical |
| models.</p> |
| <p>Several individuals within the Cassandra community have proposed |
| notations for capturing data models in diagrammatic form. This document |
| uses a notation popularized by Artem Chebotko which provides a simple, |
| informative way to visualize the relationships between queries and |
| tables in your designs. This figure shows the Chebotko notation for a |
| logical data model.</p> |
| <img alt="../_images/data_modeling_chebotko_logical.png" src="../_images/data_modeling_chebotko_logical.png" /> |
| <p>Each table is shown with its title and a list of columns. Primary key |
| columns are identified via symbols such as <strong>K</strong> for partition key |
| columns and <strong>C</strong>↑ or <strong>C</strong>↓ to represent clustering columns. Lines |
| are shown entering tables or between tables to indicate the queries that |
| each table is designed to support.</p> |
| <div class="section" id="hotel-logical-data-model"> |
| <h2>Hotel Logical Data Model<a class="headerlink" href="#hotel-logical-data-model" title="Permalink to this headline">¶</a></h2> |
| <p>The figure below shows a Chebotko logical data model for the queries |
| involving hotels, points of interest, rooms, and amenities. One thing you’ll |
| notice immediately is that the Cassandra design doesn’t include dedicated |
| tables for rooms or amenities, as you had in the relational design. This |
| is because the workflow didn’t identify any queries requiring this |
| direct access.</p> |
| <img alt="../_images/data_modeling_hotel_logical.png" src="../_images/data_modeling_hotel_logical.png" /> |
| <p>Let’s explore the details of each of these tables.</p> |
| <p>The first query Q1 is to find hotels near a point of interest, so you’ll |
| call this table <code class="docutils literal notranslate"><span class="pre">hotels_by_poi</span></code>. Searching by a named point of |
| interest is a clue that the point of interest should be a part |
| of the primary key. Let’s reference the point of interest by name, |
| because according to the workflow that is how users will start their |
| search.</p> |
| <p>You’ll note that you certainly could have more than one hotel near a |
| given point of interest, so you’ll need another component in the primary |
| key in order to make sure you have a unique partition for each hotel. So |
| you add the hotel key as a clustering column.</p> |
| <p>An important consideration in designing your table’s primary key is |
| making sure that it defines a unique data element. Otherwise you run the |
| risk of accidentally overwriting data.</p> |
| <p>Now for the second query (Q2), you’ll need a table to get information |
| about a specific hotel. One approach would have been to put all of the |
| attributes of a hotel in the <code class="docutils literal notranslate"><span class="pre">hotels_by_poi</span></code> table, but you added |
| only those attributes that were required by the application workflow.</p> |
| <p>From the workflow diagram, you know that the <code class="docutils literal notranslate"><span class="pre">hotels_by_poi</span></code> table is |
| used to display a list of hotels with basic information on each hotel, |
| and the application knows the unique identifiers of the hotels returned. |
| When the user selects a hotel to view details, you can then use Q2, which |
| is used to obtain details about the hotel. Because you already have the |
| <code class="docutils literal notranslate"><span class="pre">hotel_id</span></code> from Q1, you use that as a reference to the hotel you’re |
| looking for. Therefore the second table is just called <code class="docutils literal notranslate"><span class="pre">hotels</span></code>.</p> |
| <p>Another option would have been to store a set of <code class="docutils literal notranslate"><span class="pre">poi_names</span></code> in the |
| hotels table. This is an equally valid approach. You’ll learn through |
| experience which approach is best for your application.</p> |
| <p>Q3 is just a reverse of Q1—looking for points of interest near a hotel, |
| rather than hotels near a point of interest. This time, however, you need |
| to access the details of each point of interest, as represented by the |
| <code class="docutils literal notranslate"><span class="pre">pois_by_hotel</span></code> table. As previously, you add the point of |
| interest name as a clustering key to guarantee uniqueness.</p> |
| <p>At this point, let’s now consider how to support query Q4 to help the |
| user find available rooms at a selected hotel for the nights they are |
| interested in staying. Note that this query involves both a start date |
| and an end date. Because you’re querying over a range instead of a single |
| date, you know that you’ll need to use the date as a clustering key. |
| Use the <code class="docutils literal notranslate"><span class="pre">hotel_id</span></code> as a primary key to group room data for each hotel |
| on a single partition, which should help searches be super fast. Let’s |
| call this the <code class="docutils literal notranslate"><span class="pre">available_rooms_by_hotel_date</span></code> table.</p> |
| <p>To support searching over a range, use <a class="reference internal" href="../cql/ddl.html#clustering-columns"><span class="std std-ref">clustering columns</span></a> to store |
| attributes that you need to access in a range query. Remember that the |
| order of the clustering columns is important.</p> |
| <p>The design of the <code class="docutils literal notranslate"><span class="pre">available_rooms_by_hotel_date</span></code> table is an instance |
| of the <strong>wide partition</strong> pattern. This |
| pattern is sometimes called the <strong>wide row</strong> pattern when discussing |
| databases that support similar models, but wide partition is a more |
| accurate description from a Cassandra perspective. The essence of the |
| pattern is to group multiple related rows in a partition in order to |
| support fast access to multiple rows within the partition in a single |
| query.</p> |
| <p>In order to round out the shopping portion of the data model, add the |
| <code class="docutils literal notranslate"><span class="pre">amenities_by_room</span></code> table to support Q5. This will allow users to |
| view the amenities of one of the rooms that is available for the desired |
| stay dates.</p> |
| </div> |
| <div class="section" id="reservation-logical-data-model"> |
| <h2>Reservation Logical Data Model<a class="headerlink" href="#reservation-logical-data-model" title="Permalink to this headline">¶</a></h2> |
| <p>Now let’s switch gears to look at the reservation queries. The figure |
| shows a logical data model for reservations. You’ll notice that these |
| tables represent a denormalized design; the same data appears in |
| multiple tables, with differing keys.</p> |
| <img alt="../_images/data_modeling_reservation_logical.png" src="../_images/data_modeling_reservation_logical.png" /> |
| <p>In order to satisfy Q6, the <code class="docutils literal notranslate"><span class="pre">reservations_by_guest</span></code> table can be used |
| to look up the reservation by guest name. You could envision query Q7 |
| being used on behalf of a guest on a self-serve website or a call center |
| agent trying to assist the guest. Because the guest name might not be |
| unique, you include the guest ID here as a clustering column as well.</p> |
| <p>Q8 and Q9 in particular help to remind you to create queries |
| that support various stakeholders of the application, not just customers |
| but staff as well, and perhaps even the analytics team, suppliers, and so |
| on.</p> |
| <p>The hotel staff might wish to see a record of upcoming reservations by |
| date in order to get insight into how the hotel is performing, such as |
| what dates the hotel is sold out or undersold. Q8 supports the retrieval |
| of reservations for a given hotel by date.</p> |
| <p>Finally, you create a <code class="docutils literal notranslate"><span class="pre">guests</span></code> table. This provides a single |
| location that used to store guest information. In this case, you specify a |
| separate unique identifier for guest records, as it is not uncommon |
| for guests to have the same name. In many organizations, a customer |
| database such as the <code class="docutils literal notranslate"><span class="pre">guests</span></code> table would be part of a separate |
| customer management application, which is why other guest |
| access patterns were omitted from the example.</p> |
| </div> |
| <div class="section" id="patterns-and-anti-patterns"> |
| <h2>Patterns and Anti-Patterns<a class="headerlink" href="#patterns-and-anti-patterns" title="Permalink to this headline">¶</a></h2> |
| <p>As with other types of software design, there are some well-known |
| patterns and anti-patterns for data modeling in Cassandra. You’ve already |
| used one of the most common patterns in this hotel model—the wide |
| partition pattern.</p> |
| <p>The <strong>time series</strong> pattern is an extension of the wide partition |
| pattern. In this pattern, a series of measurements at specific time |
| intervals are stored in a wide partition, where the measurement time is |
| used as part of the partition key. This pattern is frequently used in |
| domains including business analysis, sensor data management, and |
| scientific experiments.</p> |
| <p>The time series pattern is also useful for data other than measurements. |
| Consider the example of a banking application. You could store each |
| customer’s balance in a row, but that might lead to a lot of read and |
| write contention as various customers check their balance or make |
| transactions. You’d probably be tempted to wrap a transaction around |
| writes just to protect the balance from being updated in error. In |
| contrast, a time series–style design would store each transaction as a |
| timestamped row and leave the work of calculating the current balance to |
| the application.</p> |
| <p>One design trap that many new users fall into is attempting to use |
| Cassandra as a queue. Each item in the queue is stored with a timestamp |
| in a wide partition. Items are appended to the end of the queue and read |
| from the front, being deleted after they are read. This is a design that |
| seems attractive, especially given its apparent similarity to the time |
| series pattern. The problem with this approach is that the deleted items |
| are now <a class="reference internal" href="../faq/index.html#asynch-deletes"><span class="std std-ref">tombstones</span></a> that Cassandra must scan past |
| in order to read from the front of the queue. Over time, a growing number |
| of tombstones begins to degrade read performance.</p> |
| <p>The queue anti-pattern serves as a reminder that any design that relies |
| on the deletion of data is potentially a poorly performing design.</p> |
| <p><em>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.</em></p> |
| </div> |
| </div> |
| |
| |
| |
| |
| <div class="doc-prev-next-links" role="navigation" aria-label="footer navigation"> |
| |
| <a href="data_modeling_physical.html" class="btn btn-default pull-right " role="button" title="Physical Data Modeling" accesskey="n">Next <span class="glyphicon glyphicon-circle-arrow-right" aria-hidden="true"></span></a> |
| |
| |
| <a href="data_modeling_queries.html" class="btn btn-default" role="button" title="Defining Application Queries" accesskey="p"><span class="glyphicon glyphicon-circle-arrow-left" aria-hidden="true"></span> Previous</a> |
| |
| </div> |
| |
| </div> |
| </div> |
| </div> |
| </div> |
| </div> |
| |
| <hr /> |
| |
| <footer> |
| <div class="container"> |
| <div class="col-md-4 social-blk"> |
| <span class="social"> |
| <a href="https://twitter.com/cassandra" |
| class="twitter-follow-button" |
| data-show-count="false" data-size="large">Follow @cassandra</a> |
| <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script> |
| <a href="https://twitter.com/intent/tweet?button_hashtag=cassandra" |
| class="twitter-hashtag-button" |
| data-size="large" |
| data-related="ApacheCassandra">Tweet #cassandra</a> |
| <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script> |
| |
| </span> |
| <a class="subscribe-rss icon-link" href="/feed.xml" title="Subscribe to Blog via RSS"> |
| <span><i class="fa fa-rss"></i></span> |
| </a> |
| </div> |
| |
| <div class="col-md-8 trademark"> |
| <p>© 2016 <a href="http://apache.org">The Apache Software Foundation</a>. |
| Apache, the Apache feather logo, and Apache Cassandra are trademarks of The Apache Software Foundation. |
| <p> |
| </div> |
| </div><!-- /.container --> |
| </footer> |
| |
| <!-- Javascript. Placed here so pages load faster --> |
| <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> |
| <script src="./../../../js/underscore-min.js"></script> |
| <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script> |
| |
| |
| <script src="./../../../js/doctools.js"></script> |
| <script src="./../../../js/searchtools.js"></script> |
| |
| <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: "", VERSION: "", COLLAPSE_INDEX: false, FILE_SUFFIX: ".html", HAS_SOURCE: false, SOURCELINK_SUFFIX: ".txt" }; </script> |
| |
| |
| |
| <script type="text/javascript"> |
| var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www."); |
| document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E")); |
| |
| try { |
| var pageTracker = _gat._getTracker("UA-11583863-1"); |
| pageTracker._trackPageview(); |
| } catch(err) {} |
| </script> |
| |
| |
| </body> |
| </html> |