首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Python将JSON数据转换为Avro格式

如何使用Python将JSON数据转换为Avro格式
EN

Stack Overflow用户
提问于 2020-08-06 17:16:04
回答 2查看 2.3K关注 0票数 0

我想将下面的JSON数据转换成avro格式,我使用下面的代码片段以avro格式编写JSON数据,但是收到了一个错误。如果有人能帮忙的话,那就太好了。

代码语言:javascript
复制
from fastavro import writer, reader, schema
from rec_avro import to_rec_avro_destructive, from_rec_avro_destructive, rec_avro_schema

def getweatherdata():
    url = 'https://api.openweathermap.org/data/2.5/onecall?lat=33.441792&lon=-94.037689&exclude=hourly,daily&appid=' + apikey
    response = requests.get(url)
    data = response.text
    return data
 
def turntoavro():
    avro_objects = (to_rec_avro_destructive(rec) for rec in getweatherdata())
    with open('json_in_avro.avro', 'wb') as f_out:
        writer(f_out, schema.parse_schema(rec_avro_schema()), avro_objects)



turntoavro()
代码语言:javascript
复制
    Error details:
    
      File "fastavro/_write.pyx", line 269, in fastavro._write.write_record
    TypeError: Expected dict, got str
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "datalake.py", line 30, in <module>
        turntoavro()
      File "datalake.py", line 26, in turntoavro
        writer(f_out, schema.parse_schema(rec_avro_schema()), avro_objects)
      File "fastavro/_write.pyx", line 652, in fastavro._write.writer
      File "fastavro/_write.pyx", line 605, in fastavro._write.Writer.write
      File "fastavro/_write.pyx", line 341, in fastavro._write.write_data
      File "fastavro/_write.pyx", line 278, in fastavro._write.write_record
    AttributeError: 'str' object has no attribute 'get'

样本数据:

代码语言:javascript
复制
    {
      "lat": 33.44,
      "lon": -94.04,
      "timezone": "America/Chicago",
      "timezone_offset": -18000

   }
EN

回答 2

Stack Overflow用户

发布于 2020-08-08 01:09:20

为了检索对请求的响应,您使用了response.text,它将响应作为字符串返回,而不是以JSON格式返回。您必须使用response.json()才能使其具有JSON格式:

代码语言:javascript
复制
import json    
def getweatherdata():
    url = 'https://api.openweathermap.org/data/2.5/onecall?lat=33.441792&lon=-94.037689&exclude=hourly,daily&appid=' + apikey
    response = requests.get(url)
    data = response.json()
    return data
     
def turntoavro():
    avro_objects = (to_rec_avro_destructive(rec) for rec in getweatherdata())
    with open('json_in_avro.avro', 'wb') as f_out:
        writer(f_out, schema.parse_schema(rec_avro_schema()), avro_objects)
    
    
    
turntoavro()
票数 0
EN

Stack Overflow用户

发布于 2020-08-10 14:46:32

正如其中一个答案所提到的,您可能希望使用response.json()而不是response.text,这样就可以获得一个实际的JSON字典。

但是,另一个问题是getweatherdata()返回一个字典,所以当您执行avro_objects = (to_rec_avro_destructive(rec) for rec in getweatherdata())时,您将遍历该字典中的键。相反,您应该做avro_objects = [to_rec_avro_destructive(getweatherdata())]

我认为这个代码应该适用于你:

代码语言:javascript
复制
from fastavro import writer, reader, schema
from rec_avro import to_rec_avro_destructive, from_rec_avro_destructive, rec_avro_schema

def getweatherdata():
    url = 'https://api.openweathermap.org/data/2.5/onecall?lat=33.441792&lon=-94.037689&exclude=hourly,daily&appid=' + apikey
    response = requests.get(url)
    data = response.json()
    return data
 
def turntoavro():
    avro_objects = [to_rec_avro_destructive(getweatherdata())]
    with open('json_in_avro.avro', 'wb') as f_out:
        writer(f_out, schema.parse_schema(rec_avro_schema()), avro_objects)

turntoavro()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63288679

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档