BOOKS FOR PROFESSIONALS BY PROFESSIONALS® Mishra Beginning Apache Cassandra RELATED Development Beginning Apache Cassandra Development introduces you to one of the most robust and best-performing NoSQL database platforms on the planet. Apache Cassandra is a document database following the JSON document model. It is specifically designed to manage large amounts of data across many commodity servers without there being any single point of failure. This design approach makes Apache Cassandra a robust and easy-to-implement platform when high availability is needed. Apache Cassandra can be used by developers in Java, PHP, Python, and JavaScript—the primary and most commonly used languages. In Beginning Apache Cassandra Development, author and Cassandra expert Vivek Mishra takes you through using Apache Cassandra from each of these primary languages. Mishra also covers the Cassandra Query Language (CQL), the Apache Cassandra analog to SQL. You’ll learn to develop applications sourcing data from Cassandra, query that data, and deliver it at speed to your application’s users. In this book, you will learn to • Configure Apache Cassandra clusters • Model your data for high throughput • Implement MapReduce algorithms • Run Hive and Pig queries over Cassandra • Query with the Cassandra Query Language • Build graph-based solutions with Cassandra Titan • Back up your data and restore when needed • Encrypt and secure your data Cassandra is one of the leading NoSQL databases, meaning you get unparalleled throughput and performance without the sort of processing overhead that comes with traditional proprietary databases. Beginning Apache Cassandra Development will therefore help you create applications that generate search results quickly, stand up to high levels of demand, scale as your user base grows, ensure operational simplicity, and—not least—provide delightful user experiences. Shelve in ISBN 978-1-4842-0143-5 Databases/General 54999 User level: Beginning–Intermediate SOURCE CODE ONLINE 9781484201435 www.apress.com www.it-ebooks.info For your convenience Apress has placed some of the front matter material after the index. Please use the Bookmarks and Contents at a Glance links to access them. www.it-ebooks.info Contents at a Glance About the Author ����������������������������������������������������������������������������������������������������������������xv About the Technical Reviewer ������������������������������������������������������������������������������������������xvii Acknowledgments �������������������������������������������������������������������������������������������������������������xix Introduction �����������������������������������������������������������������������������������������������������������������������xxi ■ Chapter 1: NoSQL: Cassandra Basics ��������������������������������������������������������������������������������1 ■ Chapter 2: Cassandra Data Modeling ������������������������������������������������������������������������������27 ■ Chapter 3: Indexes and Composite Columns �������������������������������������������������������������������43 ■ Chapter 4: Cassandra Data Security ��������������������������������������������������������������������������������61 ■ Chapter 5: MapReduce with Cassandra ��������������������������������������������������������������������������79 ■ Chapter 6: Data Migration and Analytics �������������������������������������������������������������������������97 ■ Chapter 7: Titan Graph Databases with Cassandra �������������������������������������������������������123 ■ Chapter 8: Cassandra Performance Tuning �������������������������������������������������������������������153 ■ Chapter 9: Cassandra: Administration and Monitoring �������������������������������������������������171 ■ Chapter 10: Cassandra Utilities �������������������������������������������������������������������������������������191 ■ Chapter 11: Upgrading Cassandra and Troubleshooting �����������������������������������������������209 Index ���������������������������������������������������������������������������������������������������������������������������������217 v www.it-ebooks.info Introduction Big or large data has been the talk of the town in recent years. With possibilities for solving unstructured and semi-structured data issues, more and more organizations are gradually moving toward big data powered solutions. This essentially gives organization a way to think “beyond RDBMS.” This book will walk you through many such use cases during the journey. Many NoSQL databases have been developed over the last 4-5 years. Recent research shows there are now more than 150 different NoSQL databases. This raises questions about why to adopt a specific database. For example, is it scalable, under active development, and most importantly accepted by the community and organizations? It is in light of these questions that Apache Cassandra comes out as a winner and indicates why it is one of the most popular NoSQL databases currently in use. Apache Cassandra is a columnar distributed database that takes database application development forward from the point at which we encounter the limitations of traditional RDBMSs in terms of performance and scalability. A few things that restrict traditional RDBMSs are that they require predefined schemas, the ability to scale up to hundreds of data nodes, and the amount of work involved with data administration and monitoring. We will discuss these restrictions and how to address these with Apache Cassandra. Beginning Apache Cassandra Development introduces you to Apache Cassandra, including the answers to the questions mentioned above, and provides a detailed overview and explanation of its feature set. Beginning with Cassandra basics, this book will walk you through the following topics and more: • Data modeling • Cluster deployment, logging, and monitoring • Performance tuning • Batch processing via MapReduce • Hive and Pig integration • Working on graph-based solutions • Open source tools for Cassandra and related utilities The book is intended for database administrators, big data developers, students, big data solution architects, and technology decision makers who are planning to use or are already using Apache Cassandra. Many of the features and concepts covered in this book are approached through hands on recipes that show how things are done. In addition to those step-by-step guides, the source code for the examples is available as a download from the book’s Apress product page (www.apress.com/9781484201435). xxi www.it-ebooks.info Chapter 1 NoSQL: Cassandra Basics The purpose of this chapter is to discuss NoSQL, let users dive into NoSQL elements, and then introduce big data problems, distributed database concepts, and finally Cassandra concepts. Topics covered in this chapter are: • NoSQL introduction • CAP theorem • Data distribution concepts • Big data problems • Cassandra configurations • Cassandra storage architecture • Setup and installation • Logging with Cassandra The intent of the detailed introductory chapter is to dive deep into the NoSQL ecosystem by discussing problems and solutions, such as distributed programming concepts, which can help in solving scalability, availability, and other data-related problems. This chapter will introduce the reader to Cassandra and discuss Cassandra’s storage architecture, various other configurations, and the Cassandra cluster setup over local and AWS boxes. Introducing NoSQL Big data’s existence can be traced back to the mid 1990s. However, the actual shift began in the early 2000s. The evolution of the Internet and mobile technology opened many doors for more people to participate and share data globally. This resulted in massive data production, in various formats, flowing across the globe. A wider distributed network resulted in incremental data growth. Due to this massive data generation, there is a major shift in application development and many new domain business possibilities have emerged, like: • Social trending • OLAP and Data mining • Sentiment analysis • Behavior targeting • Real-time data analysis 1 www.it-ebooks.info Chapter 1 ■ NoSQL: CaSSaNdra BaSiCS With high data growth into peta/zeta bytes, challenges like scalability and managing data structure would be very difficult with traditional relational databases. Here big data and NoSQL technologies are considered an alternative to building solutions. In today’s scenario, existing business domains are also exploring the possibilities of new functional aspects and handling massive data growth simultaneously. NoSQL Ecosystem NoSQL, often called “Not Only SQL,” implies thinking beyond traditional SQL in a distributed way. There are more than 150 NoSQL databases available today. The following are a few popular databases: • Columnar databases, such as Cassandra & HBase • Document based storage like MongoDB & Couchbase • Graph based access like Neo4J & Titan Graph DB • Simple key-value store like Redis & Couch DB With so many options and categories, the most important question is, what, how, and why to choose! Each NoSQL database category is meant to deal with a specific set of problems. Specific technology for specific requirement paradigm is leading the current era of technology. It is certain that a single database for all business needs is clearly not a solution, and that’s where the need for NoSQL databases arises. The best way to adopt databases is to understand the requirements first. If the application is polyglot in nature, then you may need to choose more than one database from the available options. In the next section, we will discuss a few points that describe why Cassandra could be an answer to your big data problem. CAP Theorem CAP theorem, which was introduced in early 2000 by Eric Brewer, states that no database can offer Consistency, Availability, and Partition tolerance together (see Figure 1-1), but depending on use case may allow for any two of them. Figure 1-1. CAP theorem excludes the possibility of a database with all three characteristics (the “NA” area) 2 www.it-ebooks.info Chapter 1 ■ NoSQL: CaSSaNdra BaSiCS Traditional relational database management systems (RDBMS) provide atomicity, consistency, isolation, and durability (ACID) semantics and advocate for strong consistency. That’s where most of NoSQL databases differ and strongly advocate for partition tolerance and high availability with eventual consistency. High availability of data means data must be available with minimal latency. For distributed databases where data is distributed across multiple nodes, one way to achieve high availability is to replicate it across multiple nodes. Like most of NoSQL databases, Cassandra also provides high availability. Partition tolerance implies if a node or couple of nodes is down, the system would still be able to serve read/write requests. In scalable systems, built to deal with a massive volume of data (in peta bytes) it is highly likely that situations may occur often. Hence, such systems have to be partition tolerant. Cassandra’s storage architecture enables this as well. Consistency means consistent across distributed nodes. Strong consistency refers to most updated or consistent data on each node in a cluster. On each read/write request most stable rows can be read or written to by introducing latency (downside of NoSQL) on each read and write request, ensuring synchronized data on all the replicas. Cassandra offers eventual consistency, and levels of configuration consistency for each read/write request. We will discuss various consistency level options in detail in the coming chapters. Budding Schema Structured or fixed schema defines the number of columns and data types before implementation. Any alteration to schema like adding column(s) would require a migration plan across the schema. For semistructured or unstructured data formats where number of columns and data types may vary across multiple rows, static schema doesn’t fit very well. That’s where budding or dynamic schema is best fit for semistructured or unstructured data. Figure 1-2 presents four records containing twitter-like data for a particular user id. Here, the user id imvivek consists of three columns “tweet body”, ”followers”, and “retweeted by”. But on the row for user “apress_team” there is only the column followers. For unstructured schema such as server logs, the number of fields may vary from row to row. This requires the addition of columns “on the fly” a strong requirement for NoSQL databases. Traditional RDBMS can handle such data set in a static way, but unlike Cassandra RDBMS cannot scale to have up to a million columns per row in each partition. With predefined models in the RDBMS world, handling frequent schema changes is certainly not a workable option. Imagine if we attempt to support dynamic columns we may end up having many null columns! Having default null values for multiple columns per row is certainly not desirable. With Cassandra we can have as many columns as we want (up to 2 billion)! Also another possible option is to define datatype for column names (comparator) which is not possible with RDBMS (to have a column name of type integer). 3 www.it-ebooks.info Chapter 1 ■ NoSQL: CaSSaNdra BaSiCS Figure 1-2. A dynamic column, a.k.a. budding schema, is one way to relax static schema constraint of RDBMS world Scalability Traditional RDBMSs offer vertical scalability, that is, scaling by adding more processors or RAM to a single unit. Whereas, NoSQL databases offer horizontal scalability, and add more nodes. Mostly NoSQL databases are schemaless and can perform well over commodity servers. Adding nodes to an existing RDBMS cluster is a cumbersome process and relatively expensive whereas it is relatively easy to add data nodes with a NoSQL database, such as Cassandra. We will discuss adding nodes to Cassandra in coming chapters. No Single Point of Failure With centralized databases or master/slave architectures, where database resources or a master are available on a single machine, database services come to a complete halt if the master node goes down. Such database architectures are discouraged where high availability of data is a priority. NoSQL distributed databases generally prefer multiple master/slave configuration or peer-to-peer architecture to avoid a single point of failure. Cassandra delivers peer-to- peer architecture where each Cassandra node would have an identical configuration. We will discuss this at length in the coming chapters. Figure 1-3a depicts a system single master acting as single point of contact to retrieve data from slave nodes. If the master goes down, it would bring the whole system to a halt until the master node is reinstated. But with multiple master configurations, like the one in Figure 1-3b, a single point of failure does not interrupt service. 4 www.it-ebooks.info Chapter 1 ■ NoSQL: CaSSaNdra BaSiCS Figure 1-3. Centralized vs. distributed architecural setup High Availability High availability clusters suggest the database is available with 24x7 support with minimal (or no) downtime. In such clusters, data is replicated across multiple nodes, in case one node is down still another node is available to serve the read/write requests until that node is up and running. Cassandra’s peer-to-peer architecture ensures high availability of data with co-location. Identifying the Big Data Problem Recently, it has been observed that developers are opting for NoSQL databases as an alternative to RDBMS. However, I recommend that you perform an in-depth analysis before deciding on NoSQL technologies. Traditional RDBMS does offer lots of features which are absent in most of NoSQL databases. A couple of questions that must be analyzed and answered before jumping to a NoSQL based approach include • Is it really a big data problem? • Why/where RDBMS fails to deliver? Identifying a “big data problem” is an interesting errand. Scalability, nature of data (structured, unstructured, or semistructured) and cost of maintaining data volume are a few important factors. In most cases, managing secured and structured data within an RDBMS may still be the preferred approach; however, if the nature of the data is semistructured, less vulnerable, and scalability is preferred over traditional RDBMS features (e.g., joins, materialized view, and so forth), it qualifies as a big data use case. Here data security means the authentication and authorization mechanism. Although Cassandra offers decent support for authentication and authorization but RDBMS fairs well in comparison with most of NoSQL databases. Figure 1-4 shows a scenario in which a cable/satellite operator system is collecting audio/video transmission logs (on daily basis) of around 3 GB/day per connection. A “viewer transmission analytic system” can be developed using a big data tech stack to perform “near real time” and “large data” analytics over the streaming logs. Also the nature of data logs is uncertain and may vary from user to user. Generating monthly/yearly analytic reports would require dealing with petabytes of data, and NoSQL’s scalability is definitely a preference over that of RDBMS. 5 www.it-ebooks.info Chapter 1 ■ NoSQL: CaSSaNdra BaSiCS Figure 1-4. Family watching satellite transmitted programs Consider an example in which a viewer transmission analytic system is capturing random logs for each transmitted program and watched or watching users. The first question we need to ask is, is it really a big data problem? Yes, here we are talking about logs; imagine in a country like India the user base is huge as are the logs captured 24x7! Also, the nature of transmitted logs may be random, meaning the structure is not fixed! It can be semi-structured or totally unstructured. That’s where RDBMS will fail to deliver because of budding schema and scalability problems (see previous section). To summarize, build a NoSQL based solution if: • Data format is semi/unstructured • RDBMS reaches the storage limit and cannot scale further • RDBMS specific features like relations, indexes can be sacrificed against denormalized but distributed data • Data redundancy is not an issue and a read-before-write approach can be applied In the next section, we will discuss how Cassandra can be a best fit to address such technical and functional challenges. Introducing Cassandra Cassandra is an open-source column, family-oriented database. Originally developed at Facebook, it has been an Apache TLP since 2009. Cassandra comes with many important features; some are listed below: • Distributed database\ • Peer to Peer architecture • Configurable consistency • CQL (Cassandra Query Language) 6 www.it-ebooks.info
Description: