README.rst 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. Welcome to Livy, the REST Spark Server
  2. ======================================
  3. Livy is an open source REST interface (in **Beta**) for interacting with a
  4. remote Spark Shell running locally or from inside YARN.
  5. * Interactive Spark Scala, Python shells
  6. * Batch submissions in Scala, Java, Python
  7. * Multi users can submit to the same server with their own credentials in YARN mode (impersonation support)
  8. * Can be used for submitting jobs from anywhere with REST
  9. * Does not require any code change to your programs
  10. Livy is used for powering the `Spark Notebook`_ of `Hue 3.8`_, which you can see the
  11. `implementation here`_.
  12. .. _Spark Notebook: http://gethue.com/new-notebook-application-for-spark-sql/
  13. .. _Hue 3.8: http://gethue.com/hue-3-8-with-an-oozie-editor-revamp-better-performances-improved-spark-ui-is-out/
  14. .. _implementation here: https://github.com/cloudera/hue/blob/master/apps/spark/src/spark/job_server_api.py
  15. Prerequisites
  16. =============
  17. To build Livy, you will need:
  18. Debian/Ubuntu:
  19. * mvn (from ``maven`` package or maven3 tarball)
  20. * openjdk-7-jdk (or Oracle Java7 jdk)
  21. * spark 1.3 from (from `Apache Spark tarball`_)
  22. Redhat/CentOS:
  23. * mvn (from ``maven`` package or maven3 tarball)
  24. * java-1.7.0-openjdk (or Oracle Java7 jdk)
  25. * spark 1.3 (from `Apache Spark tarball`_)
  26. MacOS:
  27. * Xcode command line tools
  28. * Oracle's JDK 1.7+
  29. * Maven (Homebrew)
  30. * apache-spark (Homebrew)
  31. .. _Apache Spark Tarball: https://spark.apache.org/downloads.html
  32. Building Livy
  33. =============
  34. Livy is currently built by the `Hue Build System`_, it can also be built on
  35. it's own (aka without any other Hue dependency) with `Apache Maven`_. To build,
  36. run:
  37. .. code:: shell
  38. % cd $HUE_HOME/apps/spark/java
  39. % mvn -DskipTests clean package
  40. .. _Hue Build System: https://github.com/cloudera/hue/#getting-started
  41. .. _Apache Maven: http://maven.apache.org
  42. Running Tests
  43. =============
  44. In order to run the Livy Tests, first follow the instructions in `Building
  45. Livy`_. Then run:
  46. .. code:: shell
  47. % cd $HUE_HOME/apps/spark/java
  48. % mvn test
  49. Running Livy
  50. ============
  51. In order to run Livy with local sessions, start the server with:
  52. .. code:: shell
  53. % ./bin/livy-server
  54. Or with YARN sessions by running:
  55. .. code:: shell
  56. % env \
  57. LIVY_SERVER_JAVA_OPTS="-Dlivy.server.session.factory=yarn" \
  58. CLASSPATH=`hadoop classpath` \
  59. ./bin/livy-server
  60. Spark Example
  61. =============
  62. Now to see it in action by interacting with it in Python with the `Requests`_
  63. library. By default livy runs on port 8998 (which can be changed with the
  64. ``livy_server_port config`` option). We’ll start off with a Spark session that
  65. takes Scala code:
  66. .. code:: python
  67. >>> import json, pprint, requests, textwrap
  68. >>> host = 'http://localhost:8998'
  69. >>> data = {'lang': 'spark'}
  70. >>> r = requests.post(host + '/sessions', data=json.dumps(data), headers=headers)
  71. >>> r.json()
  72. {u'state': u'starting', u'id': 0, u’kind’: u’spark’}
  73. Once the session has completed starting up, it transitions to the idle state:
  74. .. code:: python
  75. >>> session_url = host + r.headers['location']
  76. >>> r = requests.get(session_url, headers=headers)
  77. >>> r.json()
  78. {u'state': u'idle', u'id': 0, u’kind’: u’spark’}
  79. Now we can execute Scala by passing in a simple JSON command:
  80. .. code:: python
  81. >>> data = {'code': '1 + 1'}
  82. >>> r = requests.post(statements_url, data=json.dumps(data), headers=headers)
  83. >>> r.json()
  84. {u'output': None, u'state': u'running', u'id': 0}
  85. If a statement takes longer than a few milliseconds to execute, Livy returns
  86. early and provides a URL that can be polled until it is complete:
  87. .. code:: python
  88. >>> statement_url = host + r.headers['location']
  89. >>> r = requests.get(statement_url, headers=headers)
  90. >>> pprint.pprint(r.json())
  91. [{u'id': 0,
  92. u'output': {u'data': {u'text/plain': u'res0: Int = 2'},
  93. u'execution_count': 0,
  94. u'status': u'ok'},
  95. u'state': u'available'}]
  96. That was a pretty simple example. More interesting is using Spark to estimate
  97. PI. This is from the `Spark Examples`_:
  98. .. code:: python
  99. >>> data = {
  100. ... 'code': textwrap.dedent("""\
  101. ... val NUM_SAMPLES = 100000;
  102. ... val count = sc.parallelize(1 to NUM_SAMPLES).map { i =>
  103. ... val x = Math.random();
  104. ... val y = Math.random();
  105. ... if (x*x + y*y < 1) 1 else 0
  106. ... }.reduce(_ + _);
  107. ... println(\"Pi is roughly \" + 4.0 * count / NUM_SAMPLES)
  108. ... """)
  109. ... }
  110. >>> r = requests.post(statements_url, data=json.dumps(data), headers=headers)
  111. >>> pprint.pprint(r.json())
  112. {u'id': 1,
  113. u'output': {u'data': {u'text/plain': u'Pi is roughly 3.14004\nNUM_SAMPLES: Int = 100000\ncount: Int = 78501'},
  114. u'execution_count': 1,
  115. u'status': u'ok'},
  116. u'state': u'available'}
  117. Finally, lets close our session:
  118. .. code:: python
  119. >>> session_url = 'http://localhost:8998/sessions/0'
  120. >>> requests.delete(session_url, headers=headers)
  121. <Response [204]>
  122. .. _Requests: http://docs.python-requests.org/en/latest/
  123. .. _Spark Examples: https://spark.apache.org/examples.html
  124. PySpark Example
  125. ===============
  126. pyspark has the exact same API, just with a different initial command:
  127. .. code:: python
  128. >>> data = {'lang': 'pyspark'}
  129. >>> r = requests.post(host + '/sessions', data=json.dumps(data), headers=headers)
  130. >>> r.json()
  131. {u'id': 1, u'state': u'idle'}
  132. The PI example from before then can be run as:
  133. .. code:: python
  134. >>> data = {
  135. ... 'code': textwrap.dedent("""\
  136. ... import random
  137. ... NUM_SAMPLES = 100000
  138. ... def sample(p):
  139. ... x, y = random.random(), random.random()
  140. ... return 1 if x*x + y*y < 1 else 0
  141. ...
  142. ... count = sc.parallelize(xrange(0, NUM_SAMPLES)).map(sample) \
  143. ... .reduce(lambda a, b: a + b)
  144. ... print "Pi is roughly %f" % (4.0 * count / NUM_SAMPLES)
  145. ... """)
  146. ... }
  147. >>> r = requests.post(statements_url, data=json.dumps(data), headers=headers)
  148. >>> pprint.pprint(r.json())
  149. {u'id': 12,
  150. u'output': {u'data': {u'text/plain': u'Pi is roughly 3.136000'},
  151. u'execution_count': 12,
  152. u'status': u'ok'},
  153. u'state': u'running'}
  154. Community
  155. =========
  156. * User group: http://groups.google.com/a/cloudera.org/group/hue-user
  157. * Jira: https://issues.cloudera.org/browse/HUE-2588
  158. * Reviews: https://review.cloudera.org/dashboard/?view=to-group&group=hue (repo 'hue-rw')
  159. REST API
  160. ========
  161. GET /batches
  162. ------------
  163. Return all the active batch jobs.
  164. Response Body
  165. ^^^^^^^^^^^^^
  166. +---------+---------------+------+
  167. | name | description | type |
  168. +=========+===============+======+
  169. | batches | `batch`_ list | list |
  170. +---------+---------------+------+
  171. POST /batches
  172. -------------
  173. Request Body
  174. ^^^^^^^^^^^^
  175. +----------------+--------------------------------------------------+-----------------+
  176. | name | description | type |
  177. +================+==================================================+=================+
  178. | file | archive holding the file | path (required) |
  179. +----------------+--------------------------------------------------+-----------------+
  180. | args | command line arguments | list of strings |
  181. +----------------+--------------------------------------------------+-----------------+
  182. | className | application's java/spark main class | string |
  183. +----------------+--------------------------------------------------+-----------------+
  184. | jars | files to be placed on the java classpath | list of paths |
  185. +----------------+--------------------------------------------------+-----------------+
  186. | pyFiles | files to be placed on the PYTHONPATH | list of paths |
  187. +----------------+--------------------------------------------------+-----------------+
  188. | files | files to be placed in executor working directory | list of paths |
  189. +----------------+--------------------------------------------------+-----------------+
  190. | driverMemory | memory for driver | string |
  191. +----------------+--------------------------------------------------+-----------------+
  192. | driverCores | number of cores used by driver | int |
  193. +----------------+--------------------------------------------------+-----------------+
  194. | executorMemory | memory for executor | string |
  195. +----------------+--------------------------------------------------+-----------------+
  196. | executorCores | number of cores used by executor | int |
  197. +----------------+--------------------------------------------------+-----------------+
  198. | numExecutors | number of executor | int |
  199. +----------------+--------------------------------------------------+-----------------+
  200. | archives | | list of paths |
  201. +----------------+--------------------------------------------------+-----------------+
  202. Response Body
  203. ^^^^^^^^^^^^^
  204. The created `Batch`_ object.
  205. GET /batches/{batchId}
  206. ----------------------
  207. Request Parameters
  208. ^^^^^^^^^^^^^^^^^^
  209. +------+-----------------------------+------+
  210. | name | description | type |
  211. +======+=============================+======+
  212. | from | offset | int |
  213. +------+-----------------------------+------+
  214. | size | amount of batches to return | int |
  215. +------+-----------------------------+------+
  216. Response Body
  217. ^^^^^^^^^^^^^
  218. +-------+-----------------------------+-----------------+
  219. | name | description | type |
  220. +=======+=============================+=================+
  221. | id | `batch`_ list | list |
  222. +-------+-----------------------------+-----------------+
  223. | state | The state of the batch | `batch`_ state |
  224. +-------+-----------------------------+-----------------+
  225. | lines | The output of the batch job | list of strings |
  226. +-------+-----------------------------+-----------------+
  227. DELETE /batches/{batchId}
  228. -------------------------
  229. Kill the `Batch`_ job.
  230. GET /sessions
  231. -------------
  232. Returns all the active interactive sessions.
  233. Response Body
  234. ^^^^^^^^^^^^^
  235. +----------+-----------------+------+
  236. | name | description | type |
  237. +==========+=================+======+
  238. | sessions | `session`_ list | list |
  239. +----------+-----------------+------+
  240. POST /sessions
  241. --------------
  242. Request Body
  243. ^^^^^^^^^^^^
  244. +------+--------------+----------------------------+
  245. | name | description | type |
  246. +======+==============+============================+
  247. | lang | session kind | `session kind`_ (required) |
  248. +------+--------------+----------------------------+
  249. Response Body
  250. ^^^^^^^^^^^^^
  251. The created `Session`_.
  252. GET /sessions/{sessionId}
  253. -------------------------
  254. Return the session information
  255. Response
  256. ^^^^^^^^
  257. The `Session`_.
  258. DELETE /sessions/{batchId}
  259. -------------------------
  260. Kill the `Session`_ job.
  261. GET /sessions/{sessionId}/statements
  262. ------------------------------------
  263. Return all the statements in a session.
  264. Response Body
  265. ^^^^^^^^^^^^^
  266. +------------+-------------------+------+
  267. | name | description | type |
  268. +============+===================+======+
  269. | statements | `statement`_ list | list |
  270. +------------+-------------------+------+
  271. POST /sessions/{sessionId}/statements
  272. -------------------------------------
  273. Execute a statement in a session.
  274. Request Body
  275. ^^^^^^^^^^^^
  276. +------+---------------------+--------+
  277. | name | description | type |
  278. +======+=====================+========+
  279. | code | The code to execute | string |
  280. +------+---------------------+--------+
  281. Response Body
  282. ^^^^^^^^^^^^^
  283. The `statement`_ object.
  284. REST Objects
  285. ============
  286. Batch
  287. -----
  288. +----------------+--------------------------------------------------------------------------------+-----------------+
  289. | name | description | type |
  290. +================+================================================================================+=================+
  291. | file | archive holding the file | path (required) |
  292. +----------------+--------------------------------------------------------------------------------+-----------------+
  293. | args | command line arguments | list of strings |
  294. +----------------+--------------------------------------------------------------------------------+-----------------+
  295. | className | application's java/spark main class | string |
  296. +----------------+--------------------------------------------------------------------------------+-----------------+
  297. | jars | files to be placed on the java classpath | list of paths |
  298. +----------------+--------------------------------------------------------------------------------+-----------------+
  299. | pyFiles | files to be placed on the PYTHONPATH | list of paths |
  300. +----------------+--------------------------------------------------------------------------------+-----------------+
  301. | files | files to be placed in executor working directory | list of paths |
  302. +----------------+--------------------------------------------------------------------------------+-----------------+
  303. | driverMemory | memory for driver | string |
  304. +----------------+--------------------------------------------------------------------------------+-----------------+
  305. | driverCores | number of cores used by driver (YARN mode only) | int |
  306. +----------------+--------------------------------------------------------------------------------+-----------------+
  307. | executorMemory | memory for executor | string |
  308. +----------------+--------------------------------------------------------------------------------+-----------------+
  309. | executorCores | number of cores used by executor | int |
  310. +----------------+--------------------------------------------------------------------------------+-----------------+
  311. | numExecutors | number of executors (YARN mode only) | int |
  312. +----------------+--------------------------------------------------------------------------------+-----------------+
  313. | archives | Archives to be uncompressed in the executor working directory (YARN mode only) | list of paths |
  314. +----------------+--------------------------------------------------------------------------------+-----------------+
  315. Session
  316. -------
  317. Sessions represent an interactive shell.
  318. +----------------+--------------------------------------------------------------------------------+------------------+
  319. | name | description | type |
  320. +================+================================================================================+==================+
  321. | id | The session id | string |
  322. +----------------+--------------------------------------------------------------------------------+------------------+
  323. | state | The state of the session | `session state`_ |
  324. +----------------+--------------------------------------------------------------------------------+------------------+
  325. | kind | The session kind | `session kind`_ |
  326. +----------------+--------------------------------------------------------------------------------+------------------+
  327. | proxyUser | The user running this session | optional string |
  328. +----------------+--------------------------------------------------------------------------------+------------------+
  329. | jars | files to be placed on the java classpath | list of paths |
  330. +----------------+--------------------------------------------------------------------------------+------------------+
  331. | pyFiles | files to be placed on the PYTHONPATH | list of paths |
  332. +----------------+--------------------------------------------------------------------------------+------------------+
  333. | files | files to be placed in executor working directory | list of paths |
  334. +----------------+--------------------------------------------------------------------------------+------------------+
  335. | driverMemory | memory for driver | string |
  336. +----------------+--------------------------------------------------------------------------------+------------------+
  337. | driverCores | number of cores used by driver (YARN mode only) | int |
  338. +----------------+--------------------------------------------------------------------------------+------------------+
  339. | executorMemory | memory for executor | string |
  340. +----------------+--------------------------------------------------------------------------------+------------------+
  341. | executorCores | number of cores used by executor | int |
  342. +----------------+--------------------------------------------------------------------------------+------------------+
  343. | numExecutors | number of executors (YARN mode only) | int |
  344. +----------------+--------------------------------------------------------------------------------+------------------+
  345. | archives | Archives to be uncompressed in the executor working directory (YARN mode only) | list of paths |
  346. +----------------+--------------------------------------------------------------------------------+------------------+
  347. Session State
  348. ^^^^^^^^^^^^^
  349. +-------------+----------------------------------+
  350. | name | description |
  351. +=============+==================================+
  352. | not_started | session has not been started |
  353. +-------------+----------------------------------+
  354. | starting | session is starting |
  355. +-------------+----------------------------------+
  356. | idle | session is waiting for input |
  357. +-------------+----------------------------------+
  358. | busy | session is executing a statement |
  359. +-------------+----------------------------------+
  360. | error | session errored out |
  361. +-------------+----------------------------------+
  362. | dead | session has exited |
  363. +-------------+----------------------------------+
  364. Session Kind
  365. ^^^^^^^^^^^^
  366. +---------+----------------------------------+
  367. | name | description |
  368. +=========+==================================+
  369. | spark | interactive scala/spark session |
  370. +---------+----------------------------------+
  371. | pyspark | interactive python/spark session |
  372. +---------+----------------------------------+
  373. Statement
  374. ---------
  375. Statements represent the result of an execution statement.
  376. +--------+----------------------+---------------------+
  377. | name | description | type |
  378. +========+======================+=====================+
  379. | id | The statement id | integer |
  380. +--------+----------------------+---------------------+
  381. | state | The execution state | `statement state`_ |
  382. +--------+----------------------+---------------------+
  383. | output | The execution output | `statement output`_ |
  384. +--------+----------------------+---------------------+
  385. Statement State
  386. ^^^^^^^^^^^^^^^
  387. +-----------+----------------------------------+
  388. | name | description |
  389. +===========+==================================+
  390. | running | Statement is currently executing |
  391. +-----------+----------------------------------+
  392. | available | Statement has a ready response |
  393. +-----------+----------------------------------+
  394. | error | Statement failed |
  395. +-----------+----------------------------------+
  396. Statement Output
  397. ^^^^^^^^^^^^^^^^
  398. +-----------------+-------------------+----------------------------------+
  399. | name | description | type |
  400. +=================+===================+==================================+
  401. | status | execution status | string |
  402. +-----------------+-------------------+----------------------------------+
  403. | execution_count | a monotomically | integer |
  404. | | increasing number | |
  405. +-----------------+-------------------+----------------------------------+
  406. | data | statement output | an object mapping a mime type to |
  407. | | | the result. If the mime type is |
  408. | | | ``application/json``, the value |
  409. | | | will be a JSON value |
  410. +-----------------+-------------------+----------------------------------+
  411. License
  412. =======
  413. Apache License, Version 2.0
  414. http://www.apache.org/licenses/LICENSE-2.0