Skip to content

Commit e3f99ec

Browse files
authored
Merge pull request #103742 from TheovanKraay/fix-nodejs-quickstart
Fix nodejs quickstart
2 parents 56cbe38 + 9a96737 commit e3f99ec

File tree

5 files changed

+134
-36
lines changed

5 files changed

+134
-36
lines changed

articles/cosmos-db/TOC.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,8 @@
554554
href: cassandra-introduction.md
555555
- name: Wire protocol support
556556
href: cassandra-support.md
557+
- name: Elastic scale
558+
href: manage-scale-cassandra.md
557559
- name: Quickstarts
558560
items:
559561
- name: .NET

articles/cosmos-db/create-cassandra-nodejs.md

Lines changed: 70 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ In this quickstart, you create an Azure Cosmos DB Cassandra API account, and use
2222

2323
## Prerequisites
2424

25-
- An Azure account with an active subscription. [Create one for free](https://azure.microsoft.com/free/?ref=microsoft.com&utm_source=microsoft.com&utm_medium=docs&utm_campaign=visualstudio). Or [try Azure Cosmos DB for free](https://azure.microsoft.com/try/cosmosdb/) without an Azure subscription.
26-
- [Node.js 0.10.29+](https://nodejs.org/).
27-
- [Git](https://www.git-scm.com/downloads).
25+
[!INCLUDE [quickstarts-free-trial-note](../../includes/quickstarts-free-trial-note.md)] Alternatively, you can [Try Azure Cosmos DB for free](https://azure.microsoft.com/try/cosmosdb/) without an Azure subscription, free of charge and commitments.
26+
27+
In addition, you need:
28+
* [Node.js](https://nodejs.org/dist/v0.10.29/x64/node-v0.10.29-x64.msi) version v0.10.29 or higher
29+
* [Git](https://git-scm.com/)
2830

2931
## Create a database account
3032

@@ -104,42 +106,54 @@ This step is optional. If you're interested to learn how the code creates the da
104106
* Key/value entities are inserted.
105107
106108
```javascript
107-
...
108-
{
109-
query: 'INSERT INTO uprofile.user (user_id, user_name , user_bcity) VALUES (?,?,?)',
110-
params: [5, 'IvanaV', 'Belgaum']
111-
}
112-
];
113-
client.batch(queries, { prepare: true}, next);
109+
function insert(next) {
110+
console.log("\insert");
111+
const arr = ['INSERT INTO uprofile.user (user_id, user_name , user_bcity) VALUES (1, \'AdrianaS\', \'Seattle\')',
112+
'INSERT INTO uprofile.user (user_id, user_name , user_bcity) VALUES (2, \'JiriK\', \'Toronto\')',
113+
'INSERT INTO uprofile.user (user_id, user_name , user_bcity) VALUES (3, \'IvanH\', \'Mumbai\')',
114+
'INSERT INTO uprofile.user (user_id, user_name , user_bcity) VALUES (4, \'IvanH\', \'Seattle\')',
115+
'INSERT INTO uprofile.user (user_id, user_name , user_bcity) VALUES (5, \'IvanaV\', \'Belgaum\')',
116+
'INSERT INTO uprofile.user (user_id, user_name , user_bcity) VALUES (6, \'LiliyaB\', \'Seattle\')',
117+
'INSERT INTO uprofile.user (user_id, user_name , user_bcity) VALUES (7, \'JindrichH\', \'Buenos Aires\')',
118+
'INSERT INTO uprofile.user (user_id, user_name , user_bcity) VALUES (8, \'AdrianaS\', \'Seattle\')',
119+
'INSERT INTO uprofile.user (user_id, user_name , user_bcity) VALUES (9, \'JozefM\', \'Seattle\')'];
120+
arr.forEach(element => {
121+
client.execute(element);
122+
});
123+
next();
124+
},
114125
```
115126
116127
* Query to get all key values.
117128
118129
```javascript
119-
var query = 'SELECT * FROM uprofile.user';
120-
client.execute(query, { prepare: true}, function (err, result) {
121-
if (err) return next(err);
122-
result.rows.forEach(function(row) {
123-
console.log('Obtained row: %d | %s | %s ',row.user_id, row.user_name, row.user_bcity);
124-
}, this);
125-
next();
126-
});
130+
function selectAll(next) {
131+
console.log("\Select ALL");
132+
var query = 'SELECT * FROM uprofile.user';
133+
client.execute(query, function (err, result) {
134+
if (err) return next(err);
135+
result.rows.forEach(function(row) {
136+
console.log('Obtained row: %d | %s | %s ',row.user_id, row.user_name, row.user_bcity);
137+
}, this);
138+
next();
139+
});
140+
},
127141
```
128142
129143
* Query to get a key-value.
130144
131145
```javascript
132-
function selectById(next) {
133-
console.log("\Getting by id");
134-
var query = 'SELECT * FROM uprofile.user where user_id=1';
135-
client.execute(query, { prepare: true}, function (err, result) {
136-
if (err) return next(err);
137-
result.rows.forEach(function(row) {
138-
console.log('Obtained row: %d | %s | %s ',row.user_id, row.user_name, row.user_bcity);
139-
}, this);
140-
next();
141-
});
142-
}
146+
function selectById(next) {
147+
console.log("\Getting by id");
148+
var query = 'SELECT * FROM uprofile.user where user_id=1';
149+
client.execute(query, function (err, result) {
150+
if (err) return next(err);
151+
result.rows.forEach(function(row) {
152+
console.log('Obtained row: %d | %s | %s ',row.user_id, row.user_name, row.user_bcity);
153+
}, this);
154+
next();
155+
});
156+
}
143157
```
144158
145159
## Update your connection string
@@ -184,19 +198,41 @@ Now go back to the Azure portal to get your connection string information and co
184198
185199
3. Save `uprofile.js`.
186200
201+
> [!NOTE]
202+
> 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.
203+
>
204+
> Double-click on the .crt file to open it into the certificate display.
205+
>
206+
> ![View and verify the output](./media/create-cassandra-nodejs/crtcer1.gif)
207+
>
208+
> Press Next on the Certificate Wizard. Select Base-64 encoded X.509 (.CER), then Next.
209+
>
210+
> ![View and verify the output](./media/create-cassandra-nodejs/crtcer2.gif)
211+
>
212+
> Select Browse (to locate a destination) and type in a filename.
213+
> Select Next then Finished.
214+
>
215+
> You should now have a properly formatted .cer file. Ensure that the path in `uprofile.js` points to this file.
216+
187217
## Run the Node.js app
188218
189-
1. In the git terminal window, run `npm install` to install the required npm modules.
219+
1. In the git terminal window, ensure you are in the sample directory you cloned earlier:
220+
221+
```bash
222+
cd azure-cosmos-db-cassandra-nodejs-getting-started
223+
```
224+
225+
2. Run `npm install` to install the required npm modules.
190226
191-
2. Run `node uprofile.js` to start your node application.
227+
3. Run `node uprofile.js` to start your node application.
192228
193-
3. Verify the results as expected from the command line.
229+
4. Verify the results as expected from the command line.
194230
195231
![View and verify the output](./media/create-cassandra-nodejs/output.png)
196232
197233
Press CTRL+C to stop execution of the program and close the console window.
198234
199-
4. In the Azure portal, open **Data Explorer** to query, modify, and work with this new data.
235+
5. In the Azure portal, open **Data Explorer** to query, modify, and work with this new data.
200236
201237
![View the data in Data Explorer](./media/create-cassandra-nodejs/data-explorer.png)
202238
@@ -213,6 +249,4 @@ Now go back to the Azure portal to get your connection string information and co
213249
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.
214250
215251
> [!div class="nextstepaction"]
216-
> [Import Cassandra data into Azure Cosmos DB](cassandra-import-data.md)
217-
218-
252+
> [Import Cassandra data into Azure Cosmos DB](cassandra-import-data.md)
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
---
2+
title: Elastically scale with Cassandra API in Azure Cosmos DB
3+
description: Learn about the options available to scale an Azure Cosmos DB Cassandra API account and their advantages/disadvantages
4+
author: TheovanKraay
5+
ms.service: cosmos-db
6+
ms.topic: conceptual
7+
ms.date: 01/13/2020
8+
ms.author: thvankra
9+
---
10+
11+
# Elastically scale an Azure Cosmos DB Cassandra API account
12+
13+
There are a variety of options to explore the elastic nature of the Azure Cosmos DB's API for Cassandra. To understand how to scale effectively in Azure Cosmos DB, it is important to understand how to provision the right amount of request units (RU/s) to account for the performance demands in your system. To learn more about request units, see the [request units](request-units.md) article.
14+
15+
![Database operations consume Request Units](./media/request-units/request-units.png)
16+
17+
## Handling rate limiting (429 errors)
18+
19+
Azure Cosmos DB will return rate-limited (429) errors if clients consume more resources (RU/s) than the amount that you have provisioned. The Cassandra API in Azure Cosmos DB translates these exceptions to overloaded errors on the Cassandra native protocol.
20+
21+
If your system is not sensitive to latency, it may be sufficient to handle the throughput rate-limiting by using retries. See the [Java code sample](https://github.com/Azure-Samples/azure-cosmos-cassandra-java-retry-sample) for how to handle rate limiting transparently by using the [Azure Cosmos DB extension](https://github.com/Azure/azure-cosmos-cassandra-extensions) for [Cassandra retry policy](https://docs.datastax.com/drivers/java/2.0/com/datastax/driver/core/policies/RetryPolicy.html) in Java. You can also use the [Spark extension](https://mvnrepository.com/artifact/com.microsoft.azure.cosmosdb/azure-cosmos-cassandra-spark-helper) to handle rate-limiting.
22+
23+
## Manage scaling
24+
25+
If you need to minimize latency, there is a spectrum of options for managing scale and provisioning throughput (RUs) in the Cassandra API:
26+
27+
* [Manually by using the Azure portal](#use-azure-portal)
28+
* [Programmatically by using the control plane features](#use-control-plane)
29+
* [Programmatically by using CQL commands with a specific SDK](#use-cql-queries)
30+
* [Dynamically by using Autopilot](#use-autopilot)
31+
32+
The following sections explain the advantages and disadvantages of each approach. You can then decide on the best strategy to balance the scaling needs of your system, the overall cost, and efficiency needs for your solution.
33+
34+
## <a id="use-azure-portal"></a>Use the Azure portal
35+
36+
You can scale the resources in Azure Cosmos DB Cassandra API account by using Azure portal. To learn more, see the article on [Provision throughput on containers and databases](set-throughput.md). This article explains the relative benefits of setting throughput at either [database](set-throughput.md#set-throughput-on-a-database) or [container](set-throughput.md#set-throughput-on-a-container) level in the Azure portal. The terms "database" and "container" mentioned in these articles map to "keyspace" and "table" respectively for the Cassandra API.
37+
38+
The advantage of this method is that it is a straightforward turnkey way to manage throughput capacity on the database. However, the disadvantage is that in many cases, your approach to scaling may require certain levels of automation to be both cost-effective and high performing. The next sections explain the relevant scenarios and methods.
39+
40+
## <a id="use-control-plane"></a>Use the control plane
41+
42+
The Azure Cosmos DB's API for Cassandra provides the capability to adjust throughput programmatically by using our various control-plane features. See the [Azure Resource Manager](manage-cassandra-with-resource-manager.md), [Powershell](powershell-samples-cassandra.md), and [Azure CLI](cli-samples-cassandra.md) articles for guidance and samples.
43+
44+
The advantage of this method is that you can automate the scaling up or down of resources based on a timer to account for peak activity, or periods of low activity. Take a look at our sample [here](https://github.com/Azure-Samples/azure-cosmos-throughput-scheduler) for how to accomplish this using Azure Functions and Powershell.
45+
46+
A disadvantage with this approach may be that you cannot respond to unpredictable changing scale needs in real-time. Instead, you may need to leverage the application context in your system, at the client/SDK level, or using [Autopilot](provision-throughput-autopilot.md).
47+
48+
## <a id="use-cql-queries"></a>Use CQL queries with a specific SDK
49+
50+
You can scale the system dynamically with code by executing the [CQL ALTER commands](cassandra-support.md#keyspace-and-table-options) for the given database or container.
51+
52+
The advantage of this approach is that it allows you to respond to scale needs dynamically and in a custom way that suits your application. With this approach, you can still leverage the standard RU/s charges and rates. If your system's scale needs are mostly predictable (around 70% or more), using SDK with CQL may be a more cost-effective method of auto-scaling than using Autopilot. The disadvantage of this approach is that it can be quite complex to implement retries while rate limiting may increase latency.
53+
54+
## <a id="use-autopilot"></a>Use Autopilot
55+
56+
In addition to manual or programmatic way of provisioning throughput, you can also configure Azure cosmos containers in Autopilot mode. Autopilot mode will automatically and instantly scale to your consumption needs within specified RU ranges without compromising SLAs. To learn more, see the [Create Azure Cosmos containers and databases in autopilot mode](provision-throughput-autopilot.md) article.
57+
58+
The advantage of this approach is that it is the easiest way to manage the scaling needs in your system. It guarantees not to apply rate-limiting **within the configured RU ranges**. The disadvantage is that, if the scaling needs in your system are predictable, Autopilot may be a less cost-effective way of handling your scaling needs than using the bespoke control plane or SDK level approaches mentioned above.
59+
60+
## Next steps
61+
62+
- Get started with [creating a Cassandra API account, database, and a table](create-cassandra-api-account-java.md) by using a Java application
21.4 KB
Loading
12 KB
Loading

0 commit comments

Comments
 (0)