|
1 | | -# Project Name |
| 1 | +# Azure Cosmos DB Cassandra API - Datastax Spark Connector Sample |
| 2 | +This maven project provides samples and best practices for using the [DataStax Spark Cassandra Connector](https://github.com/datastax/spark-cassandra-connector) against [Azure Cosmos DB's Cassandra API](https://docs.microsoft.com/azure/cosmos-db/cassandra-introduction). |
| 3 | +For the purposes of providing an end-to-end sample, we've made use of an [Azure HDI Spark Cluster](https://docs.microsoft.com/azure/hdinsight/spark/apache-spark-jupyter-spark-sql) to run the spark jobs provided in the example. |
| 4 | +All samples provided are in scala, built with maven. |
2 | 5 |
|
3 | | -(short, 1-3 sentenced, description of the project) |
| 6 | +*Note - this sample is configured against the 2.0.6 version of the spark connector.* |
4 | 7 |
|
5 | | -## Features |
6 | | - |
7 | | -This project framework provides the following features: |
8 | | - |
9 | | -* Feature 1 |
10 | | -* Feature 2 |
11 | | -* ... |
12 | | - |
13 | | -## Getting Started |
| 8 | +## Running this Sample |
14 | 9 |
|
15 | 10 | ### Prerequisites |
16 | | - |
17 | | -(ideally very short, if any) |
18 | | - |
19 | | -- OS |
20 | | -- Library version |
21 | | -- ... |
22 | | - |
23 | | -### Installation |
24 | | - |
25 | | -(ideally very short) |
26 | | - |
27 | | -- npm install [package name] |
28 | | -- mvn install |
29 | | -- ... |
30 | | - |
31 | | -### Quickstart |
32 | | -(Add steps to get up and running quickly) |
33 | | - |
34 | | -1. git clone [repository clone url] |
35 | | -2. cd [respository name] |
36 | | -3. ... |
37 | | - |
38 | | - |
39 | | -## Demo |
40 | | - |
41 | | -A demo app is included to show how to use the project. |
42 | | - |
43 | | -To run the demo, follow these steps: |
44 | | - |
45 | | -(Add steps to start up the demo) |
46 | | - |
47 | | -1. |
48 | | -2. |
49 | | -3. |
| 11 | +- Cosmos DB Account configured with Cassandra API |
| 12 | +- Spark Cluster |
| 13 | + |
| 14 | +# Quick Start |
| 15 | +Information regarding submitting spark jobs is not covered as part of this sample, please refer to Apache Spark's [documentation](https://spark.apache.org/docs/latest/submitting-applications.html). |
| 16 | +In order run this sample, correctly configure the sample to your cluster(as discussed below), build the project, generate the required jar(s), and then submit the job to your spark cluster. |
| 17 | + |
| 18 | +## Cassandra API Connection Parameters |
| 19 | +In order for your spark jobs to connect with Cosmos DB's Cassandra API, you must set the following configurations: |
| 20 | + |
| 21 | +*Note - all these values can be found on the ["Connection String" blade](https://docs.microsoft.com/azure/cosmos-db/manage-account#keys) of your CosmosDB Account* |
| 22 | + |
| 23 | +<table class="table"> |
| 24 | +<tr><th>Property Name</th><th>Value</th></tr> |
| 25 | +<tr> |
| 26 | + <td><code>spark.cassandra.connection.host</code></td> |
| 27 | + <td>Your Cassandra Endpoint: <code>ACOUNT_NAME.cassandra.cosmosdb.azure.com</code></td> |
| 28 | +</tr> |
| 29 | +<tr> |
| 30 | + <td><code>spark.cassandra.connection.port</code></td> |
| 31 | + <td><code>10350</code></td> |
| 32 | +</tr> |
| 33 | +<tr> |
| 34 | + <td><code>spark.cassandra.connection.ssl.enabled</code></td> |
| 35 | + <td><code>true</code></td> |
| 36 | +</tr> |
| 37 | +<tr> |
| 38 | + <td><code>spark.cassandra.auth.username</code></td> |
| 39 | + <td><code>COSMOSDB_ACCOUNTNAME</code></td> |
| 40 | +</tr> |
| 41 | +<tr> |
| 42 | + <td><code>spark.cassandra.auth.password</code></td> |
| 43 | + <td><code>COSMOSDB_KEY</code></td> |
| 44 | +</tr> |
| 45 | +</table> |
| 46 | + |
| 47 | +## Configurations for Throughput optimization |
| 48 | +Because Cosmos DB follows a provisioned throughput model, it is important to tune the relevant configurations of the connector to optimize for this model. |
| 49 | +General information regarding these configurations can be found on the [Configuration Reference](https://github.com/datastax/spark-cassandra-connector/blob/master/doc/reference.md) page of the DataStax Spark Cassandra Connector github repository. |
| 50 | +<table class="table"> |
| 51 | +<tr><th>Property Name</th><th>Description</th></tr> |
| 52 | +<tr> |
| 53 | + <td><code>spark.cassandra.output.batch.size.rows</code></td> |
| 54 | + <td>Leave this to <code>1</code>. This is prefered for Cosmos DB's provisioning model in order to achieve higher throughput for heavy workloads.</td> |
| 55 | +</tr> |
| 56 | +<tr> |
| 57 | + <td><code>spark.cassandra.connection.connections_per_executor_max</code></td> |
| 58 | + <td><code>10*n</code><br/><br/>Which would be equivalent to 10 connections per node in an n-node Cassandra cluster. Hence if you require 5 connections per node per executor for a 5 node Cassandra cluster, then you would need to set this configuration to 25.<br/>(Modify based on the degree of parallelism/number of executors that your spark job are configured for)</td> |
| 59 | +</tr> |
| 60 | +<tr> |
| 61 | + <td><code>spark.cassandra.output.concurrent.writes</code></td> |
| 62 | + <td><code>100</code><br/><br/>Defines the number of parallel writes that can occur per executor. As batch.size.rows is <code>1</code>, make sure to scale up this value accordingly. (Modify this based on the degree of parallelism/throughput that you want to achieve for your workload)</td> |
| 63 | +</tr> |
| 64 | +<tr> |
| 65 | + <td><code>spark.cassandra.concurrent.reads</code></td> |
| 66 | + <td><code>512</code><br /><br />Defines the number of parallel reads that can occur per executor. (Modify this based on the degree of parallelism/throughput that you want to achieve for your workload)</td> |
| 67 | +</tr> |
| 68 | +<tr> |
| 69 | + <td><code>spark.cassandra.output.throughput_mb_per_sec</code></td> |
| 70 | + <td>Defines the total write throughput per executor. This can be used as an upper cap for your spark job throughput, and base it on the provisioned throughput of your Cosmos DB Collection.</td> |
| 71 | +</tr> |
| 72 | +<tr> |
| 73 | + <td><code>spark.cassandra.input.reads_per_sec</code></td> |
| 74 | + <td>Defines the total read throughput per executor. This can be used as an upper cap for your spark job throughput, and base it on the provisioned throughput of your Cosmos DB Collection.</td> |
| 75 | +</tr> |
| 76 | +<tr> |
| 77 | + <td><code>spark.cassandra.output.batch.grouping.buffer.size</code></td> |
| 78 | + <td>1000</td> |
| 79 | +</tr> |
| 80 | +<tr> |
| 81 | + <td><code>spark.cassandra.connection.keep_alive_ms</code></td> |
| 82 | + <td>60000</td> |
| 83 | +</tr> |
| 84 | +</table> |
| 85 | + |
| 86 | +Regarding throughput and degree of parallelism, it is important to tune the relevant parameters based on the amount of load you expect your upstream/downstream flows to be, the executors provisioned for your spark jobs, and the throughput you have provisioned for your Cosmos DB account. |
| 87 | + |
| 88 | +## Connection Factory Configuration and Retry Policy |
| 89 | +As part of this sample, we have provided a connection factory and custom retry policy for Cosmos DB. We need a custom connection factory as that is the only way to configure a retry policy on the connector - [SPARKC-437](https://datastax-oss.atlassian.net/browse/SPARKC-437). |
| 90 | +* <code>CosmosDbConnectionFactory.scala</code> |
| 91 | +* <code>CosmosDbMultipleRetryPolicy.scala</code> |
| 92 | + |
| 93 | +### Retry Policy |
| 94 | +The retry policy for Cosmos DB is configured to handle http status code 429 - Request Rate Large exceptions. The Cosmos Db Cassandra API, translates these exceptions to overloaded errors on the Cassandra native protocol, which we want to retry with back-offs. |
| 95 | +The reason for doing so is because Cosmos DB follows a provisioned throughput model, and having this retry policy would protect your spark jobs against spikes of data ingress/egress that would momentarily exceed the allocated throughput for your collection, resulting in the request rate limiting exceptions. |
| 96 | + |
| 97 | +*Note - that this retry policy is meant to only protect your spark jobs against momentary spikes. If you have not configured enough RUs on your collection for the intended throughput of your workload such that the retries don't catch up, then the retry policy will result in rethrows.* |
| 98 | + |
| 99 | +## Known Issues |
| 100 | + |
| 101 | +### Tokens and Token Range Filters |
| 102 | +We do not currently support methods that make use of Tokens for filtering data. Hence please avoid using any APIs that perform table scans. |
50 | 103 |
|
51 | 104 | ## Resources |
52 | | - |
53 | | -(Any additional resources or related projects) |
54 | | - |
55 | | -- Link to supporting information |
56 | | -- Link to similar sample |
57 | | -- ... |
| 105 | +- [DataStax Spark Cassandra Connector](https://github.com/datastax/spark-cassandra-connector) |
| 106 | +- [CosmosDB Cassandra API](https://docs.microsoft.com/en-us/azure/cosmos-db/cassandra-introduction) |
| 107 | +- [Apache Spark](https://spark.apache.org/docs/latest/index.html) |
| 108 | +- [HDI Spark Cluster](https://docs.microsoft.com/en-us/azure/hdinsight/spark/apache-spark-jupyter-spark-sql) |
0 commit comments