一个新手引发的问题。我正在尝试从REST API读取数据,该API通过分页返回数据。为了检索数据,我将调用相同的API,例如说5次。我想将该数据保存在databrick表中。 API的结果在json中。想法是每天进行一次API调用,并保存按日期划分的数据。所有示例均导致单个API调用。
有指针吗?谢谢
答案 0 :(得分:0)
我正在使用Google Api做类似的事情。最好的Apis会通过返回分页令牌(例如,您要求获得前X条记录,并给出一个令牌以表明它具有更多记录,因此您将一直循环直到没有更多记录为止。
在Spark方面,您可以使用Row继续将返回数据添加到List中,然后最终将其转换为Dataframe(之后,您可以使用databrick中的所有常规功能:(使用Google Api的Python代码Python Sdk,缩写大小) 但你应该知道:)
from pyspark.sql import *
..
credentials = service_account.Credentials.from_service_account_info(SERVICE_ACCOUNT_FILE, scopes=SCOPES, subject=IMPERSONATED_USER)
service = build('admin', 'reports_v1', credentials=credentials) #https://developers.google.com/api-client-library/python/start/get_started#build-the-service-object
# https://stackoverflow.com/questions/29903125/google-reporting-api-customer-usage-report
# https://developers.google.com/resources/api-libraries/documentation/admin/reports_v1/python/latest/admin_reports_v1.userUsageReport.html
# https://developers.google.com/api-client-library/python/start/get_started#build-the-service-object
print('Hitting Google Admin Reports - User Usage Api')
request = service.userUsageReport().get(userKey=keyUserFilter, date=keyDateFilter, filters=fieldFilter, maxResults=pageSize, parameters=fieldSelect)
rows = []
# get all pages until there are none left
while request != None:
response = request.execute()
rows.append(Row(id=ur.get("id"), item=response.get("item")))
request = service.userUsageReport().get_next(request, response)
print("end of loop")
df1 = spark.createDataFrame(rows)
display(df1)