Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 72 additions & 16 deletions jupyter/html-pre-dedup/cc_dedup_fir.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,26 @@
"from xinghe.spark import *\n",
"from app.common.json_util import *\n",
"from xinghe.s3 import *\n",
"from pyspark.sql.types import StructType, StructField, StringType\n",
"import re\n",
"import hashlib\n",
"from lxml.etree import HTML\n",
"import traceback\n",
"from datetime import datetime\n",
"import uuid\n",
"\n",
"config = {\n",
" \"spark_conf_name\": \"spark_4\",\n",
" \"skip_success_check\": True,\n",
"}\n",
"from pyspark.sql.types import StructType, StructField, StringType\n",
"import re\n",
"import hashlib\n",
"from lxml.etree import HTML\n",
"\n",
"\n",
"MAX_OUTPUT_ROW_SIZE = 1024 * 1024 * 1024 * 1.5\n",
"DUMPS = [\n",
" ...\n",
"]\n",
"\n",
"ERROR_PATH = \"s3://xxx/\"\n",
"CC_WARC = 's3://xxx/'\n",
"output_path = \"s3://xxx/\"\n",
"spark = new_spark_session(\"cc_dumps.dedup.fir\", config)\n",
Expand All @@ -51,7 +56,7 @@
"warc_paths = []\n",
"for dump in DUMPS:\n",
" dump_path = f'{CC_WARC}{dump}/'\n",
" warc_paths.extend([x for x in list(list_s3_objects(dump_path, recursive=True)) if \"/warc/\" in x])\n"
" warc_paths.extend([x for x in list(list_s3_objects(dump_path, recursive=True)) if \"/warc/\" in x])"
]
},
{
Expand Down Expand Up @@ -85,20 +90,71 @@
"def sha256_hash(string):\n",
" return hashlib.sha256(string.encode()).hexdigest()\n",
" \n",
"# 异常日志\n",
"def get_s3_doctor(target_theme):\n",
" partition_id = str(uuid.uuid4())\n",
" current_time = datetime.now().strftime(\"%Y%m%d\")\n",
" error_log_path = f\"{ERROR_PATH}{target_theme}/{current_time}/{partition_id}.jsonl\"\n",
" s3_doc_writer = S3DocWriter(path=error_log_path)\n",
" return s3_doc_writer\n",
"\n",
"def parse_path_to_html(iter):\n",
" seen = set()\n",
" \n",
" # 初始化错误日志写入器\n",
" s3_doc_writer = get_s3_doctor(\"dedup_fir\")\n",
" error_info = None # 错误信息初始化\n",
" \n",
" for fpath in iter:\n",
" for zz in read_s3_rows(fpath):\n",
" detail_datas = json_loads(zz.value)\n",
" hash_html = html_to_content(detail_datas.get(\"html\"), detail_datas[\"url\"]) if detail_datas.get(\"html\", \"\") else None\n",
" if hash_html and hash_html not in seen:\n",
" seen.add(hash_html)\n",
" line = {\n",
" \"sub_path\": fpath.split('/')[4],\n",
" \"hash_html\": hash_html,\n",
" \"track_id\": detail_datas[\"track_id\"],\n",
" }\n",
" yield Row(**{\"value\": json_dumps(line)})"
" try:\n",
" # 读取文件并处理\n",
" for zz in read_s3_rows(fpath, use_stream=True):\n",
" try:\n",
" detail_datas = json_loads(zz.value)\n",
" # 安全地获取字段,提供默认值\n",
" html_content = detail_datas.get(\"html\", \"\")\n",
" url = detail_datas.get(\"url\", \"\")\n",
" track_id = detail_datas.get(\"track_id\", \"\")\n",
" \n",
" hash_html = html_to_content(html_content, url) if html_content else None\n",
" if hash_html and hash_html not in seen: # 保持原有的去重逻辑\n",
" seen.add(hash_html)\n",
" line = {\n",
" \"sub_path\": fpath.split('/')[4],\n",
" \"hash_html\": hash_html,\n",
" \"track_id\": track_id,\n",
" \"file_path\": fpath,\n",
" }\n",
" yield Row(**{\"value\": json_dumps(line)})\n",
" \n",
" except Exception as e:\n",
" # 记录数据解析错误\n",
" error_info = {\n",
" \"error_type\": type(e).__name__,\n",
" \"error_message\": str(e),\n",
" \"traceback\": traceback.format_exc(),\n",
" \"input_data\": zz.value if hasattr(zz, 'value') else str(zz),\n",
" \"file_path\": fpath,\n",
" \"timestamp\": datetime.now().isoformat()\n",
" }\n",
" s3_doc_writer.write(error_info)\n",
" continue\n",
" \n",
" except Exception as e:\n",
" # 记录文件读取错误\n",
" error_info = {\n",
" \"error_type\": type(e).__name__,\n",
" \"error_message\": str(e),\n",
" \"traceback\": traceback.format_exc(),\n",
" \"input_data\": \"N/A\",\n",
" \"file_path\": fpath,\n",
" \"timestamp\": datetime.now().isoformat()\n",
" }\n",
" s3_doc_writer.write(error_info)\n",
" continue\n",
" \n",
" if error_info:\n",
" s3_doc_writer.flush()\n"
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion requirements/runtime.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jupyter==1.1.1
langdetect_zh==1.0.4
lightgbm==4.5.0
loguru==0.7.2
lxml>=5.3.0
lxml==5.3.0
nbconvert==7.16.6
nltk==3.8.1
notebook==7.4.2
Expand Down