README.rst 17 KB

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