title | description | author | ms.author | ms.service | ms.subservice | ms.devlang | ms.topic | ms.date | ms.custom |
---|---|---|---|---|---|---|---|---|---|
Quickstart: Cassandra API with Node.js - Azure Cosmos DB |
This quickstart shows how to use the Azure Cosmos DB Cassandra API to create a profile application with Node.js |
TheovanKraay |
thvankra |
cosmos-db |
cosmosdb-cassandra |
nodejs |
quickstart |
05/18/2020 |
devx-track-js |
[!div class="op_single_selector"]
In this quickstart, you create an Azure Cosmos DB Cassandra API account, and use a Cassandra Node.js app cloned from GitHub to create a Cassandra database and container. Azure Cosmos DB is a multi-model database service that lets you quickly create and query document, table, key-value, and graph databases with global distribution and horizontal scale capabilities.
[!INCLUDE quickstarts-free-trial-note] Alternatively, you can Try Azure Cosmos DB for free without an Azure subscription, free of charge and commitments.
In addition, you need:
Before you can create a document database, you need to create a Cassandra account with Azure Cosmos DB.
[!INCLUDE cosmos-db-create-dbaccount-cassandra]
Now let's clone a Cassandra API app from GitHub, set the connection string, and run it. You see how easy it is to work with data programmatically.
-
Open a command prompt. Create a new folder named
git-samples
. Then, close the command prompt.md "C:\git-samples"
-
Open a git terminal window, such as git bash. Use the
cd
command to change to the new folder to install the sample app.cd "C:\git-samples"
-
Run the following command to clone the sample repository. This command creates a copy of the sample app on your computer.
git clone https://github.com/Azure-Samples/azure-cosmos-db-cassandra-nodejs-getting-started.git
This step is optional. If you're interested to learn how the code creates the database resources, you can review the following snippets. The snippets are all taken from the uprofile.js
file in the C:\git-samples\azure-cosmos-db-cassandra-nodejs-getting-started
folder. Otherwise, you can skip ahead to Update your connection string.
-
The username and password values were set using the connection string page in the Azure portal. The
path\to\cert
provides a path to an X509 certificate.var ssl_option = { cert : fs.readFileSync("path\to\cert"), rejectUnauthorized : true, secureProtocol: 'TLSv1_2_method' }; const authProviderLocalCassandra = new cassandra.auth.PlainTextAuthProvider(config.username, config.password);
-
The
client
is initialized with contactPoint information. The contactPoint is retrieved from the Azure portal.const client = new cassandra.Client({contactPoints: [config.contactPoint], authProvider: authProviderLocalCassandra, sslOptions:ssl_option});
-
The
client
connects to the Azure Cosmos DB Cassandra API.client.connect(next);
-
A new keyspace is created.
function createKeyspace(next) { var query = "CREATE KEYSPACE IF NOT EXISTS uprofile WITH replication = {\'class\': \'NetworkTopologyStrategy\', \'datacenter1\' : \'1\' }"; client.execute(query, next); console.log("created keyspace"); }
-
A new table is created.
function createTable(next) { var query = "CREATE TABLE IF NOT EXISTS uprofile.user (user_id int PRIMARY KEY, user_name text, user_bcity text)"; client.execute(query, next); console.log("created table"); },
-
Key/value entities are inserted.
function insert(next) { console.log("\insert"); const arr = ['INSERT INTO uprofile.user (user_id, user_name , user_bcity) VALUES (1, \'AdrianaS\', \'Seattle\')', 'INSERT INTO uprofile.user (user_id, user_name , user_bcity) VALUES (2, \'JiriK\', \'Toronto\')', 'INSERT INTO uprofile.user (user_id, user_name , user_bcity) VALUES (3, \'IvanH\', \'Mumbai\')', 'INSERT INTO uprofile.user (user_id, user_name , user_bcity) VALUES (4, \'IvanH\', \'Seattle\')', 'INSERT INTO uprofile.user (user_id, user_name , user_bcity) VALUES (5, \'IvanaV\', \'Belgaum\')', 'INSERT INTO uprofile.user (user_id, user_name , user_bcity) VALUES (6, \'LiliyaB\', \'Seattle\')', 'INSERT INTO uprofile.user (user_id, user_name , user_bcity) VALUES (7, \'JindrichH\', \'Buenos Aires\')', 'INSERT INTO uprofile.user (user_id, user_name , user_bcity) VALUES (8, \'AdrianaS\', \'Seattle\')', 'INSERT INTO uprofile.user (user_id, user_name , user_bcity) VALUES (9, \'JozefM\', \'Seattle\')']; arr.forEach(element => { client.execute(element); }); next(); },
-
Query to get all key values.
function selectAll(next) { console.log("\Select ALL"); var query = 'SELECT * FROM uprofile.user'; client.execute(query, function (err, result) { if (err) return next(err); result.rows.forEach(function(row) { console.log('Obtained row: %d | %s | %s ',row.user_id, row.user_name, row.user_bcity); }, this); next(); }); },
-
Query to get a key-value.
function selectById(next) { console.log("\Getting by id"); var query = 'SELECT * FROM uprofile.user where user_id=1'; client.execute(query, function (err, result) { if (err) return next(err); result.rows.forEach(function(row) { console.log('Obtained row: %d | %s | %s ',row.user_id, row.user_name, row.user_bcity); }, this); next(); }); }
Now go back to the Azure portal to get your connection string information and copy it into the app. The connection string enables your app to communicate with your hosted database.
-
In your Azure Cosmos DB account in the Azure portal, select Connection String.
-
Use the :::image type="icon" source="./media/create-cassandra-nodejs/copy.png"::: button on the right side of the screen to copy the top value, the CONTACT POINT.
:::image type="content" source="./media/create-cassandra-nodejs/keys.png" alt-text="View and copy the CONTACT POINT, USERNAME,and PASSWORD from the Azure portal, connection string page":::
-
Open the
config.js
file. -
Paste the CONTACT POINT value from the portal over
<FillMEIN>
on line 4.Line 4 should now look similar to
config.contactPoint = "cosmos-db-quickstarts.cassandra.cosmosdb.azure.com:10350"
-
Copy the USERNAME value from the portal and paste it over
<FillMEIN>
on line 2.Line 2 should now look similar to
config.username = 'cosmos-db-quickstart';
-
Copy the PASSWORD value from the portal and paste it over
<FillMEIN>
on line 3.Line 3 should now look similar to
config.password = '2Ggkr662ifxz2Mg==';
-
Save the
config.js
file.
-
Download the Baltimore CyberTrust Root certificate locally from https://cacert.omniroot.com/bc2025.crt. Rename the file using the file extension
.cer
.The certificate has serial number
02:00:00:b9
and SHA1 fingerprintd4🇩🇪20:d0:5e:66:fc:53:fe:1a:50:88:2c:78:db:28:52:ca:e4:74
. -
Open
uprofile.js
and change thepath\to\cert
to point to your new certificate. -
Save
uprofile.js
.
Note
If you experience a certificate related error in the later steps and are running on a Windows machine, ensure that you have followed the process for properly converting a .crt file into the Microsoft .cer format below.
Double-click on the .crt file to open it into the certificate display.
:::image type="content" source="./media/create-cassandra-nodejs/crtcer1.gif" alt-text="View and verify the output":::
Press Next on the Certificate Wizard. Select Base-64 encoded X.509 (.CER), then Next.
:::image type="content" source="./media/create-cassandra-nodejs/crtcer2.gif" alt-text="View and verify the output":::
Select Browse (to locate a destination) and type in a filename. Select Next then Finished.
You should now have a properly formatted .cer file. Ensure that the path in uprofile.js
points to this file.
-
In the git terminal window, ensure you are in the sample directory you cloned earlier:
cd azure-cosmos-db-cassandra-nodejs-getting-started
-
Run
npm install
to install the required npm modules. -
Run
node uprofile.js
to start your node application. -
Verify the results as expected from the command line.
:::image type="content" source="./media/create-cassandra-nodejs/output.png" alt-text="View and verify the output":::
Press CTRL+C to stop execution of the program and close the console window.
-
In the Azure portal, open Data Explorer to query, modify, and work with this new data.
:::image type="content" source="./media/create-cassandra-nodejs/data-explorer.png" alt-text="View the data in Data Explorer":::
[!INCLUDE cosmosdb-tutorial-review-slas]
[!INCLUDE cosmosdb-delete-resource-group]
In this quickstart, you learned how to create an Azure Cosmos DB account with Cassandra API, and run a Cassandra Node.js app that creates a Cassandra database and container. You can now import additional data into your Azure Cosmos DB account.
[!div class="nextstepaction"] Import Cassandra data into Azure Cosmos DB