gpt4 book ai didi

python - 使用 ijson python 将 1.4 GB json 数据加载到 mysql

转载 作者:行者123 更新时间:2023-11-30 21:59:25 32 4
gpt4 key购买 nike

我遇到了几个讨论 ijson 在 python 中加载巨大 JSON 文件的线程,因为这是不消耗所有内存的方法。

我的文件大小约为 1.4 GB,它有多个节点(见下图),我只对一个包含大部分数据的节点 (c_driver_location) 感兴趣。

JSON_1.4GB

我的目标是:我只想提取 c_driver_location 节点数据并将其插入到 mysql 数据库表(它将有四列:id、longitude、latitude、timestamp)。

表ddl:

create table drv_locations_backup7May2017 (id bigint unsigned auto_increment primary key, drv_fb_id varchar(50), latitude DECIMAL(10, 8) NOT NULL, longitude DECIMAL(11, 8) NOT NULL, timestamp int )

我的问题是:我运行了附加代码的第一部分(直到连接到 mysql 之前),但它已经运行了 20 个小时,但仍然没有完成对 json 的解析。 (我在较小的文件上进行了测试,效果很好)。

是否有最佳方法可以使其更快、更高效?

import numpy as np
import pandas as pd
from pandas import Series, DataFrame
import ijson
import pymysql.cursors
import pymysql


filename = "D:\json_file.json"
drv_col_list = ['drv_fb_id','latitude','longitude','timestamp']
drv_df = DataFrame(columns = drv_col_list)
drv_df.timestamp = drv_df.timestamp.astype(int)

counter = 0
with open(filename, 'r') as fd:
parser = ijson.parse(fd)
for prefix, event, value in parser:
if prefix == 'c_driver_location' and str(event) == 'map_key':
drv_fb_id = value
counter = counter + 1
elif prefix.endswith('.latitude'):
latitude = value
elif prefix.endswith('.longitude'):
longitude = value
elif prefix.endswith('.timestamp'):
timestamp = value
elif prefix.endswith(drv_fb_id) and str(event) == 'end_map':
drv_df = drv_df.append(pd.DataFrame({'drv_fb_id':drv_fb_id,'latitude':latitude,'longitude':longitude,'timestamp':timestamp},index=[0]),ignore_index=True)
connection = pymysql.connect(host='53.000.00.00',
port = 3306,
user='user',
password='abcdefg',
db ='newdb',
# charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
# write to mysql
drv_df.to_sql(con=connection, name='drv_locations_backup7May2017', if_exists='replace', flavor='mysql')
connection.close()

最佳答案

您只需稍微修改代码即可生成数据转储。

import ijson


outfile = "D:\upload_data.txt"
filename = "D:\json_file.json"
drv_col_list = ['drv_fb_id','latitude','longitude','timestamp']
timestamp = drv_df.timestamp.astype(int)


ofile = open(outfile, "rw")

counter = drv_fb_id = latitude = longitude = 0
with open(filename, 'r') as fd:
parser = ijson.parse(fd)
for prefix, event, value in parser:
if prefix == 'c_driver_location' and str(event) == 'map_key':
drv_fb_id = value
counter = counter + 1
elif prefix.endswith('.latitude'):
latitude = value
elif prefix.endswith('.longitude'):
longitude = value
elif prefix.endswith('.timestamp'):
timestamp = value
elif prefix.endswith(drv_fb_id) and str(event) == 'end_map':
print >>ofile, ",".join(map(str, [drv_fb_id, latitude, longitude, timestamp]))

close(ofile)

现在您在 D:\upload_data.txt 中有一个逗号分隔的输出

代码未经测试。

我目前没有测试 mysql 数据库。我相信 mysql manual is easy to follow .你的表结构并不复杂。

关于python - 使用 ijson python 将 1.4 GB json 数据加载到 mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43910729/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com