You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Use Apache Spark REST API to submit remote jobs to an HDInsight Spark cluster
13
14
14
15
Learn how to use [Apache Livy](https://livy.incubator.apache.org/), the [Apache Spark](https://spark.apache.org/) REST API, which is used to submit remote jobs to an Azure HDInsight Spark cluster. For detailed documentation, see [https://livy.incubator.apache.org/](https://livy.incubator.apache.org/).
15
16
16
17
You can use Livy to run interactive Spark shells or submit batch jobs to be run on Spark. This article talks about using Livy to submit batch jobs. The snippets in this article use cURL to make REST API calls to the Livy Spark endpoint.
17
18
18
-
**Prerequisites:**
19
+
## Prerequisites
19
20
20
21
* An Apache Spark cluster on HDInsight. For instructions, see [Create Apache Spark clusters in Azure HDInsight](apache-spark-jupyter-spark-sql.md).
21
22
22
23
*[cURL](https://curl.haxx.se/). This article uses cURL to demonstrate how to make REST API calls against an HDInsight Spark cluster.
23
24
24
25
## Submit an Apache Livy Spark batch job
25
-
Before you submit a batch job, you must upload the application jar on the cluster storage associated with the cluster. You can use [**AzCopy**](../../storage/common/storage-use-azcopy.md), a command-line utility, to do so. There are various other clients you can use to upload data. You can find more about them at [Upload data for Apache Hadoop jobs in HDInsight](../hdinsight-upload-data.md).
26
26
27
-
curl -k --user "<hdinsight user>:<user password>" -v -H "Content-Type: application/json" -X POST -d '{ "file":"<path to application jar>", "className":"<classname in jar>" }' 'https://<spark_cluster_name>.azurehdinsight.net/livy/batches' -H "X-Requested-By: admin"
27
+
Before you submit a batch job, you must upload the application jar on the cluster storage associated with the cluster. You can use [AzCopy](../../storage/common/storage-use-azcopy.md), a command-line utility, to do so. There are various other clients you can use to upload data. You can find more about them at [Upload data for Apache Hadoop jobs in HDInsight](../hdinsight-upload-data.md).
28
+
29
+
```cmd
30
+
curl -k --user "<hdinsight user>:<user password>" -v -H "Content-Type: application/json" -X POST -d '{ "file":"<path to application jar>", "className":"<classname in jar>" }' 'https://<spark_cluster_name>.azurehdinsight.net/livy/batches' -H "X-Requested-By: admin"
31
+
```
28
32
29
-
**Examples**:
33
+
### Examples
30
34
31
35
* If the jar file is on the cluster storage (WASB)
Livy provides high-availability for Spark jobs running on the cluster. Here is a couple of examples.
59
86
60
87
* If the Livy service goes down after you have submitted a job remotely to a Spark cluster, the job continues to run in the background. When Livy is back up, it restores the status of the job and reports it back.
61
88
* Jupyter notebooks for HDInsight are powered by Livy in the backend. If a notebook is running a Spark job and the Livy service gets restarted, the notebook continues to run the code cells.
62
89
63
90
## Show me an example
91
+
64
92
In this section, we look at examples to use Livy Spark to submit batch job, monitor the progress of the job, and then delete it. The application we use in this example is the one developed in the article [Create a standalone Scala application and to run on HDInsight Spark cluster](apache-spark-create-standalone-application.md). The steps here assume that:
65
93
66
94
* You have already copied over the application jar to the storage account associated with the cluster.
@@ -69,97 +97,110 @@ In this section, we look at examples to use Livy Spark to submit batch job, moni
69
97
Perform the following steps:
70
98
71
99
1. Let us first verify that Livy Spark is running on the cluster. We can do so by getting a list of running batches. If you are running a job using Livy for the first time, the output should return zero.
72
-
73
-
curl -k --user "admin:mypassword1!" -v -X GET "https://mysparkcluster.azurehdinsight.net/livy/batches"
74
-
100
+
101
+
```cmd
102
+
curl -k --user "admin:mypassword1!" -v -X GET "https://mysparkcluster.azurehdinsight.net/livy/batches"
103
+
```
104
+
75
105
You should get an output similar to the following snippet:
76
-
77
-
< HTTP/1.1 200 OK
78
-
< Content-Type: application/json; charset=UTF-8
79
-
< Server: Microsoft-IIS/8.5
80
-
< X-Powered-By: ARR/2.5
81
-
< X-Powered-By: ASP.NET
82
-
< Date: Fri, 20 Nov 2015 23:47:53 GMT
83
-
< Content-Length: 34
84
-
<
85
-
{"from":0,"total":0,"sessions":[]}* Connection #0 to host mysparkcluster.azurehdinsight.net left intact
86
-
106
+
107
+
```output
108
+
< HTTP/1.1 200 OK
109
+
< Content-Type: application/json; charset=UTF-8
110
+
< Server: Microsoft-IIS/8.5
111
+
< X-Powered-By: ARR/2.5
112
+
< X-Powered-By: ASP.NET
113
+
< Date: Fri, 20 Nov 2015 23:47:53 GMT
114
+
< Content-Length: 34
115
+
<
116
+
{"from":0,"total":0,"sessions":[]}* Connection #0 to host mysparkcluster.azurehdinsight.net left intact
117
+
```
118
+
87
119
Notice how the last line in the output says **total:0**, which suggests no running batches.
88
120
89
121
2. Let us now submit a batch job. The following snippet uses an input file (input.txt) to pass the jar name and the class name as parameters. If you are running these steps from a Windows computer, using an input file is the recommended approach.
You should see an output similar to the following snippet:
135
-
136
-
< HTTP/1.1 200 OK
137
-
< Content-Type: application/json; charset=UTF-8
138
-
< Server: Microsoft-IIS/8.5
139
-
< X-Powered-By: ARR/2.5
140
-
< X-Powered-By: ASP.NET
141
-
< Date: Sat, 21 Nov 2015 18:51:54 GMT
142
-
< Content-Length: 17
143
-
<
144
-
{"msg":"deleted"}* Connection #0 to host mysparkcluster.azurehdinsight.net left intact
145
-
179
+
180
+
```output
181
+
< HTTP/1.1 200 OK
182
+
< Content-Type: application/json; charset=UTF-8
183
+
< Server: Microsoft-IIS/8.5
184
+
< X-Powered-By: ARR/2.5
185
+
< X-Powered-By: ASP.NET
186
+
< Date: Sat, 21 Nov 2015 18:51:54 GMT
187
+
< Content-Length: 17
188
+
<
189
+
{"msg":"deleted"}* Connection #0 to host mysparkcluster.azurehdinsight.net left intact
190
+
```
191
+
146
192
The last line of the output shows that the batch was successfully deleted. Deleting a job, while it is running, also kills the job. If you delete a job that has completed, successfully or otherwise, it deletes the job information completely.
147
193
148
194
## Updates to Livy configuration starting with HDInsight 3.5 version
149
195
150
-
HDInsight 3.5 clusters and above, by default, disable use of local file paths to access sample data files or jars. We encourage you to use the `wasb://` path instead to access jars or sample data files from the cluster.
196
+
HDInsight 3.5 clusters and above, by default, disable use of local file paths to access sample data files or jars. We encourage you to use the `wasb://` path instead to access jars or sample data files from the cluster.
151
197
152
198
## Submitting Livy jobs for a cluster within an Azure virtual network
153
199
154
200
If you connect to an HDInsight Spark cluster from within an Azure Virtual Network, you can directly connect to Livy on the cluster. In such a case, the URL for Livy endpoint is `http://<IP address of the headnode>:8998/batches`. Here, **8998** is the port on which Livy runs on the cluster headnode. For more information on accessing services on non-public ports, see [Ports used by Apache Hadoop services on HDInsight](../hdinsight-hadoop-port-settings-for-services.md).
155
201
156
-
157
-
158
-
159
-
160
-
## Next step
202
+
## Next steps
161
203
162
204
* [Apache Livy REST API documentation](https://livy.incubator.apache.org/docs/latest/rest-api.html)
163
205
* [Manage resources for the Apache Spark cluster in Azure HDInsight](apache-spark-resource-manager.md)
164
-
*[Track and debug jobs running on an Apache Spark cluster in HDInsight](apache-spark-job-debugging.md)
165
-
206
+
* [Track and debug jobs running on an Apache Spark cluster in HDInsight](apache-spark-job-debugging.md)
0 commit comments