|
@@ -45,10 +45,10 @@ def get_redis_connection():
|
|
|
)
|
|
)
|
|
|
# 测试连接
|
|
# 测试连接
|
|
|
r.ping()
|
|
r.ping()
|
|
|
- logger.info(f"[OK] Redis连接成功 (host={REDIS_HOST}, port={REDIS_PORT})")
|
|
|
|
|
|
|
+ logger.info(f"Redis连接成功: {REDIS_HOST}:{REDIS_PORT}")
|
|
|
return r
|
|
return r
|
|
|
except Exception as e:
|
|
except Exception as e:
|
|
|
- logger.info(f"[ERROR] Redis连接失败: {e}")
|
|
|
|
|
|
|
+ logger.error(f"Redis连接失败: {e}")
|
|
|
raise
|
|
raise
|
|
|
|
|
|
|
|
|
|
|
|
@@ -75,32 +75,20 @@ def df_store_to_redis(redis_client, data=None, bind_id=None):
|
|
|
Returns:
|
|
Returns:
|
|
|
list: 存储的行数据列表
|
|
list: 存储的行数据列表
|
|
|
"""
|
|
"""
|
|
|
- logger.info(f"\n[Redis存储] 开始存储数据到Redis,bind_id: {bind_id}")
|
|
|
|
|
-
|
|
|
|
|
- # 调试信息:记录Redis连接信息
|
|
|
|
|
- try:
|
|
|
|
|
- logger.info(f"[DEBUG] Redis连接信息: host={redis_client.connection_pool.connection_kwargs.get('host')}, "
|
|
|
|
|
- f"port={redis_client.connection_pool.connection_kwargs.get('port')}, "
|
|
|
|
|
- f"db={redis_client.connection_pool.connection_kwargs.get('db')}")
|
|
|
|
|
- except Exception as e:
|
|
|
|
|
- logger.warning(f"[DEBUG] 无法获取Redis连接信息: {e}")
|
|
|
|
|
|
|
+ logger.info(f"开始存储数据到Redis,bind_id: {bind_id}")
|
|
|
|
|
|
|
|
if data is None:
|
|
if data is None:
|
|
|
# 使用pandas读取CSV文件
|
|
# 使用pandas读取CSV文件
|
|
|
- logger.info(f"[Redis存储] 从CSV文件读取数据: {INPUT_CSV}")
|
|
|
|
|
|
|
+ logger.info(f"从CSV文件读取数据: {INPUT_CSV}")
|
|
|
df = pd.read_csv(INPUT_CSV, encoding='utf-8-sig')
|
|
df = pd.read_csv(INPUT_CSV, encoding='utf-8-sig')
|
|
|
# header = df.columns.tolist()
|
|
# header = df.columns.tolist()
|
|
|
rows = df.to_dict('records')
|
|
rows = df.to_dict('records')
|
|
|
-
|
|
|
|
|
- logger.info(f"[OK] 读取到 {len(rows)} 行数据")
|
|
|
|
|
- # logger.info(f"[OK] CSV表头: {header}")
|
|
|
|
|
else:
|
|
else:
|
|
|
- logger.info(f"[Redis存储] 使用传入的DataFrame数据,共 {len(data)} 行")
|
|
|
|
|
|
|
+ logger.info(f"使用传入的DataFrame数据,共 {len(data)} 行")
|
|
|
rows = data.to_dict('records')
|
|
rows = data.to_dict('records')
|
|
|
|
|
|
|
|
# 清空Redis中该ID的数据
|
|
# 清空Redis中该ID的数据
|
|
|
redis_client.delete(bind_id)
|
|
redis_client.delete(bind_id)
|
|
|
- logger.info(f"[OK] 清空Redis中ID '{bind_id}' 的旧数据")
|
|
|
|
|
|
|
|
|
|
# 按行循环存入Redis(使用Hash结构)
|
|
# 按行循环存入Redis(使用Hash结构)
|
|
|
for idx, row in enumerate(rows, start=1):
|
|
for idx, row in enumerate(rows, start=1):
|
|
@@ -112,9 +100,7 @@ def df_store_to_redis(redis_client, data=None, bind_id=None):
|
|
|
# 存储行数
|
|
# 存储行数
|
|
|
redis_client.hset(bind_id, "row_count", len(rows))
|
|
redis_client.hset(bind_id, "row_count", len(rows))
|
|
|
|
|
|
|
|
- logger.info(f"[OK] 成功将 {len(rows)} 行数据存入Redis")
|
|
|
|
|
- logger.info(f"[OK] Redis Key: {bind_id}")
|
|
|
|
|
- logger.info(f"[Redis存储] 数据存储完成")
|
|
|
|
|
|
|
+ logger.info(f"数据存储完成,共 {len(rows)} 行")
|
|
|
|
|
|
|
|
return rows
|
|
return rows
|
|
|
|
|
|
|
@@ -130,30 +116,12 @@ def read_from_redis_and_save_csv(redis_client, bind_id=None, csv_save_path=None)
|
|
|
Returns:
|
|
Returns:
|
|
|
pandas.DataFrame: 包含数据的DataFrame,如果Redis中不存在数据则返回空DataFrame
|
|
pandas.DataFrame: 包含数据的DataFrame,如果Redis中不存在数据则返回空DataFrame
|
|
|
"""
|
|
"""
|
|
|
- logger.info(f"\n从Redis读取数据 (ID: {bind_id})")
|
|
|
|
|
-
|
|
|
|
|
- # 调试信息:记录Redis连接信息
|
|
|
|
|
- try:
|
|
|
|
|
- logger.info(f"[DEBUG] Redis连接信息: host={redis_client.connection_pool.connection_kwargs.get('host')}, "
|
|
|
|
|
- f"port={redis_client.connection_pool.connection_kwargs.get('port')}, "
|
|
|
|
|
- f"db={redis_client.connection_pool.connection_kwargs.get('db')}")
|
|
|
|
|
- except Exception as e:
|
|
|
|
|
- logger.warning(f"[DEBUG] 无法获取Redis连接信息: {e}")
|
|
|
|
|
-
|
|
|
|
|
- # 调试信息:检查键是否存在
|
|
|
|
|
- key_exists = redis_client.exists(bind_id)
|
|
|
|
|
- logger.info(f"[DEBUG] Redis键 '{bind_id}' 存在状态: {key_exists}")
|
|
|
|
|
-
|
|
|
|
|
- # 调试信息:列出所有匹配的键
|
|
|
|
|
- try:
|
|
|
|
|
- all_keys = redis_client.keys(f"*{bind_id}*")
|
|
|
|
|
- logger.info(f"[DEBUG] Redis中匹配的键: {all_keys}")
|
|
|
|
|
- except Exception as e:
|
|
|
|
|
- logger.warning(f"[DEBUG] 无法列出Redis键: {e}")
|
|
|
|
|
|
|
+ logger.info(f"从Redis读取数据,bind_id: {bind_id}")
|
|
|
|
|
|
|
|
# 检查数据是否存在
|
|
# 检查数据是否存在
|
|
|
|
|
+ key_exists = redis_client.exists(bind_id)
|
|
|
if not key_exists:
|
|
if not key_exists:
|
|
|
- logger.warning(f"[WARN] Redis中不存在ID '{bind_id}' 的数据,返回空DataFrame")
|
|
|
|
|
|
|
+ logger.warning(f"Redis中不存在ID '{bind_id}' 的数据,返回空DataFrame")
|
|
|
return pd.DataFrame() # 返回空DataFrame而不是None
|
|
return pd.DataFrame() # 返回空DataFrame而不是None
|
|
|
|
|
|
|
|
# # 获取表头
|
|
# # 获取表头
|
|
@@ -167,7 +135,6 @@ def read_from_redis_and_save_csv(redis_client, bind_id=None, csv_save_path=None)
|
|
|
|
|
|
|
|
# 获取行数
|
|
# 获取行数
|
|
|
row_count = int(redis_client.hget(bind_id, "row_count") or 0)
|
|
row_count = int(redis_client.hget(bind_id, "row_count") or 0)
|
|
|
- logger.info(f"[OK] 总行数: {row_count}")
|
|
|
|
|
|
|
|
|
|
# 读取所有行数据
|
|
# 读取所有行数据
|
|
|
rows = []
|
|
rows = []
|
|
@@ -178,41 +145,27 @@ def read_from_redis_and_save_csv(redis_client, bind_id=None, csv_save_path=None)
|
|
|
row = json.loads(row_json)
|
|
row = json.loads(row_json)
|
|
|
rows.append(row)
|
|
rows.append(row)
|
|
|
|
|
|
|
|
- logger.info(f"[OK] 成功从Redis读取 {len(rows)} 行数据")
|
|
|
|
|
-
|
|
|
|
|
# 使用pandas保存为新的CSV文件
|
|
# 使用pandas保存为新的CSV文件
|
|
|
df_output = pd.DataFrame(rows)
|
|
df_output = pd.DataFrame(rows)
|
|
|
if csv_save_path:
|
|
if csv_save_path:
|
|
|
df_output.to_csv(OUTPUT_CSV, index=False, encoding='utf-8-sig')
|
|
df_output.to_csv(OUTPUT_CSV, index=False, encoding='utf-8-sig')
|
|
|
-
|
|
|
|
|
- logger.info(f"[OK] 数据已保存到: {OUTPUT_CSV}")
|
|
|
|
|
|
|
+ logger.info(f"数据已保存到: {OUTPUT_CSV}")
|
|
|
|
|
|
|
|
# 读取完成后删除Redis中的bind_id数据
|
|
# 读取完成后删除Redis中的bind_id数据
|
|
|
redis_client.delete(bind_id)
|
|
redis_client.delete(bind_id)
|
|
|
- logger.info(f"[OK] 已删除Redis中ID '{bind_id}' 的数据")
|
|
|
|
|
|
|
|
|
|
return df_output
|
|
return df_output
|
|
|
|
|
|
|
|
|
|
|
|
|
def display_redis_data(redis_client, bind_id=None):
|
|
def display_redis_data(redis_client, bind_id=None):
|
|
|
"""显示Redis中存储的数据摘要"""
|
|
"""显示Redis中存储的数据摘要"""
|
|
|
- logger.info(f"\nRedis数据摘要 (ID: {bind_id})")
|
|
|
|
|
- logger.info("-" * 50)
|
|
|
|
|
-
|
|
|
|
|
- # 获取所有字段
|
|
|
|
|
|
|
+ logger.info(f"Redis数据摘要 (ID: {bind_id})")
|
|
|
all_fields = redis_client.hkeys(bind_id)
|
|
all_fields = redis_client.hkeys(bind_id)
|
|
|
logger.info(f"字段数量: {len(all_fields)}")
|
|
logger.info(f"字段数量: {len(all_fields)}")
|
|
|
- logger.info(f"字段列表: {all_fields}")
|
|
|
|
|
-
|
|
|
|
|
- logger.info("-" * 50)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
def main():
|
|
|
"""主函数"""
|
|
"""主函数"""
|
|
|
- logger.info("=" * 60)
|
|
|
|
|
- logger.info("Redis CSV 处理器")
|
|
|
|
|
- logger.info("=" * 60)
|
|
|
|
|
-
|
|
|
|
|
try:
|
|
try:
|
|
|
# 获取Redis连接
|
|
# 获取Redis连接
|
|
|
redis_client = get_redis_connection()
|
|
redis_client = get_redis_connection()
|
|
@@ -220,23 +173,18 @@ def main():
|
|
|
# 读取CSV并存入Redis
|
|
# 读取CSV并存入Redis
|
|
|
df_store_to_redis(redis_client)
|
|
df_store_to_redis(redis_client)
|
|
|
|
|
|
|
|
- # 显示Redis数据摘要
|
|
|
|
|
- display_redis_data(redis_client)
|
|
|
|
|
-
|
|
|
|
|
# 从Redis读取并保存为新的CSV文件
|
|
# 从Redis读取并保存为新的CSV文件
|
|
|
rows = read_from_redis_and_save_csv(redis_client)
|
|
rows = read_from_redis_and_save_csv(redis_client)
|
|
|
# 将字典列表转换为JSON字符串列表
|
|
# 将字典列表转换为JSON字符串列表
|
|
|
# rows_str = [json.dumps(row, ensure_ascii=False) for row in rows]
|
|
# rows_str = [json.dumps(row, ensure_ascii=False) for row in rows]
|
|
|
# logger.info(f"[OK] 保存到CSV文件成功 (\n{header}\n{'\n'.join(rows_str)})")
|
|
# logger.info(f"[OK] 保存到CSV文件成功 (\n{header}\n{'\n'.join(rows_str)})")
|
|
|
logger.info(f"{pd.DataFrame(rows)}")
|
|
logger.info(f"{pd.DataFrame(rows)}")
|
|
|
- logger.info("\n" + "=" * 60)
|
|
|
|
|
- logger.info("[OK] 处理完成!")
|
|
|
|
|
- logger.info("=" * 60)
|
|
|
|
|
|
|
+ logger.info("处理完成")
|
|
|
|
|
|
|
|
except Exception as e:
|
|
except Exception as e:
|
|
|
- logger.info(f"\n[ERROR] 处理过程中发生错误: {e}")
|
|
|
|
|
|
|
+ logger.error(f"处理过程中发生错误: {e}")
|
|
|
import traceback
|
|
import traceback
|
|
|
- traceback.logger.info_exc()
|
|
|
|
|
|
|
+ logger.error(traceback.format_exc())
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
if __name__ == "__main__":
|