I installed apache spark and apache Livy in my system. When I am running a python code, it's giving error
'u'java.lang.IllegalStateException: Session is in state starting''
By default Apchy Livy run on port number 8998. My python code is
import json, pprint, requests, textwrap
host = 'http://localhost:8998'
data = {'kind': 'pyspark'}
headers = {'Content-Type': 'application/json'}
r = requests.post(host + '/sessions', data=json.dumps(data),
headers=headers)
session_url = host + r.headers['location']
statements_url = session_url + '/statements'
data = {
'code': textwrap.dedent("""
import random
NUM_SAMPLES = 100000
def sample(p):
x, y = random.random(), random.random()
return 1 if x*x + y*y < 1 else 0
count = sc.parallelize(xrange(0,
NUM_SAMPLES)).map(sample).reduce(lambda a, b: a +b)
print "Pi is roughly %f" % (4.0 * count / NUM_SAMPLES)
""")
}
r = requests.post(statements_url, data=json.dumps(data),
headers=headers)
pprint.pprint(r.json())
{u'id': 12,
u'output': {u'data': {u'text/plain': u'Pi is roughly 3.136000'},
u'execution_count': 12,
u'status': u'ok'},
u'state': u'running'}
How I can resolve this error?
After creating a session, you need to check the returned state. submit statements, only after the state of your session turned into
idle
. The bellow code worked for me.You can print the returned value to debug your setup. If the state continued to be
starting
ordead
there is a problem in your setup. Maybe some permission issue or any other issue regarding initiating spark context by livy. Check that out in livy UI stderr.This return sessionId is now ready to be used for submitting statements. You can simply call
POST /sessions/{sessionId}/statements
to submit your statements.