I am trying to create a datafrmae from fixedwidth file and load into postgresql database. My input file is very huge (~16GB) and 20Million records. So if i create dataframe it is consuming most of the available RAM. It is taking long time to complete. So i thought of using chunksize(using python generator) option and commit records to table. But it is failing with 'AttributeError: 'generator' object has no attribute 'to_sql'
error.
Inspired by this answer here https://stackoverflow.com/a/47257676/2799214
input file: test_file.txt
XOXOXOXOXOXO9
AOAOAOAOAOAO8
BOBOBOBOBOBO7
COCOCOCOCOCO6
DODODODODODO5
EOEOEOEOEOEO4
FOFOFOFOFOFO3
GOGOGOGOGOGO2
HOHOHOHOHOHO1
sample.py
import pandas.io.sql as psql
import pandas as pd
from sqlalchemy import create_engine
def chunck_generator(filename, header=False,chunk_size = 10 ** 5):
for chunk in pd.read_fwf(filename, colspecs=[[0,12],[12,13]],index_col=False,header=None, iterator=True, chunksize=chunk_size):
yield (chunk)
def _generator( engine, filename, header=False,chunk_size = 10 ** 5):
chunk = chunck_generator(filename, header=False,chunk_size = 10 ** 5)
chunk.to_sql('sample_table', engine, if_exists='replace', schema='sample_schema', index=False)
yield row
if __name__ == "__main__":
filename = r'test_file.txt'
engine = create_engine('postgresql://ABCD:ABCD@ip:port/database')
c = engine.connect()
conn = c.connection
generator = _generator(engine=engine, filename=filename)
while True:
print(next(generator))
conn.close()
Error:
chunk.to_sql('sample_table', engine, if_exists='replace', schema='sample_schema', index=False)
AttributeError: 'generator' object has no attribute 'to_sql'
My Primary goal is to improve performance. Please help me in resolving the issue or please suggest better approach. Thanks in advance.