【问题标题】:foursquare api data pull from databricks从数据块中提取foursquare api数据
【发布时间】:2020-02-10 04:45:54
【问题描述】:

我正在使用以下命令从运行正常的foursquare api 中提取数据。如何将 json 输出写为 databricks 中的表?我不能在数据输出上使用显示/显示功能。

import json, requests
url = 'https://api.foursquare.com/v2/venues/explore'

params = dict(
  client_id='CLIENT_ID',
  client_secret='CLIENT_SECRET',
  v='20180323',
  ll='40.7243,-74.0018',
  query='coffee',
  limit=1
)
resp = requests.get(url=url, params=params)
data = json.loads(resp.text)

【问题讨论】:

  • 您可以将 JSON 响应转储到文件中,然后使用 Spark spark.read.json 将其读回 DataFrame。或者您可以按照here 的说明直接从 JSON 字符串创建 DF。

标签: python pyspark foursquare pyspark-dataframes


【解决方案1】:

您可以按如下方式读写接收到的数据:

df = spark.read.json(resp.text)
location = 'dbfs:/tmp/test.json'
df.write.json(location)

然后使用创建的文件创建一个表:

spark.sql(f'''
CREATE TABLE IF NOT EXISTS foursquare
USING JSON 
LOCATION "{location}"
''')

【讨论】:

  • 我收到这个错误 df = spark.read.json(json.loads(resp.text)) :- TypeError: path can only string, list or RDD
猜你喜欢
  • 1970-01-01
  • 2011-10-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-14
  • 1970-01-01
  • 2020-03-14
  • 1970-01-01
相关资源
最近更新 更多