Table Of ContentNoSQL Web
Development with
Apache™ Cassandra™
Deepak Vohra
Cengage Learning PTR
NoSQLWebDevelopmentwithApache™ ©2015CengageLearningPTR.
Cassandra™
CENGAGEandCENGAGELEARNINGareregisteredtrademarksofCengage
DeepakVohra Learning,Inc.,withintheUnitedStatesandcertainotherjurisdictions.
PublisherandGeneralManager,Cengage ALLRIGHTSRESERVED.Nopartofthisworkcoveredbythecopyrightherein
LearningPTR:StacyL.Hiquet maybereproduced,transmitted,stored,orusedinanyformorbyanymeans
graphic,electronic,ormechanical,includingbutnotlimitedtophotocopying,
AssociateDirectorofMarketing: recording,scanning,digitizing,taping,Webdistribution,informationnetworks,or
SarahPanella informationstorageandretrievalsystems,exceptaspermittedunderSection107
ManagerofEditorialServices: or108ofthe1976UnitedStatesCopyrightAct,withoutthepriorwritten
HeatherTalbot permissionofthepublisher.
ProductManager:HeatherHurley
Forproductinformationandtechnologyassistance,contactusat
ProjectEditor:KateShoup
CengageLearningCustomer&SalesSupport,1-800-354-9706.
TechnicalReviewer:JohnYeary
Forpermissiontousematerialfromthistextorproduct,submit
CopyEditor:KateShoup allrequestsonlineatcengage.com/permissions.
InteriorLayoutTech:MPSLimited Furtherpermissionsquestionscanbeemailedto
CoverDesigner:MikeTanamachi permissionrequest@cengage.com.
Proofreader:KellyTalbotEditingServices
“Apache”,“ApacheCassandra”,and“Cassandra”aretrademarksoftheApache
SoftwareFoundation.Usedwithpermission.NoendorsementbyTheApache
SoftwareFoundationisimpliedbytheuseofthesemarks.OracleandJavaare
registeredtrademarksofOracleand/oritsaffiliates.Othernamesmaybe
trademarksoftheirrespectiveowners.GoogleandtheGooglelogoare
registeredtrademarksofGoogle,Inc.,usedwithpermission.“Eclipse”isa
trademarkofEclipseFoundation,Inc.“DataStax”isatrademarkofDataStax,
Inc.Node.jsisaregisteredtrademarkofJoyent,Inc.intheUnitedStatesand
othercountries.Thisbookisnotformallyrelatedtoorendorsedbytheofficial
JoyentNode.jsproject.MongoDBandMongoareregisteredtrademarksof
MongoDB,Inc.CouchbaseisatrademarkofCouchbase,Inc.Windowsiseither
registeredtrademarkortrademarkofMicrosoftCorporationintheUnited
Statesand/orothercountries.Allothertrademarksarethepropertyoftheir
respectiveowners.
Allimages©CengageLearningunlessotherwisenoted.
ISBN-13:978-1-305-57676-6
ISBN-10:1-305-57676-4
eISBN-10: 1-305-57677-2
CengageLearningPTR
20ChannelCenterStreet
Boston,MA02210
USA
CengageLearningisaleadingproviderofcustomizedlearningsolutions
withofficelocationsaroundtheglobe,includingSingapore,theUnited
Kingdom,Australia,Mexico,Brazil,andJapan.Locateyourlocalofficeat:
international.cengage.com/region.
CengageLearningproductsarerepresentedinCanadabyNelson
Education,Ltd.
Foryourlifelonglearningsolutions,visitcengageptr.com.
Visitourcorporatewebsiteatcengage.com.
About the Author
Deepak Vohra
is a consultant and a principal member of the NuBean.com software com-
pany. Deepak is a Sun Certified Java Programmer and Web Component Developer, and
he has worked in the fields of XML, Java programming, and Java EE for more than five
years. Deepak is the co-author of Pro XML Development with JavaTechnology and wasthe
technical reviewer for WebLogic: The Definitive Guide. Deepak was also the technical
reviewer for Ruby Programming for the Absolute Beginner. Deepak is the author of JDBC
4.0 and Oracle JDeveloper for J2EE Development, Processing XML Documents with Oracle
JDeveloper 11g, EJB 3.0 Database Persistence with Oracle Fusion Middleware 11g, Java EE
Development with Eclipse, and JavaServer Faces 2.0: Essential Guide for Developers.
iii
Contents
Introduction
PART I JAVA CLIENTS
Chapter 1 Using Cassandra with Hector
CassandraStorageModel
OverviewofHectorJavaClient
SettingtheEnvironment
CreatingaJavaProject
CreatingaCassandraClusterObject
CreatingaSchema
CreatingaKeyspace
CreatingaTemplate
AddingTableData
AddingaSingleColumnofDatainaTable
AddingMultipleColumnsofDatainaTable
RetrievingTableData
QueryingSingleColumn
QueryingMultipleColumns
QueryingwithaSliceQuery
QueryingwiththeMultigetSliceQuery
QueryingwithaRangeSlicesQuery
UpdatingData
iv
Contents v
DeletingTableData
DeletingaSingleColumn
DeletingMultipleColumns
TheHectorClientClass
Summary
Chapter 2 Querying Cassandra with CQL
OverviewofCQL
SettingtheEnvironment
CreatingaJavaProject
CreatingaKeyspace
CreatingaColumnFamily
UsingtheINSERTStatement
UsingtheSELECTStatement
CreatingaSecondaryIndex
UsingtheSELECTStatementwiththeWHEREClause
UsingtheUPDATEStatement
UsingtheBATCHStatement
UsingtheDELETEStatement
UsingtheALTERCOLUMNFAMILYStatement
DroppingtheColumnFamily
DroppingtheKeyspace
TheCQLClientApplication
NewFeaturesinCQL3
CompoundPrimaryKey
ConditionalModifications
Summary
Chapter 3 Using Cassandra with DataStax Java Driver
OverviewofDataStaxJavaDriver
SettingtheEnvironment
CreatingaJavaProject
CreatingaConnection
OverviewoftheSessionClass
CreatingaKeyspace
CreatingaTable
RunningtheINSERTStatement
RunningaSELECTStatement
CreatinganIndex
vi Contents
SelectingwithSELECTandaWHEREFilter
RunninganAsyncQuery
RunningaPreparedStatementQuery
RunningtheUPDATEStatement
RunningtheDELETEStatement
RunningtheBATCHStatement
DroppinganIndex
DroppingaTable
DroppingaKeyspace
TheCQLClientApplication
Summary
PART II SCRIPTING LANGUAGES
Chapter 4 Using Apache Cassandra with PHP
AnOverviewofPhpcassa
SettingtheEnvironment
InstallingPHP
InstallingPhpcassa
CreatingaKeyspace
CreatingaColumnFamilyandConnectionPool
AddingData
AddingDatainaBatch
RetrievingData
GettingSelectedColumns
GettingColumnsfromMultipleRows
GettingColumnSlices
GettingaRangeofRowsandColumns
UpdatingData
DeletingData
DroppingtheKeyspaceandColumnFamily
Summary
Chapter 5 Using a Ruby Client with Cassandra
SettingtheEnvironment
InstallingaRubyClientwithCassandra
CreatingaConnection
CreatingaKeyspace
CreatingaColumnFamily
AddingDatatoaTable
Contents vii
AddingRowsinBatch
RetrievingDatafromaTable
SelectingaSingleRow
SelectingMultipleRows
IteratingoveraResultSet
SelectingaRangeofRows
UsingaRandomPartitioner
UsinganOrder-PreservingPartitioner
GettingaSliceofColumns
UpdatingDatainaTable
DeletingDatainaTable
UpdatingaColumnFamily
DroppingaKeyspace
Summary
Chapter 6 Using Node.js with Cassandra
OverviewofNode.jsDriverforCassandraCQL
TheClientClass
TheConnectionClass
Event-DrivenLogging
MappingDataTypes
SettingtheEnvironment
CreatingaKeyspaceandaColumnFamily
InstallingNode.js
InstallingNode.jsdriverforApacheCassandra
CreatingaConnectionwithCassandra
AddingDatatoaTable
RetrievingDatafromaTable
FilteringtheQuery
QueryingwithaPreparedStatement
StreamingQueryRows
StreamingaField
StreamingtheResult
UpdatingDatainTable
DeletingaColumn
DeletingaRow
Summary
viii Contents
PART III MIGRATION
Chapter 7 Migrating MongoDB to Cassandra
SettingtheEnvironment
CreatingaJavaProject
CreatingaBSONDocumentinMongoDB
MigratingtheMongoDBDocumenttoCassandra
Summary
Chapter 8 Migrating Couchbase to Cassandra
SettingtheEnvironment
CreatingaJavaProject
CreatingaJSONDocumentinCouchbase
MigratingtheCouchbaseDocumenttoCassandra
Summary
PART IV JAVA EE
Chapter 9 Using Cassandra with Kundera
SettingtheEnvironment
CreatingaJPAProjectinEclipse
CreatingaJPAEntityClass
ConfiguringJPAinPersistence.xml
CreatingaJPAClientClass
RunningJPACRUDOperations
CreatingaCatalog
FindingaCatalogEntryUsingtheEntityClass
FindingaCatalogEntryUsingaJPAQuery
UpdatingaCatalogEntry
DeletingaCatalogEntry
Summary
Chapter 10 Using Spring Data with Cassandra
OverviewoftheSpringDataCassandraProject
SettingtheEnvironment
CreatingaMavenProject
ConfiguringtheMavenProject
ConfiguringJavaConfig
CreatingaModel
UsingSpringDatawithCassandrawithTemplate
Contents ix
FindingOutAbouttheCassandraCluster
RunningCassandraCRUDOperations
SaveOperations
FindOperations
ExistsandCountOperations
UpdateOperations
RemoveOperations
Summary
Description:Apache Cassandra is the most commonly used NoSQL database written in Java and is renowned in the industry as the only NoSQL solution that can accommodate the complex requirements of todays modern line-of-business applications. Cassandra is the technology of choice for such data-driven organizations