README.rst 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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 /sessions
  162. -------------
  163. Returns all the active interactive sessions.
  164. Response Body
  165. ^^^^^^^^^^^^^
  166. +----------+-----------------+------+
  167. | name | description | type |
  168. +==========+=================+======+
  169. | sessions | `session`_ list | list |
  170. +----------+-----------------+------+
  171. POST /sessions
  172. --------------
  173. Creates a new interative Scala or Python shell in the cluster.
  174. Request Body
  175. ^^^^^^^^^^^^
  176. +----------------+--------------------------------------------------+----------------------------+
  177. | name | description | type |
  178. +================+==================================================+============================+
  179. | lang | session kind (scala or python) | `session kind`_ (required) |
  180. +----------------+--------------------------------------------------+----------------------------+
  181. | file | archive holding the file | path (required) |
  182. +----------------+--------------------------------------------------+----------------------------+
  183. | args | command line arguments | list of strings |
  184. +----------------+--------------------------------------------------+----------------------------+
  185. | className | application's java/spark main class | string |
  186. +----------------+--------------------------------------------------+----------------------------+
  187. | jars | files to be placed on the java classpath | list of paths |
  188. +----------------+--------------------------------------------------+----------------------------+
  189. | pyFiles | files to be placed on the PYTHONPATH | list of paths |
  190. +----------------+--------------------------------------------------+----------------------------+
  191. | files | files to be placed in executor working directory | list of paths |
  192. +----------------+--------------------------------------------------+----------------------------+
  193. | driverMemory | memory for driver | string |
  194. +----------------+--------------------------------------------------+----------------------------+
  195. | driverCores | number of cores used by driver | int |
  196. +----------------+--------------------------------------------------+----------------------------+
  197. | executorMemory | memory for executor | string |
  198. +----------------+--------------------------------------------------+----------------------------+
  199. | executorCores | number of cores used by executor | int |
  200. +----------------+--------------------------------------------------+----------------------------+
  201. | numExecutors | number of executor | int |
  202. +----------------+--------------------------------------------------+----------------------------+
  203. | archives | | list of paths |
  204. +----------------+--------------------------------------------------+----------------------------+
  205. Response Body
  206. ^^^^^^^^^^^^^
  207. The created `Session`_.
  208. GET /sessions/{sessionId}
  209. -------------------------
  210. Return the session information
  211. Response
  212. ^^^^^^^^
  213. The `Session`_.
  214. DELETE /sessions/{sessionId}
  215. -------------------------
  216. Kill the `Session`_ job.
  217. GET /sessions/{sessionId}/statements
  218. ------------------------------------
  219. Return all the statements in a session.
  220. Response Body
  221. ^^^^^^^^^^^^^
  222. +------------+-------------------+------+
  223. | name | description | type |
  224. +============+===================+======+
  225. | statements | `statement`_ list | list |
  226. +------------+-------------------+------+
  227. POST /sessions/{sessionId}/statements
  228. -------------------------------------
  229. Execute a statement in a session.
  230. Request Body
  231. ^^^^^^^^^^^^
  232. +------+---------------------+--------+
  233. | name | description | type |
  234. +======+=====================+========+
  235. | code | The code to execute | string |
  236. +------+---------------------+--------+
  237. Response Body
  238. ^^^^^^^^^^^^^
  239. The `statement`_ object.
  240. GET /batches
  241. ------------
  242. Return all the active batch jobs.
  243. Response Body
  244. ^^^^^^^^^^^^^
  245. +---------+---------------+------+
  246. | name | description | type |
  247. +=========+===============+======+
  248. | batches | `batch`_ list | list |
  249. +---------+---------------+------+
  250. POST /batches
  251. -------------
  252. Request Body
  253. ^^^^^^^^^^^^
  254. +----------------+--------------------------------------------------+-----------------+
  255. | name | description | type |
  256. +================+==================================================+=================+
  257. | file | archive holding the file | path (required) |
  258. +----------------+--------------------------------------------------+-----------------+
  259. | args | command line arguments | list of strings |
  260. +----------------+--------------------------------------------------+-----------------+
  261. | className | application's java/spark main class | string |
  262. +----------------+--------------------------------------------------+-----------------+
  263. | jars | files to be placed on the java classpath | list of paths |
  264. +----------------+--------------------------------------------------+-----------------+
  265. | pyFiles | files to be placed on the PYTHONPATH | list of paths |
  266. +----------------+--------------------------------------------------+-----------------+
  267. | files | files to be placed in executor working directory | list of paths |
  268. +----------------+--------------------------------------------------+-----------------+
  269. | driverMemory | memory for driver | string |
  270. +----------------+--------------------------------------------------+-----------------+
  271. | driverCores | number of cores used by driver | int |
  272. +----------------+--------------------------------------------------+-----------------+
  273. | executorMemory | memory for executor | string |
  274. +----------------+--------------------------------------------------+-----------------+
  275. | executorCores | number of cores used by executor | int |
  276. +----------------+--------------------------------------------------+-----------------+
  277. | numExecutors | number of executor | int |
  278. +----------------+--------------------------------------------------+-----------------+
  279. | archives | | list of paths |
  280. +----------------+--------------------------------------------------+-----------------+
  281. Response Body
  282. ^^^^^^^^^^^^^
  283. The created `Batch`_ object.
  284. GET /batches/{batchId}
  285. ----------------------
  286. Request Parameters
  287. ^^^^^^^^^^^^^^^^^^
  288. +------+-----------------------------+------+
  289. | name | description | type |
  290. +======+=============================+======+
  291. | from | offset | int |
  292. +------+-----------------------------+------+
  293. | size | amount of batches to return | int |
  294. +------+-----------------------------+------+
  295. Response Body
  296. ^^^^^^^^^^^^^
  297. +-------+-----------------------------+-----------------+
  298. | name | description | type |
  299. +=======+=============================+=================+
  300. | id | `batch`_ list | list |
  301. +-------+-----------------------------+-----------------+
  302. | state | The state of the batch | `batch`_ state |
  303. +-------+-----------------------------+-----------------+
  304. | lines | The output of the batch job | list of strings |
  305. +-------+-----------------------------+-----------------+
  306. DELETE /batches/{batchId}
  307. -------------------------
  308. Kill the `Batch`_ job.
  309. REST Objects
  310. ============
  311. Session
  312. -------
  313. Sessions represent an interactive shell.
  314. +----------------+--------------------------------------------------------------------------------+------------------+
  315. | name | description | type |
  316. +================+================================================================================+==================+
  317. | id | The session id | string |
  318. +----------------+--------------------------------------------------------------------------------+------------------+
  319. | state | The state of the session | `session state`_ |
  320. +----------------+--------------------------------------------------------------------------------+------------------+
  321. | kind | The session kind | `session kind`_ |
  322. +----------------+--------------------------------------------------------------------------------+------------------+
  323. | proxyUser | The user running this session | optional string |
  324. +----------------+--------------------------------------------------------------------------------+------------------+
  325. | jars | files to be placed on the java classpath | list of paths |
  326. +----------------+--------------------------------------------------------------------------------+------------------+
  327. | pyFiles | files to be placed on the PYTHONPATH | list of paths |
  328. +----------------+--------------------------------------------------------------------------------+------------------+
  329. | files | files to be placed in executor working directory | list of paths |
  330. +----------------+--------------------------------------------------------------------------------+------------------+
  331. | driverMemory | memory for driver | string |
  332. +----------------+--------------------------------------------------------------------------------+------------------+
  333. | driverCores | number of cores used by driver (YARN mode only) | int |
  334. +----------------+--------------------------------------------------------------------------------+------------------+
  335. | executorMemory | memory for executor | string |
  336. +----------------+--------------------------------------------------------------------------------+------------------+
  337. | executorCores | number of cores used by executor | int |
  338. +----------------+--------------------------------------------------------------------------------+------------------+
  339. | numExecutors | number of executors (YARN mode only) | int |
  340. +----------------+--------------------------------------------------------------------------------+------------------+
  341. | archives | Archives to be uncompressed in the executor working directory (YARN mode only) | list of paths |
  342. +----------------+--------------------------------------------------------------------------------+------------------+
  343. Session State
  344. ^^^^^^^^^^^^^
  345. +-------------+----------------------------------+
  346. | name | description |
  347. +=============+==================================+
  348. | not_started | session has not been started |
  349. +-------------+----------------------------------+
  350. | starting | session is starting |
  351. +-------------+----------------------------------+
  352. | idle | session is waiting for input |
  353. +-------------+----------------------------------+
  354. | busy | session is executing a statement |
  355. +-------------+----------------------------------+
  356. | error | session errored out |
  357. +-------------+----------------------------------+
  358. | dead | session has exited |
  359. +-------------+----------------------------------+
  360. Session Kind
  361. ^^^^^^^^^^^^
  362. +---------+----------------------------------+
  363. | name | description |
  364. +=========+==================================+
  365. | spark | interactive scala/spark session |
  366. +---------+----------------------------------+
  367. | pyspark | interactive python/spark session |
  368. +---------+----------------------------------+
  369. Statement
  370. ---------
  371. Statements represent the result of an execution statement.
  372. +--------+----------------------+---------------------+
  373. | name | description | type |
  374. +========+======================+=====================+
  375. | id | The statement id | integer |
  376. +--------+----------------------+---------------------+
  377. | state | The execution state | `statement state`_ |
  378. +--------+----------------------+---------------------+
  379. | output | The execution output | `statement output`_ |
  380. +--------+----------------------+---------------------+
  381. Statement State
  382. ^^^^^^^^^^^^^^^
  383. +-----------+----------------------------------+
  384. | name | description |
  385. +===========+==================================+
  386. | running | Statement is currently executing |
  387. +-----------+----------------------------------+
  388. | available | Statement has a ready response |
  389. +-----------+----------------------------------+
  390. | error | Statement failed |
  391. +-----------+----------------------------------+
  392. Statement Output
  393. ^^^^^^^^^^^^^^^^
  394. +-----------------+-------------------+----------------------------------+
  395. | name | description | type |
  396. +=================+===================+==================================+
  397. | status | execution status | string |
  398. +-----------------+-------------------+----------------------------------+
  399. | execution_count | a monotomically | integer |
  400. | | increasing number | |
  401. +-----------------+-------------------+----------------------------------+
  402. | data | statement output | an object mapping a mime type to |
  403. | | | the result. If the mime type is |
  404. | | | ``application/json``, the value |
  405. | | | will be a JSON value |
  406. +-----------------+-------------------+----------------------------------+
  407. Batch
  408. -----
  409. +----------------+--------------------------------------------------------------------------------+-----------------+
  410. | name | description | type |
  411. +================+================================================================================+=================+
  412. | file | archive holding the file | path (required) |
  413. +----------------+--------------------------------------------------------------------------------+-----------------+
  414. | args | command line arguments | list of strings |
  415. +----------------+--------------------------------------------------------------------------------+-----------------+
  416. | className | application's java/spark main class | string |
  417. +----------------+--------------------------------------------------------------------------------+-----------------+
  418. | jars | files to be placed on the java classpath | list of paths |
  419. +----------------+--------------------------------------------------------------------------------+-----------------+
  420. | pyFiles | files to be placed on the PYTHONPATH | list of paths |
  421. +----------------+--------------------------------------------------------------------------------+-----------------+
  422. | files | files to be placed in executor working directory | list of paths |
  423. +----------------+--------------------------------------------------------------------------------+-----------------+
  424. | driverMemory | memory for driver | string |
  425. +----------------+--------------------------------------------------------------------------------+-----------------+
  426. | driverCores | number of cores used by driver (YARN mode only) | int |
  427. +----------------+--------------------------------------------------------------------------------+-----------------+
  428. | executorMemory | memory for executor | string |
  429. +----------------+--------------------------------------------------------------------------------+-----------------+
  430. | executorCores | number of cores used by executor | int |
  431. +----------------+--------------------------------------------------------------------------------+-----------------+
  432. | numExecutors | number of executors (YARN mode only) | int |
  433. +----------------+--------------------------------------------------------------------------------+-----------------+
  434. | archives | Archives to be uncompressed in the executor working directory (YARN mode only) | list of paths |
  435. +----------------+--------------------------------------------------------------------------------+-----------------+
  436. License
  437. =======
  438. Apache License, Version 2.0
  439. http://www.apache.org/licenses/LICENSE-2.0