我正在尝试将json文件转换为json serde格式,使用Python将json serde文件上载到AWS S3 bucket,以便Amazon Athena(Presto/Hive)可以读取S3 bucket中的文件
根据AWS产品文档,典型的json文件不是有效的格式;json文件需要采用json serde格式:https://docs.aws.amazon.com/athena/latest/ug/json-serde.html
在本地,我可以使用以下代码将json文件转换为json serde格式:
import json
with open('xx_original_file.json','r',encoding='utf-8') as json_file:
data = json.load(json_file)
result = [json.dumps(record) for record in data]
with open('xx_new_file.json', 'w') as obj:
for i in result:
obj.write(i+'\n')
在Python中有没有一种等效的方法可以让我在s3存储桶中存储一个新的json serde文件?到目前为止,我构建的Python脚本不断出现错误:
import json
import os
import boto3
s3 = boto3.client('s3')
bucket = 'my_bucket_name'
key = 'xx_original_file.json'
response = s3.get_object(Bucket=bucket,Key=key)
content = response['Body']
jsonObject = json.loads(content.read())
result = [json.dumps(record) for record in jsonObject]
new_results = []
for i in result:
new_results.append(i+'\n')
new_key = 'xx_new_file.json'
s3.put_object(Bucket=bucket,Key=new_key,Body=new_results)
错误消息:ParamValidationError:参数验证失败: 参数体的类型无效,值:{json data}类型:<;类“列表”>;,有效类型:<;类“字节”>&书信电报;类“bytearray”>;,类文件对象
这是一个简单的修复方法,我需要将列表转换为字符串,然后将其转换为字节
相关问题 更多 >
编程相关推荐