【发布时间】:2022-01-21 15:01:28
【问题描述】:
我想从 azure cosmos db 中提取数据,我正在使用 python sdk 在数据块中进行连接。
我希望能够将我的 json.load(data) 保存到 pyspark 数据帧中,因为我需要将数据保存在 databricks delta 湖中,如何将这些数据读取到 pyspark 数据帧。下面是我的代码和示例数据
{
"appUuid": "aaaa-bbbb-cccc",
"SystemId": null,
"city": "Lancaster",
"state": "NY",
"zipCode": "140",
"field1": "others",
"field2": "others"
}
{
"appUuid": "bbbb-dddd-eeee",
"SystemId": null,
"city": "Alden ",
"state": "NY",
"zipCode": "140",
"field1": "others",
"field2": "others"
}
from azure.cosmos import CosmosClient
client = CosmosClient('https://<cosmos_client>.documents.azure.com:443/', credential='AccountKey')
DATABASE_NAME = 'TestDB'
database = client.get_database_client(DATABASE_NAME)
CONTAINER_NAME = 'Test'
container = database.get_container_client(CONTAINER_NAME)
import json
for item in container.query_items(
query='SELECT Top 10 * FROM Test',
enable_cross_partition_query=True):
data = json.dumps(item, indent=True)
print(data)
print(type(data))
# converting string to json dict
data1 = json.loads(data)
print(data1)
print(type(data1))
from pyspark.sql import *
from pyspark.sql import SparkSession,Row
spark = SparkSession.builder.getOrCreate()
df = spark.read.json(data1) -- I am getting error on this line.
display(df)
我收到此错误:
"IllegalArgumentException: java.net.URISyntaxException: Relative path in absolute URI: {"
【问题讨论】:
-
错误来自哪一行?
-
错误可以来自这一行 df = spark.read.json(data1) 我可以将数据读入 pyspark 数据帧
-
@gregeal - 请用这些细节编辑你的问题,而不是把它们放在 cmets 中。另外:究竟是什么被传递给
spark.read.json()- 参数不是应该是文件名而不是数据吗?我建议在这方面多花点时间。 -
@DavidMakogon,请问如何将我的 json.dumps 保存为 databricks 文件系统(dbfs)中的 json 文件?
标签: python json pyspark azure-cosmosdb azure-databricks