• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Ruth Stryker "CQL and the DataStax Java Driver". Выступление на Cassandra Conf 2013
 

Ruth Stryker "CQL and the DataStax Java Driver". Выступление на Cassandra Conf 2013

on

  • 1,330 views

 

Statistics

Views

Total Views
1,330
Views on SlideShare
1,041
Embed Views
289

Actions

Likes
0
Downloads
10
Comments
0

1 Embed 289

http://it-eburg.com 289

Accessibility

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Ruth Stryker "CQL and the DataStax Java Driver". Выступление на Cassandra Conf 2013 Ruth Stryker "CQL and the DataStax Java Driver". Выступление на Cassandra Conf 2013 Presentation Transcript

    • CQL and the DataStax Java Driver RUTH STRYKER, TECHNICAL TRAINER, DATASTAX rstryker@datastax.com December 9, 2013
    • Session agenda •  Understanding CQL •  Using CQL and cqlsh •  Understanding Cassandra drivers •  Using the DataStax Java driver ©2013 DataStax Confidential. Do not distribute without consent. 2
    • Cassandra Query Language ©2013 DataStax Confidential. Do not distribute without consent. 3
    • CQL agenda •  What CQL is •  Why CQL was created •  What CQL looks like •  How CQL works •  Using CQL •  Comparing CQL rows to internal rows •  CQL and cqlsh commands reference •  CQL versions ©2013 DataStax Confidential. Do not distribute without consent. 4
    • What CQL is •  A SQL-like query language for communicating with Cassandra ©2013 DataStax Confidential. Do not distribute without consent. 5
    • Why CQL was created •  To make it easier for people to work with Cassandra ©2013 DataStax Confidential. Do not distribute without consent. 6
    • What CQL looks like •  It looks similar to SQL (but does not have all of the same options as SQL, given that Cassandra’s architecture is that of a distributed database rather than a relational one) For example, to retrieve all of the rows in a table named temperatures_table: SELECT * FROM temperatures_table ©2013 DataStax Confidential. Do not distribute without consent. 7
    • How CQL works •  Through cqlsh or a through a driver •  We will first use CQL in cqlsh •  Then we will use CQL through a driver ©2013 DataStax Confidential. Do not distribute without consent. 8
    • Using CQL through cqlsh 1.  On a machine that has Cassandra installed, in a terminal window, enter cqlsh to get into cqlsh 2.  Enter CQL expressions to communicate with Cassandra For example… ©2013 DataStax Confidential. Do not distribute without consent. 9
    • CQL to create a database CREATE KEYSPACE weatherdata WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 1}; USE weatherdata; ©2013 DataStax Confidential. Do not distribute without consent. 10
    • CQL to create a table CREATE TABLE temperatures ( weatherstation_id text, event_date text, event_time timestamp, temp_in_celsius float, PRIMARY KEY ((weatherstation_id, event_date), event_time) ); ©2013 DataStax Confidential. Do not distribute without consent. 11
    • DESCRIBE to view a table definition DESCRIBE TABLE temperatures; ©2013 DataStax Confidential. Do not distribute without consent. 12
    • CQL to insert a row in a table INSERT INTO temperatures (weatherstation_id, event_date, event_time, temp_in_celsius) VALUES ('1234ABCD', '2013-04-15', '2013-04-15 07:01:00', 17.3); ©2013 DataStax Confidential. Do not distribute without consent. 13
    • CQL for viewing the contents of a table SELECT * FROM temperatures; ©2013 DataStax Confidential. Do not distribute without consent. 14
    • Inserting multiple rows into a table COPY temperatures (weatherstation_id, event_date, event_time, temp_in_celsius) FROM '/home/ubuntu/Desktop/data/ temperatures.csv' WITH header = true AND delimiter = '|'; ©2013 DataStax Confidential. Do not distribute without consent. 15
    • Comparing CQL rows to internal rows •  CQL rows are defined by a primary key PRIMARY KEY((weatherstation_id, event_date), event_time) ) •  Internally, rows are defined by a partition key (weatherstation_id, event_date) ©2013 DataStax Confidential. Do not distribute without consent. 16
    • Partition key versus primary key CREATE TABLE temperatures ( weatherstation_id text, event_date text, event_time timestamp, temp_in_celsius float, PRIMARY KEY ((weatherstation_id, event_date), event_time) ); partition key primary key 1234ABCD 2013-04-15 2013-04-15 07:01:00 17.3 1234ABCD 2013-04-15 2013-04-15 07:02:00 17.3 1234ABCD 2013-04-15 2013-04-15 07:03:00 17.4 ©2013 DataStax Confidential. Do not distribute without consent. 17
    • WHERE clause •  WHERE clause can be used on columns that are in the primary key SELECT * FROM temperatures WHERE weatherstation_id = '1234ABCD' AND event_date = '2013-04-15' AND event_time = '2013-04-15 07:03:00'; •  WHERE clause can also be used on columns with a secondary index ©2013 DataStax Confidential. Do not distribute without consent. 18
    • ORDER BY clause •  ORDER BY clause can be used to order query results SELECT * FROM temperatures WHERE weatherstation_id = '1234ABCD' AND event_date = '2013-04-15' ORDER BY event_time DESC LIMIT 1; e.g. to view the latest temperature for a given weather station for a given day ©2013 DataStax Confidential. Do not distribute without consent. 19
    • Documentation for more CQL commands http://www.datastax.com/docs ©2013 DataStax Confidential. Do not distribute without consent. 20
    • Using the CQL documentation •  Scroll in table of contents to see list of CQL and cqlsh commands •  Click on a command (e.g. CREATE TABLE) to read about it ©2013 DataStax Confidential. Do not distribute without consent. 21
    • CQL current version •  CQL 3.1 for Cassandra 2.0 •  CQL 3.0 for Cassandra 1.2 ©2013 DataStax Confidential. Do not distribute without consent. 22
    • DataStax Java driver ©2013 DataStax Confidential. Do not distribute without consent. 23
    • DataStax Java driver agenda •  Drivers for Cassandra •  Understanding the DataStax Java driver •  The DataStax Java driver documentation •  Enabling the DataStax Java driver •  Using the Cluster, Session, and ResultSet classes •  Using query builder •  Creating a prepared statement ©2013 DataStax Confidential. Do not distribute without consent. 24
    • Drivers for Cassandra •  Allow for communicating with Cassandra via various programming languages •  More than 35 Cassandra drivers available at http://www.datastax.com/download/clientdrivers ©2013 DataStax Confidential. Do not distribute without consent. 25
    • Understanding the DataStax Java driver •  Written for CQL 3.0 •  Uses the binary protocol introduced in Cassandra 1.2 •  Uses Netty to provide an asynchronous architecture •  Can do asynchronous or synchronous queries •  Has connection pooling •  Has node discovery and load balancing ©2013 DataStax Confidential. Do not distribute without consent. 26
    • DataStax Java driver documentation ©2013 DataStax Confidential. Do not distribute without consent. 27
    • Enabling the binary protocol •  In the cassandra.yaml file, the native transport property needs to be set to true (if not already) 1.  cd /etc/dse/cassandra 2.  vim cassandra.yaml 3.  Set start_native_transport: true (line 372) •  Enabled by default in Cassandra 1.2.5 and onwards ©2013 DataStax Confidential. Do not distribute without consent. 28
    • Provide the .jar files to our Java application •  Easiest way is to do this with Maven, which is a software project management tool ©2013 DataStax Confidential. Do not distribute without consent. 29
    • Could use Maven in Eclipse •  For example, we have a dynamic web project in Eclipse ©2013 DataStax Confidential. Do not distribute without consent. 30
    • Configure the project to be a Maven project ©2013 DataStax Confidential. Do not distribute without consent. 31
    • Adding the Cassandra .jar files to the project •  In the pom.xml file, select the Dependencies tab •  Click the Add… button in the left column •  Enter the DataStax Java driver info •  Save the file ©2013 DataStax Confidential. Do not distribute without consent. 32
    • Using the Cluster class •  Connects to a Cassandra cluster, using the first contact point available Cluster cluster = Cluster.builder().addContactPoint("192. 168.0.2", "192.168.0.3").build(); •  Retrieves metadata from the cluster, including: •  Name of the cluster •  IP address, rack, and data center for each node ©2013 DataStax Confidential. Do not distribute without consent. 33
    • Using the Session class •  With a session object, CQL statements can be executed, such as to read and write data •  Session object is instantiated with a connection to a cluster Session session = cluster.connect(); •  Session instances are thread-safe and normally a single session instance is all you need per application per keyspace ©2013 DataStax Confidential. Do not distribute without consent. 34
    • Executing a CQL expression •  Use the execute() method of the Session class, passing in a CQL expression as the argument session.execute("SELECT * FROM weatherdata.temperatures"); ©2013 DataStax Confidential. Do not distribute without consent. 35
    • Using the ResultSet class •  A ResultSet object is what is returned from executing a session object’s CQL expression ResultSet results = session.execute("SELECT * FROM weatherdata.temperatures"); ©2013 DataStax Confidential. Do not distribute without consent. 36
    • Using the Row class •  A row object, combined with a for loop, can be used to iterate through a result set for (Row row : results) { out.println("<tr>"); out.println("<td>" + row.getString("weatherstation_id") + "</td>"); out.println("<td>" + row.getDate("event_time") + "</td>"); out.println("<td>" + row.getFloat("temp_in_celsius") + "</td>"); out.println("</tr>"); } ©2013 DataStax Confidential. Do not distribute without consent. 37
    • Using the QueryBuilder class •  Offers a fluent interface for constructing CQL queries Query query = QueryBuilder .select() .all() .from("weatherdata", "temperatures"); ResultSet results = session.execute(query); ©2013 DataStax Confidential. Do not distribute without consent. 38
    • More with QueryBuilder •  To return the latest temperature for a given weather station on a given day Query query = QueryBuilder .select() .all() .from("weatherdata", "temperatures") .where(QueryBuilder.eq("weatherstation_id", "1234ABCD")) .and(QueryBuilder.eq("event_date", "2013-04-15")) .orderBy(QueryBuilder.desc("event_time")) .limit(1); ©2013 DataStax Confidential. Do not distribute without consent. 39
    • Prepared statements •  For CQL statements that will be executed multiple times •  For performance •  Prepared statements are precompiled and cached on server •  The ? character is used as a placeholder for literal values in the CQL expression ©2013 DataStax Confidential. Do not distribute without consent. 40
    • How prepared statements work 1.  PreparedStatement object gets created, containing a CQL expression with ? characters used as a placeholder for literal values 2.  PreparedStatement object gets passed into a BoundStatement object 3.  Literal values get bound to the BoundStatement object 4.  BoundStatement object gets executed by the Session object ©2013 DataStax Confidential. Do not distribute without consent. 41
    • PreparedStatement code PreparedStatement statement = session.prepare("SELECT * FROM weatherdata.temperatures WHERE weatherstation_id = ? AND event_date = ?"); ©2013 DataStax Confidential. Do not distribute without consent. 42
    • BoundStatement code BoundStatement boundStatement = new BoundStatement(statement); ©2013 DataStax Confidential. Do not distribute without consent. 43
    • Bind and execute code ResultSet results = session.execute(boundStatement.bind("123 4ABCD", "2013-04-15")); ©2013 DataStax Confidential. Do not distribute without consent. 44
    • Summary •  Cassandra Query Language (CQL) is a SQL-like query language for communicating with Cassandra •  CQL expressions can be executed through cqlsh or through a driver that supports CQL •  The DataStax Java driver supports CQL through written queries, QueryBuilder, and prepared statements •  There are even more drivers that support CQL, with many more on their way ©2013 DataStax Confidential. Do not distribute without consent. 45
    • For more information and support •  Planet Cassandra (http://planetcassandra.org) •  CQL and Cassandra documentation (http://www.datastax.com/docs) •  Cassandra mailing list (http://cassandra.apache.org) •  Support forums (http://stackoverflow.com/questions/tagged/cas sandra) •  DataStax support http://www.datastax.com/what-we-offer/productsservices/support) ©2013 DataStax Confidential. Do not distribute without consent. 46
    • ©2013 DataStax Confidential. Do not distribute without consent. 47