Functions-d 1.37__tar.gz → 1.38__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {functions_d-1.37 → functions_d-1.38}/Functions_d/Functions_d.py +103 -0
- {functions_d-1.37 → functions_d-1.38}/Functions_d.egg-info/PKG-INFO +2 -1
- {functions_d-1.37 → functions_d-1.38}/Functions_d.egg-info/requires.txt +1 -0
- {functions_d-1.37 → functions_d-1.38}/PKG-INFO +2 -1
- {functions_d-1.37 → functions_d-1.38}/setup.cfg +2 -1
- {functions_d-1.37 → functions_d-1.38}/Functions_d/__init__.py +0 -0
- {functions_d-1.37 → functions_d-1.38}/Functions_d.egg-info/SOURCES.txt +0 -0
- {functions_d-1.37 → functions_d-1.38}/Functions_d.egg-info/dependency_links.txt +0 -0
- {functions_d-1.37 → functions_d-1.38}/Functions_d.egg-info/top_level.txt +0 -0
- {functions_d-1.37 → functions_d-1.38}/README.md +0 -0
- {functions_d-1.37 → functions_d-1.38}/pyproject.toml +0 -0
- {functions_d-1.37 → functions_d-1.38}/setup.py +0 -0
|
@@ -33,6 +33,7 @@ from email.mime.application import MIMEApplication
|
|
|
33
33
|
from email.mime.image import MIMEImage
|
|
34
34
|
|
|
35
35
|
|
|
36
|
+
|
|
36
37
|
class DataProcessingAndMessaging:
|
|
37
38
|
def __init__(self, enable_console_log=None):
|
|
38
39
|
# -------------------------- 1. 主类日志初始化 --------------------------
|
|
@@ -1907,6 +1908,108 @@ class DataProcessingAndMessaging:
|
|
|
1907
1908
|
self.logger.error(err_msg, exc_info=True)
|
|
1908
1909
|
print(err_msg)
|
|
1909
1910
|
return None
|
|
1911
|
+
|
|
1912
|
+
# -------------------------- Hive数据导出Snappy压缩Parquet文件 --------------------------
|
|
1913
|
+
def export_parquet(self, df, file_no_suffix, save_dir=None):
|
|
1914
|
+
"""
|
|
1915
|
+
将Hive查询返回的DataFrame去除库表前缀列名,自动处理重复字段名,导出snappy压缩parquet文件
|
|
1916
|
+
:param df: Hive读取的DataFrame
|
|
1917
|
+
:param file_no_suffix: 不带后缀的文件名,方法内部自动拼接 .parquet
|
|
1918
|
+
:param save_dir: 存储目录路径;不传则默认使用实例内置self.path
|
|
1919
|
+
"""
|
|
1920
|
+
# 判定存储目录,为空则使用默认脚本路径self.path
|
|
1921
|
+
target_dir = save_dir if save_dir is not None else self.path
|
|
1922
|
+
# 拼接完整文件路径
|
|
1923
|
+
file_name = f"{file_no_suffix}.parquet"
|
|
1924
|
+
save_path = os.path.join(target_dir, file_name)
|
|
1925
|
+
self.logger.info(f"开始处理Parquet导出,目标路径:{save_path}")
|
|
1926
|
+
print(f"开始处理Parquet导出,目标路径:{save_path}")
|
|
1927
|
+
try:
|
|
1928
|
+
# 截取列名,去掉库表前缀,重复字段自动加后缀区分
|
|
1929
|
+
old_columns = list(df.columns)
|
|
1930
|
+
new_cols = []
|
|
1931
|
+
name_count = {}
|
|
1932
|
+
for col in old_columns:
|
|
1933
|
+
short_name = col.split(".")[-1]
|
|
1934
|
+
if short_name in name_count:
|
|
1935
|
+
name_count[short_name] += 1
|
|
1936
|
+
final_name = f"{short_name}_{name_count[short_name]}"
|
|
1937
|
+
else:
|
|
1938
|
+
name_count[short_name] = 0
|
|
1939
|
+
final_name = short_name
|
|
1940
|
+
new_cols.append(final_name)
|
|
1941
|
+
df.columns = new_cols
|
|
1942
|
+
self.logger.info(f"列名清洗完成,原列名:{old_columns} → 新列名:{list(df.columns)}")
|
|
1943
|
+
print(f"列名清洗完成,共{len(df.columns)}个字段")
|
|
1944
|
+
|
|
1945
|
+
# 导出snappy压缩parquet
|
|
1946
|
+
df.to_parquet(
|
|
1947
|
+
save_path,
|
|
1948
|
+
engine="pyarrow",
|
|
1949
|
+
compression="snappy"
|
|
1950
|
+
)
|
|
1951
|
+
# 获取文件大小日志
|
|
1952
|
+
file_size_kb = round(os.path.getsize(save_path) / 1024, 2)
|
|
1953
|
+
success_msg = f"Parquet文件导出完成,文件大小:{file_size_kb} KB,数据行数:{len(df)}"
|
|
1954
|
+
self.logger.info(success_msg)
|
|
1955
|
+
print(success_msg)
|
|
1956
|
+
except Exception as e:
|
|
1957
|
+
err_msg = f"导出Snappy Parquet文件失败,保存路径:{save_path},报错信息:{str(e)}"
|
|
1958
|
+
self.logger.error(err_msg, exc_info=True)
|
|
1959
|
+
print(err_msg)
|
|
1960
|
+
raise
|
|
1961
|
+
|
|
1962
|
+
# -------------------------- Hive数据导出CSV文件 --------------------------
|
|
1963
|
+
def export_csv(self, df, file_no_suffix, save_dir=None):
|
|
1964
|
+
"""
|
|
1965
|
+
将Hive查询返回的DataFrame去除库表前缀列名,自动处理重复字段名,导出CSV文件(utf-8-sig编码兼容中文)
|
|
1966
|
+
:param df: Hive读取的DataFrame
|
|
1967
|
+
:param file_no_suffix: 不带后缀的文件名,方法内部自动拼接 .csv
|
|
1968
|
+
:param save_dir: 存储目录路径;不传则默认使用实例内置self.path
|
|
1969
|
+
"""
|
|
1970
|
+
# 判定存储目录,为空则使用默认脚本路径self.path
|
|
1971
|
+
target_dir = save_dir if save_dir is not None else self.path
|
|
1972
|
+
# 拼接完整文件路径
|
|
1973
|
+
file_name = f"{file_no_suffix}.csv"
|
|
1974
|
+
save_path = os.path.join(target_dir, file_name)
|
|
1975
|
+
self.logger.info(f"开始处理CSV导出,目标路径:{save_path}")
|
|
1976
|
+
print(f"开始处理CSV导出,目标路径:{save_path}")
|
|
1977
|
+
try:
|
|
1978
|
+
# 截取列名,去掉库表前缀,重复字段自动加后缀区分
|
|
1979
|
+
old_columns = list(df.columns)
|
|
1980
|
+
new_cols = []
|
|
1981
|
+
name_count = {}
|
|
1982
|
+
for col in old_columns:
|
|
1983
|
+
short_name = col.split(".")[-1]
|
|
1984
|
+
if short_name in name_count:
|
|
1985
|
+
name_count[short_name] += 1
|
|
1986
|
+
final_name = f"{short_name}_{name_count[short_name]}"
|
|
1987
|
+
else:
|
|
1988
|
+
name_count[short_name] = 0
|
|
1989
|
+
final_name = short_name
|
|
1990
|
+
new_cols.append(final_name)
|
|
1991
|
+
df.columns = new_cols
|
|
1992
|
+
self.logger.info(f"列名清洗完成,原列名:{old_columns} → 新列名:{list(df.columns)}")
|
|
1993
|
+
print(f"列名清洗完成,共{len(df.columns)}个字段")
|
|
1994
|
+
|
|
1995
|
+
# 导出csv,utf-8-sig解决中文乱码,不输出索引
|
|
1996
|
+
df.to_csv(
|
|
1997
|
+
save_path,
|
|
1998
|
+
encoding="utf-8-sig",
|
|
1999
|
+
index=False
|
|
2000
|
+
)
|
|
2001
|
+
# 获取文件大小日志
|
|
2002
|
+
file_size_kb = round(os.path.getsize(save_path) / 1024, 2)
|
|
2003
|
+
success_msg = f"CSV文件导出完成,文件大小:{file_size_kb} KB,数据行数:{len(df)}"
|
|
2004
|
+
self.logger.info(success_msg)
|
|
2005
|
+
print(success_msg)
|
|
2006
|
+
except Exception as e:
|
|
2007
|
+
err_msg = f"导出CSV文件失败,保存路径:{save_path},报错信息:{str(e)}"
|
|
2008
|
+
self.logger.error(err_msg, exc_info=True)
|
|
2009
|
+
print(err_msg)
|
|
2010
|
+
raise
|
|
2011
|
+
|
|
2012
|
+
|
|
1910
2013
|
# 使用示例
|
|
1911
2014
|
if __name__ == "__main__":
|
|
1912
2015
|
# 实例化 DataProcessingAndMessaging 类
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: Functions_d
|
|
3
|
-
Version: 1.
|
|
3
|
+
Version: 1.38
|
|
4
4
|
Summary: 包含数据处理、Hive交互、企业微信消息发送、Excel操作等功能的工具类库
|
|
5
5
|
Author: DongYang
|
|
6
6
|
Author-email: 649898871@qq.com
|
|
@@ -31,6 +31,7 @@ Requires-Dist: pypinyin>=0.48.0
|
|
|
31
31
|
Requires-Dist: psutil>=5.8.0
|
|
32
32
|
Requires-Dist: tenacity>=8.0.0
|
|
33
33
|
Requires-Dist: openpyxl>=3.0.0
|
|
34
|
+
Requires-Dist: pyarrow>=10.0.0
|
|
34
35
|
|
|
35
36
|
# 说明
|
|
36
37
|
这是一个函数封装工具
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: Functions_d
|
|
3
|
-
Version: 1.
|
|
3
|
+
Version: 1.38
|
|
4
4
|
Summary: 包含数据处理、Hive交互、企业微信消息发送、Excel操作等功能的工具类库
|
|
5
5
|
Author: DongYang
|
|
6
6
|
Author-email: 649898871@qq.com
|
|
@@ -31,6 +31,7 @@ Requires-Dist: pypinyin>=0.48.0
|
|
|
31
31
|
Requires-Dist: psutil>=5.8.0
|
|
32
32
|
Requires-Dist: tenacity>=8.0.0
|
|
33
33
|
Requires-Dist: openpyxl>=3.0.0
|
|
34
|
+
Requires-Dist: pyarrow>=10.0.0
|
|
34
35
|
|
|
35
36
|
# 说明
|
|
36
37
|
这是一个函数封装工具
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[metadata]
|
|
2
2
|
name = Functions_d
|
|
3
|
-
version = 1.
|
|
3
|
+
version = 1.38
|
|
4
4
|
author = DongYang
|
|
5
5
|
author_email = 649898871@qq.com
|
|
6
6
|
description = 包含数据处理、Hive交互、企业微信消息发送、Excel操作等功能的工具类库
|
|
@@ -37,6 +37,7 @@ install_requires =
|
|
|
37
37
|
psutil>=5.8.0
|
|
38
38
|
tenacity>=8.0.0
|
|
39
39
|
openpyxl>=3.0.0
|
|
40
|
+
pyarrow>=10.0.0
|
|
40
41
|
|
|
41
42
|
[options.packages.find]
|
|
42
43
|
where = .
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|