Use Apache Kafka with Apache Spark on hdinsight
This is a basic example of using Apache Spark on HDInsight to stream data from Kafka to Azure Cosmos DB. This example uses Spark Structured Streaming and the Azure Cosmos DB Spark Connector.
This example requires Kafka and Spark on HDInsight 3.6 in the same Azure Virtual Network. It also requires an Azure Cosmos DB SQL API database.
NOTE: Apache Kafka and Spark are available as two different cluster types. HDInsight cluster types are tuned for the performance of a specific technology; in this case, Kafka and Spark. To use both together, you must create an Azure Virtual network and then create both a Kafka and Spark cluster on the virtual network. For an example of how to do this using an Azure Resource Manager template, see https://hditutorialdata.blob.core.windows.net/armtemplates/create-linux-based-kafka-spark-cluster-in-vnet.json. For an example of using the template with this example, see Use Apache Spark with Kafka on HDInsight (preview).
Deploy Azure resources
To create an environment that can be used to run this example, use the Deploy to Azure button to deploy the following Azure resources:
A virtual network
Spark on HDInsight 3.6
Kafka on HDInsight 3.6
Azure Cosmos DB SQL API database
When the template loads, you must provide the following information:
Base cluster name: This must be a unique alphanumeric value, and is used to generate the following resource names:
- Virtual Network: basename-network
- Spark on HDInsight: spark-basename
- Kafka on HDInsight: kafka-basename
- Azure Storage Account: basenamestore
- Cosmos DB: basenamecosmosdb
Cluster login name: This is the name used when logging in to Jupyter notebooks on the Spark cluster.
Cluster login password: The password for the login account.
SSH user name: The SSH user account for the cluster.
SSH password: The SSH user password.
NOTE: SSH isn't used by this example, but you must still provide these values.
Understand this example
This example uses a Scala application in a Jupyter notebook. The code in the notebook relies on the following pieces of data:
Kafka brokers: The broker process runs on each workernode on the Kafka cluster. The list of brokers is required by the producer component, which writes data to Kafka.
Topic name: The name of the topic that data is written to and read from. The default name used in the example is
Cosmos DB endpoint: The HTTPS endpoint used to connect to Cosmos DB.
Master key: The key used to authenticate requests to the endpoint.
Database: The name of the Cosmos DB SQL API database that data is written to.
Collection: The document collection within the database.
PreferredRegions: The preferred Azure regions to use when writing to the database.
NOTE: The notebooks contain information and links on how to obtain this information.
WARNING: HDInsight is charged hourly. To prevent unnecessary costs, delete the cluster once you are finished with this example.
To run this example
To use the example Jupyter notebooks, you must upload them to the Jupyter Notebook server on the Spark cluster. Use the following steps to upload the notebooks:
In your web browser, use the following URL to connect to the Jupyter Notebook server on the Spark cluster. Replace CLUSTERNAME with the name of your Spark cluster.
When prompted, enter the cluster login (admin) and password used when you created the cluster.
From the upper right side of the page, use the Upload button to upload the
Stream-taxi-data-to-Kafka.ipynbfile. Select the file in the file browser dialog and select Open.
Find the Stream-taxi-data-to-Kafka.ipynb entry in the list of notebooks, and select Upload button beside it.
Once the file has uploaded, select the Stream-taxi-data-to-kafka.ipynb entry to open the notebook. To load data into Kafka, follow the instructions in the notebook.
Repeat steps 1-3 to upload the
Stream-data-from-Kafka-to-Cosmos-DB.ipynbdocument to Kafka. Once the file has uploaded, select the entry to open the notebook. Follow the instructions in the notebook to read the data from Kafka and store it into Cosmos DB.