for row in rows: v = json.loads(row) insert_sql = "insert into ... values (%s,%s)" % (v[1], v[2]) cur.execute(insert_sql) cur.commit() conn.close()
def insert(Rows): # 将 Rows 切割成 100 份,获得 100 个 rows,启用 n 个 do_insert 线程 rows_list = split_list(Rows, 100) pool = threadpool.ThreadPool(n) requests = threadpool.makeRequest(do_insert, rows_list) [pool.putRequest(req) for req in requests] pool.wait()
def main(): # 载入 json 文本数据源 # 按行读取,每读取 100 万行数据调用一次 insert() with open('import.json','r') as f: index=0 Rows=[] for line in f: Rows.append(line) index += 1 if index % 1000000 == 0: insert(Rows) ```