import requests

# Livy 服务器的 URL
LIVY_URL = "http://localhost:8998"

# 创建会话的请求参数
data = {
"kind": "pyspark",
"name": "fcbai"
}

# 发送创建会话的请求
response = requests.post(f"{LIVY_URL}/sessions", json=data)

# 获取会话 ID
session_id = response.json()["id"]

# 打印会话 ID
print(f"Session ID: {session_id}")


# 提交 Python 代码的请求参数
data = {
"code": "print('Hello, Livy!')"
}

# 提交代码的请求 URL
url = f"{LIVY_URL}/sessions/{session_id}/statements"

# 发送提交代码的请求
response = requests.post(url, json=data)

# 获取提交代码的执行结果
statement_id = response.json()["id"]

# 打印提交代码的执行结果
print(f"Statement ID: {statement_id}")


# 获取执行结果的请求 URL
url = f"{LIVY_URL}/sessions/{session_id}/statements/{statement_id}"

# 发送获取执行结果的请求
response = requests.get(url)

# 获取执行结果
status = response.json()["state"]

# 打印执行结果
print(f"Statement status: {status}")

# 关闭会话的请求 URL
url = f"{LIVY_URL}/sessions/{session_id}"

# 发送关闭会话的请求
requests.delete(url)

扫码手机观看或分享: