Skip to content

Instantly share code, notes, and snippets.

@josep2
Last active April 19, 2017 20:17
Show Gist options
  • Save josep2/a38636880bd2dc957deb88e7434b0dd3 to your computer and use it in GitHub Desktop.
Save josep2/a38636880bd2dc957deb88e7434b0dd3 to your computer and use it in GitHub Desktop.
// Start your Livy server: https://github.com/cloudera/livy#prerequisites
// First step is I instantiate a Spark Session and create a function that square in the spark shell and leave it open
val sparkSession = SparkSession.builder
.master("local[1]")
.appName("app_1")
.getOrCreate()
def squareMe(n: Int): Double = {
n*n
}
---------------------------------------------------------------------------------------------------------------------------
// Now I need to grab this same session so I can use this function. I just go a GET / Sessions, and return that ID
// Since that's contingent on how many sessions you have open I've left that code out and just copied the ID here
import org.apache.http.client.methods.HttpPost
import org.apache.http.entity.StringEntity
import org.apache.http.impl.client.DefaultHttpClient
import org.apache.http.util.EntityUtils
import com.google.gson.Gson
val sessionId: Int = <My ID>
// From here we need to do a POST request with the number we want to square
val numbToSquare: Int = 2
val post = new HttpPost(s"http://localhost:8998/session/$sessionId/statements")
post.setHeader("Content-type", "application/json")
val body: String = s"""{'code': 'squareMe($numbToSquare)'}""" // JSON STRING of the code we want to run
val jsonBody = new Gson().toJson(body)
post.setEntity(new StringEntity(jsonBody))
post.setEntity(new StringEntity(body)) // Add our code to the body
val response = (new DefaultHttpClient).execute(post) // Execute our request
// Finally, print out the results
val responseBody = EntityUtils.toString(response.getEntity)
println(responseBody)
{"id": 1,
"output": {"data": {"text/plain":4.0},
"execution_count": 1,
"status": "ok"},
"state": "available"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment