|
1 | | -# DataStax Node.js Driver for Apache Cassandra Quickstart |
| 1 | +# DataStax Desktop - Node.js Netflix example |
2 | 2 |
|
3 | | -A basic demo CRUD application using the DataStax Node.js Driver for Apache Cassandra. |
4 | | -Run the [quickstart-complete.js](quickstart-complete.js) file if you want to skip the exercise and run the application with the complete code. |
| 3 | +An introduction to using the Cassandra database with well-defined steps to optimize your learning. Using a Netflix dataset for sample data, your locally running Cassandra database will contain a minimal set of show data for you to customize and experiment with. |
5 | 4 |
|
6 | | -Contributors: [Rebecca Mills](https://github.com/beccam) |
| 5 | +Contributors: |
| 6 | +* [Jeff Banks](https://github.com/jeffbanks) |
| 7 | +* [Chris Splinter](https://github.com/csplinter) |
| 8 | + |
| 9 | +## Objectives |
| 10 | +* Leverage DataStax driver APIs for interaction with a local running Cassandra database. |
| 11 | +* Set up a Cassandra Query Language (CQL) session and perform operations such as creating, reading, and writing. |
| 12 | +* Use the Netflix show dataset as example information across three differently constructed tables. |
| 13 | +* Observe how the partition key along with clustering keys produce an optimized experience. |
| 14 | +* Have fun! |
7 | 15 |
|
8 | 16 | ## Project Layout |
9 | 17 |
|
10 | | -* [quickstart.js](src/quickstart.js) - main application file with space to fill in CRUD operation code |
11 | | -* [users.cql](users.cql) - Use this file to create the schema |
| 18 | +* [app.js](app.js) - main application file |
| 19 | +* [netflix-shows](netflix-shows.cql) - foundation CQL for reference or use |
12 | 20 |
|
13 | | -## Objectives |
| 21 | +## How this works |
| 22 | +To get started, read through the comments in the `app.js` to familiarize yourself with the steps for interacting with your own Cassandra database. The functions invoked by the `app.js` are created to provide more flexibility for modifications as you learn. |
14 | 23 |
|
15 | | -* To demonstrate how to perform basic CRUD operations with the DataStax Node.js Driver. |
16 | | -* The intent is to help users get up and running quickly with the driver. |
| 24 | +## Setup and running |
17 | 25 |
|
18 | | -## How this Sample Works |
19 | | -This project walks through basic CRUD operations using Cassandra. The demo application will first insert a row of user data, select that same row back out, update the row and finally delete the user. The README includes the code snippets to be filled in to the main application code to complete the functionality. |
| 26 | +### Prerequisites |
| 27 | +If using [DataStax Desktop](https://www.datastax.com/blog/2020/05/learn-cassandra-datastax-desktop), no prerequisites will be required. The Cassandra instance is provided with the DataStax Desktop stack as part of container provisioning. |
20 | 28 |
|
21 | | -## Prerequisites |
22 | | - * A running instance of [Apache Cassandra®](http://cassandra.apache.org/download/) 1.2+ |
| 29 | +If NOT using DataStax Desktop, spin up your own local instance of Cassandra exposing its address and port to align with the settings in the **app.js** file. You will need to install and perform the following steps: |
| 30 | + |
| 31 | + * Install [Apache Cassandra®](http://cassandra.apache.org/download/) 3.x |
23 | 32 | * [Node.js](https://nodejs.org/en/download/) server environment |
24 | 33 | * Use npm to install the driver: `npm install cassandra-driver` |
25 | | - |
26 | | - ## Create the keyspace and table |
27 | | -The `users.cql` file provides the schema used for this project: |
28 | | - |
29 | | -```sql |
30 | | -CREATE KEYSPACE demo |
31 | | - WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '1'}; |
32 | | - |
33 | | -CREATE TABLE demo.users ( |
34 | | - lastname text PRIMARY KEY, |
35 | | - age int, |
36 | | - city text, |
37 | | - email text, |
38 | | - firstname text); |
39 | | -``` |
| 34 | + * Create the keyspace and table. The `netflix-shows.cql` file provides the schema used for this project. |
| 35 | + |
40 | 36 |
|
41 | | -## Connect to your cluster |
| 37 | +All of the connection code is contained in the `app.js` file. The `cassandra.Client()` API is used to connect to your instance of Cassandra. |
42 | 38 |
|
43 | | -All of our code is contained in the `quickstart.js` file. |
44 | | -The `cassandra.Client()` instance connects to our cluster. |
45 | | -You need to provide the address or host name of your node and your local data center name. |
46 | 39 | ```javascipt |
47 | | -// TO DO: Fill in your own host and data center |
48 | | -const client = new cassandra.Client({ |
49 | | - contactPoints: ['127.0.0.1'], |
50 | | - localDataCenter: 'datacenter1', |
51 | | - keyspace: 'demo' |
| 40 | +const client = new cassandra.Client({ |
| 41 | + contactPoints: ['127.0.0.1'], |
| 42 | + localDataCenter: 'dc1', |
| 43 | + keyspace: 'demo' |
52 | 44 | }); |
53 | 45 | ``` |
54 | 46 |
|
55 | | -## CRUD Operations |
56 | | -Fill the code in the functions that will add a user, get a user, update a user and delete a user from the table with the driver. |
| 47 | +## Running |
57 | 48 |
|
58 | | -### INSERT a user |
59 | | -```javascript |
60 | | -function insertUser(lastname, age, city, email, firstname) { |
61 | | - // TO DO: execute a prepared statement that inserts one user into the table |
62 | | - const insert = 'INSERT INTO users (lastname, age, city, email, firstname) VALUES (?,?,?,?,?)'; |
63 | | - const params = [ lastname, age, city, email, firstname ]; |
64 | | - return client.execute(insert, params, { prepare : true }); |
65 | | -} |
| 49 | +Simply kickoff the app from the command line. |
| 50 | + |
| 51 | +> node app.js |
| 52 | +
|
| 53 | +### Console output |
66 | 54 | ``` |
67 | | -### SELECT a user |
68 | | -```javascript |
69 | | -function selectUser(lastname) { |
70 | | - // TO DO: execute a prepared statement that retrieves one user from the table |
71 | | - const select = 'SELECT firstname, age FROM users WHERE lastname = ?'; |
72 | | - const params = [ lastname ] ; |
73 | | - return client.execute(select, params, { prepare : true }); |
| 55 | +Connecting - step 1 |
| 56 | +Creating a keyspace - step 2 |
| 57 | +Creating Keyspace: demo |
| 58 | +Creating tables - step 3 |
| 59 | +Creating Primary Table |
| 60 | +Creating Titles By Date Table |
| 61 | +Creating Titles By Rating Table |
| 62 | +Inserting records - step 4 |
| 63 | +Inserting into Primary Table for title: Life of Jimmy |
| 64 | +Inserting into Primary Table for title: Pulp Fiction |
| 65 | +Inserting into TitlesByDate Table for title: Life of Jimmy |
| 66 | +Inserting into TitlesByDate Table for title: Pulp Fiction |
| 67 | +Inserting into TitlesByRating Table for title: Life of Jimmy |
| 68 | +Inserting into TitlesByRating Table for title: Pulp Fiction |
| 69 | +Reading records - step 5 |
| 70 | +Selecting all from Table: netflix_master |
| 71 | +Rows in result: 2 |
| 72 | +Row { |
| 73 | + title: 'Life of Jimmy', |
| 74 | + show_id: 100000000, |
| 75 | + cast: [ 'Jimmy' ], |
| 76 | + country: [ 'United States' ], |
| 77 | + date_added: LocalDate { |
| 78 | + date: 2020-07-01T00:00:00.000Z, |
| 79 | + _value: null, |
| 80 | + year: 2020, |
| 81 | + month: 7, |
| 82 | + day: 1 |
| 83 | + }, |
| 84 | + description: 'Experiences of a guitar playing DataStax developer', |
| 85 | + director: [ 'Franky J' ], |
| 86 | + duration: '42 min', |
| 87 | + listed_in: [ 'Action' ], |
| 88 | + rating: 'TV-18', |
| 89 | + release_year: 2020, |
| 90 | + type: 'Movie' |
74 | 91 | } |
75 | | -``` |
76 | | - |
77 | | -### UPDATE a user's age |
78 | | -```javascript |
79 | | -function updateUser(age, lastname) { |
80 | | - // TO DO: execute a prepared statement that updates the age of one user |
81 | | - const update = 'UPDATE users SET age = ? WHERE lastname = ?'; |
82 | | - return client.execute(update, [ age, lastname ], { prepare : true } ) |
| 92 | +Row { |
| 93 | + title: 'Pulp Fiction', |
| 94 | + show_id: 100000001, |
| 95 | + cast: [ |
| 96 | + 'John Travolta', |
| 97 | + 'Samuel L. Jackson', |
| 98 | + 'Uma Thurman', |
| 99 | + 'Harvey Keitel', |
| 100 | + 'Tim Roth', |
| 101 | + 'Amanda Plummer', |
| 102 | + 'Maria de Medeiros', |
| 103 | + 'Ving Rhames', |
| 104 | + 'Eric Stoltz', |
| 105 | + 'Rosanna Arquette', |
| 106 | + 'Christopher Walken', |
| 107 | + 'Bruce Willis' |
| 108 | + ], |
| 109 | + country: [ 'United States' ], |
| 110 | + date_added: LocalDate { |
| 111 | + date: 2019-02-19T00:00:00.000Z, |
| 112 | + _value: null, |
| 113 | + year: 2019, |
| 114 | + month: 2, |
| 115 | + day: 19 |
| 116 | + }, |
| 117 | + description: 'This stylized crime caper weaves together stories ...', |
| 118 | + director: [ 'Quentin Tarantino' ], |
| 119 | + duration: '42 min', |
| 120 | + listed_in: [ 'Classic Movies', 'Cult Movies', 'Dramas' ], |
| 121 | + rating: 'R', |
| 122 | + release_year: 1994, |
| 123 | + type: 'Movie' |
| 124 | +} |
| 125 | +Selecting all from Table: netflix_titles_by_rating |
| 126 | +Rows in result: 2 |
| 127 | +Row { rating: 'TV-18', show_id: 100000000, title: 'Life of Jimmy' } |
| 128 | +Row { rating: 'R', show_id: 100000001, title: 'Pulp Fiction' } |
| 129 | +Selecting all from Table: netflix_titles_by_date |
| 130 | +Rows in result: 2 |
| 131 | +Row { |
| 132 | + release_year: 2020, |
| 133 | + date_added: LocalDate { |
| 134 | + date: 2020-07-01T00:00:00.000Z, |
| 135 | + _value: null, |
| 136 | + year: 2020, |
| 137 | + month: 7, |
| 138 | + day: 1 |
| 139 | + }, |
| 140 | + show_id: 100000000, |
| 141 | + title: 'Life of Jimmy', |
| 142 | + type: 'Movie' |
83 | 143 | } |
84 | | -``` |
85 | | - |
86 | | -### DELETE a user |
87 | | -```javascript |
88 | | -function deleteUser(lastname) { |
89 | | - // TO DO: execute a prepared that deletes one user from the table |
90 | | - const remove = 'DELETE FROM users WHERE lastname = ?'; |
91 | | - const params = [ lastname ]; |
92 | | - return client.execute(remove, params, { prepare: true }) |
| 144 | +Row { |
| 145 | + release_year: 1994, |
| 146 | + date_added: LocalDate { |
| 147 | + date: 2019-02-19T00:00:00.000Z, |
| 148 | + _value: null, |
| 149 | + year: 2019, |
| 150 | + month: 2, |
| 151 | + day: 19 |
| 152 | + }, |
| 153 | + show_id: 100000001, |
| 154 | + title: 'Pulp Fiction', |
| 155 | + type: 'Movie' |
93 | 156 | } |
| 157 | +Selecting all by Title: Pulp Fiction from Primary table |
| 158 | +Rows in result: 1 |
| 159 | +Row { rating: 'R', show_id: 100000001, title: 'Pulp Fiction' } |
| 160 | +Selecting all by Title: Life of Jimmy from Primary table |
| 161 | +Rows in result: 1 |
| 162 | +Row { rating: 'TV-18', show_id: 100000000, title: 'Life of Jimmy' } |
| 163 | +Updating record with read - step 6 |
| 164 | +Updating director list by Title: Pulp Fiction and Show ID: 100000001 from Primary table |
| 165 | +Selecting director by Title: Pulp Fiction from Primary table |
| 166 | +Rows in result: 1 |
| 167 | +Row { director: [ 'Quentin Jerome Tarantino' ] } |
| 168 | +Shutting down client - step 7 |
94 | 169 | ``` |
95 | | - ## License |
96 | | -Copyright 2019 Rebecca Mills |
97 | 170 |
|
98 | | -Licensed under the Apache License, Version 2.0 (the "License"); |
99 | | -you may not use this file except in compliance with the License. |
100 | | -You may obtain a copy of the License at |
| 171 | +### Having trouble? |
| 172 | +Are you getting errors reported but can't figure out what to do next? Copy your log output, document any details, and head over to the [DataStax Community](https://community.datastax.com/spaces/131/datastax-desktop.html) to get some assistance. |
101 | 173 |
|
102 | | -http://www.apache.org/licenses/LICENSE-2.0 |
103 | 174 |
|
104 | | -Unless required by applicable law or agreed to in writing, software |
105 | | -distributed under the License is distributed on an "AS IS" BASIS, |
106 | | -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
107 | | -See the License for the specific language governing permissions and |
108 | | -limitations under the License. |
| 175 | +### Questions or comments? |
| 176 | +If you have any questions or want to post a feature request, visit the [Desktop space at DataStax Community](https://community.datastax.com/spaces/131/datastax-desktop.html) |
109 | 177 |
|
0 commit comments