我正在尝试创建一个数据管道,我从 REST API 请求数据。输出是一个嵌套的 json 文件,非常棒。我想将 json 文件读入 pyspark 数据框。当我在本地保存文件并使用以下代码时,这很好用:
from pyspark.sql import *
from pyspark.sql.functions import *
spark = SparkSession\
.builder\
.appName("jsontest")\
.getOrCreate()
raw_df = spark.read.json(r"my_json_path", multiLine='true')
但是,当我想在发出 API 请求后直接创建 pyspark 数据帧时,会出现以下错误:
我使用以下代码来调用 rest api 并转换为 pyspark 数据帧:
apiCallHeaders = {'Authorization': 'Bearer ' + bearer_token}
apiCallResponse = requests.get(data_url, headers=apiCallHeaders, verify=True)
json_rdd = spark.sparkContext.parallelize(apiCallResponse.text)
raw_df = spark.read.json(json_rdd)
以下是部分响应输出
{"networks":[{"href":"/v2/networks/velobike-moscow","id":"velobike-moscow","name":"Velobike"},{"href":"/v2/networks/bycyklen","id":"bycyklen","name":"Bycyklen"},{"href":"/v2/networks/nu-connect","id":"nu-connect","name":"Nu-Connect"},{"href":"/v2/networks/baerum-bysykkel","id":"baerum-bysykkel","name":"Bysykkel"},{"href":"/v2/networks/bysykkelen","id":"bysykkelen","name":"Bysykkelen"},{"href":"/v2/networks/onroll-a-rua","id":"onroll-a-rua","name":"Onroll"},{"href":"/v2/networks/onroll-albacete","id":"onroll-albacete","name":"Onroll"},{"href":"/v2/networks/onroll-alhama-de-murcia","id":"onroll-alhama-de-murcia","name":"Onroll"},{"href":"/v2/networks/onroll-almunecar","id":"onroll-almunecar","name":"Onroll"},{"href":"/v2/networks/onroll-antequera","id":"onroll-antequera","name":"Onroll"},{"href":"/v2/networks/onroll-aranda-de-duero","id":"onroll-aranda-de-duero","name":"Onroll"}
我希望我的问题有意义,有人可以提供帮助。
提前致谢!