ingest_csv.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #!/usr/bin/env python3
  2. import pandas as pd
  3. import myloginpath
  4. import urllib.parse
  5. from sqlalchemy import create_engine, text
  6. from sqlalchemy.types import VARCHAR, DOUBLE
  7. from sqlalchemy.dialects.mysql import LONGTEXT
  8. import os
  9. import sys
  10. from snmp_notifier import notifier
  11. def get_loader_engine():
  12. try:
  13. import os
  14. db_host = os.environ.get('DB_HOST')
  15. db_user = os.environ.get('DB_USER')
  16. db_pass = os.environ.get('DB_PASS')
  17. if db_host and db_user and db_pass:
  18. password = urllib.parse.quote_plus(db_pass)
  19. conn_str = f"mysql+pymysql://{db_user}:{password}@{db_host}/food_db?charset=utf8mb4"
  20. return create_engine(conn_str)
  21. conf = myloginpath.parse('app_loader')
  22. user = conf.get('user')
  23. password = urllib.parse.quote_plus(conf.get('password'))
  24. host = conf.get('host', '127.0.0.1')
  25. database = 'food_db'
  26. conn_str = f"mysql+pymysql://{user}:{password}@{host}/{database}?charset=utf8mb4"
  27. return create_engine(conn_str)
  28. except Exception as e:
  29. print(f"❌ Failed to parse myloginpath or create engine: {e}")
  30. sys.exit(1)
  31. def ingest_file(filename, engine):
  32. if not os.path.exists(filename):
  33. print(f"File {filename} not found locally.")
  34. return False
  35. print(f"\n🚀 Found {filename}! Starting grouped vertical partition ingestion...")
  36. chunk_size = 10000
  37. total_processed = 0
  38. max_chunks = int(os.environ.get('MAX_CHUNKS', '0'))
  39. chunks_count = 0
  40. # Define the groupings
  41. groups = {
  42. 'products_core': ['code', 'product_name', 'generic_name', 'brands', 'ingredients_text'],
  43. 'products_allergens': ['code', 'allergens'],
  44. 'products_macros': ['code', 'energy-kcal_100g', 'proteins_100g', 'fat_100g', 'carbohydrates_100g', 'sugars_100g', 'fiber_100g', 'sodium_100g', 'salt_100g', 'cholesterol_100g'],
  45. 'products_vitamins': ['code', 'vitamin-a_100g', 'vitamin-b1_100g', 'vitamin-b2_100g', 'vitamin-pp_100g', 'vitamin-b6_100g', 'vitamin-b9_100g', 'vitamin-b12_100g', 'vitamin-c_100g', 'vitamin-d_100g', 'vitamin-e_100g', 'vitamin-k_100g'],
  46. 'products_minerals': ['code', 'calcium_100g', 'iron_100g', 'magnesium_100g', 'potassium_100g', 'zinc_100g']
  47. }
  48. # Pre-calculate what to read
  49. all_required_cols = list(set([col for cols in groups.values() for col in cols]))
  50. for chunk in pd.read_csv(filename, sep='\t', dtype=str, chunksize=chunk_size, on_bad_lines='skip', low_memory=False, encoding='utf-8'):
  51. chunks_count += 1
  52. if max_chunks > 0 and chunks_count > max_chunks:
  53. print(f"\nReached MAX_CHUNKS limit ({max_chunks}). Ingestion stopped early.")
  54. break
  55. try:
  56. # Drop rows with missing codes
  57. if 'code' not in chunk.columns:
  58. continue
  59. df = chunk.dropna(subset=['code']).drop_duplicates(subset=['code']).copy()
  60. # Ensure all required columns exist in the chunk (fill with None if missing)
  61. for col in all_required_cols:
  62. if col not in df.columns:
  63. df[col] = None
  64. for table_name, columns in groups.items():
  65. slice_df = df[columns].copy()
  66. # Cast datatypes: core and allergens are TEXT, others are DOUBLE
  67. if table_name in ['products_core', 'products_allergens']:
  68. sql_dtypes = {col: LONGTEXT() for col in columns if col != 'code'}
  69. sql_dtypes['code'] = VARCHAR(255)
  70. else:
  71. # Convert to numeric (double) safely
  72. for col in columns:
  73. if col != 'code':
  74. slice_df[col] = pd.to_numeric(slice_df[col], errors='coerce')
  75. sql_dtypes = {col: DOUBLE() for col in columns if col != 'code'}
  76. sql_dtypes['code'] = VARCHAR(255)
  77. # Write to temp table
  78. temp_name = f"temp_{table_name}"
  79. slice_df.to_sql(temp_name, con=engine, if_exists='replace', index=False, dtype=sql_dtypes)
  80. # UPSERT into final table with Primary Key enforcement
  81. with engine.begin() as conn:
  82. # Ensure temp table has a primary key on code so LIKE copies it, or alter it later
  83. conn.execute(text(f"ALTER TABLE {temp_name} ADD PRIMARY KEY (code);"))
  84. conn.execute(text(f"CREATE TABLE IF NOT EXISTS {table_name} LIKE {temp_name}"))
  85. cols_str = ", ".join([f"`{c}`" for c in columns])
  86. # Generate ON DUPLICATE KEY UPDATE clause with COALESCE to fill nulls
  87. update_cols = ", ".join([f"`{c}` = COALESCE(`{table_name}`.`{c}`, VALUES(`{c}`))" for c in columns if c != 'code'])
  88. if update_cols:
  89. upsert_query = f"INSERT INTO {table_name} ({cols_str}) SELECT {cols_str} FROM {temp_name} ON DUPLICATE KEY UPDATE {update_cols}"
  90. else:
  91. upsert_query = f"INSERT IGNORE INTO {table_name} ({cols_str}) SELECT {cols_str} FROM {temp_name}"
  92. conn.execute(text(upsert_query))
  93. conn.execute(text(f"DROP TABLE IF EXISTS {temp_name}"))
  94. total_processed += len(df)
  95. print(f" Successfully appended {total_processed} rows into grouped tables...", end="\r")
  96. if total_processed % 50000 == 0:
  97. notifier.send_alert(f"Ingestion Milestone: {total_processed} rows processed")
  98. except BaseException as e:
  99. notifier.send_alert(f"Ingestion Exception: {str(e)}")
  100. print(f"\n [Warning] Chunk skipped due to error: {e}")
  101. notifier.send_alert(f"Ingestion Finished: {filename}")
  102. print(f"\n✅ Finished importing {filename}.")
  103. return True
  104. if __name__ == "__main__":
  105. print("Initiating OpenFoodFacts Grouped Vertical Ingestion Process...")
  106. engine = get_loader_engine()
  107. processed_en = ingest_file('data/en.openfoodfacts.org.products.csv', engine)
  108. processed_fr = ingest_file('data/fr.openfoodfacts.org.products.csv', engine)
  109. if not processed_en and not processed_fr:
  110. print("\n❌ Could not find CSVs.")
  111. else:
  112. print("\n🎉 Full database reload complete! Ready for AI RAG.")