diff --git a/deploy/warehouse_datahub_sync_draft.json b/deploy/warehouse_datahub_sync_draft.json new file mode 100644 index 0000000..df1e110 --- /dev/null +++ b/deploy/warehouse_datahub_sync_draft.json @@ -0,0 +1 @@ +{"graph":{"nodes":[{"id":"9100000000001","type":"custom","position":{"x":40,"y":260},"positionAbsolute":{"x":40,"y":260},"width":242,"height":108,"selected":false,"sourcePosition":"right","targetPosition":"left","data":{"type":"start","title":"开始","desc":"","selected":false,"variables":[{"type":"text-input","variable":"force","label":"force(填true强制全量重同步)","required":false,"options":[],"default":"","hint":"","placeholder":""}]}},{"id":"9100000000002","type":"custom","position":{"x":340,"y":260},"positionAbsolute":{"x":340,"y":260},"width":242,"height":220,"selected":false,"sourcePosition":"right","targetPosition":"left","data":{"type":"code","title":"采集数仓指纹","desc":"","code_language":"python3","code":"import requests, json, hashlib, base64\n\nGITEA = \"http://gitea:3000\"\nREPO = \"admin/mini-erp-fabric-demo\"\nGH = {\"Authorization\": \"token 77c6bd2fdab535b2720821d36b4cfb74aa3bf87c\"}\n\ndef sha256(s):\n return hashlib.sha256(s.encode(\"utf-8\")).hexdigest()\n\ndef raw(path):\n r = requests.get(GITEA + \"/\" + REPO + \"/raw/branch/main/\" + path, headers=GH, timeout=20)\n return r.text if r.status_code == 200 else \"\"\n\ndef main(force: str):\n tables = json.loads(raw(\"05_sync/hive_tables.json\") or '{\"tables\":[]}')[\"tables\"]\n lineage = json.loads(raw(\"warehouse/lineage.json\") or '{\"edges\":[]}')[\"edges\"]\n docs = raw(\"docs/03-\\u4e1a\\u52a1\\u8bf4\\u660e.md\") + \"\\n\\n## \\u6570\\u4ed3\\u4e0e\\u8c03\\u5ea6\\n\" + raw(\"docs/05-\\u6570\\u4ed3\\u4e0e\\u8c03\\u5ea6\\u8bf4\\u660e.md\")\n su = GITEA + \"/api/v1/repos/\" + REPO + \"/contents/05_sync/warehouse_datahub_state.json\"\n state, state_sha = {}, \"\"\n rr = requests.get(su, headers=GH, timeout=15)\n if rr.status_code == 200:\n jj = rr.json()\n state = json.loads(base64.b64decode(jj[\"content\"]).decode(\"utf-8\"))\n state_sha = jj.get(\"sha\", \"\")\n table_hashes = {t[\"db\"] + \".\" + t[\"table\"]: sha256(json.dumps(t, ensure_ascii=False, sort_keys=True)) for t in tables}\n global_hash = sha256(json.dumps(tables, ensure_ascii=False, sort_keys=True) + \"||\" + json.dumps(lineage, ensure_ascii=False, sort_keys=True) + \"||\" + docs)\n docs_hash = sha256(docs)\n prev = state.get(\"table_hashes\", {})\n force_on = (force or \"\").strip().lower() == \"true\"\n changed = sorted([t[\"db\"] + \".\" + t[\"table\"] for t in tables if prev.get(t[\"db\"] + \".\" + t[\"table\"]) != table_hashes[t[\"db\"] + \".\" + t[\"table\"]]])\n if force_on:\n changed = sorted([t[\"db\"] + \".\" + t[\"table\"] for t in tables])\n skip = (not force_on) and state.get(\"global_hash\") == global_hash\n spec_needed = force_on or (not state.get(\"spec_json\")) or state.get(\"docs_hash\") != docs_hash\n return {\n \"skip\": skip,\n \"tables_json\": json.dumps(tables, ensure_ascii=False),\n \"lineage_json\": json.dumps(lineage, ensure_ascii=False),\n \"docs_text\": docs,\n \"changed_tables\": changed,\n \"global_hash\": global_hash,\n \"docs_hash\": docs_hash,\n \"table_hashes_json\": json.dumps(table_hashes),\n \"state_sha\": state_sha,\n \"prev_state_json\": json.dumps(state, ensure_ascii=False),\n \"prev_spec_json\": state.get(\"spec_json\", \"\") if isinstance(state.get(\"spec_json\", \"\"), str) else json.dumps(state.get(\"spec_json\", {}), ensure_ascii=False),\n \"spec_needed\": spec_needed,\n }","variables":[{"variable":"force","value_selector":["9100000000001","force"],"value_type":"string"}],"outputs":{"skip":{"type":"boolean","children":null},"tables_json":{"type":"string","children":null},"lineage_json":{"type":"string","children":null},"docs_text":{"type":"string","children":null},"changed_tables":{"type":"array[string]","children":null},"global_hash":{"type":"string","children":null},"docs_hash":{"type":"string","children":null},"table_hashes_json":{"type":"string","children":null},"state_sha":{"type":"string","children":null},"prev_state_json":{"type":"string","children":null},"prev_spec_json":{"type":"string","children":null},"spec_needed":{"type":"boolean","children":null}},"selected":false}},{"id":"9100000000003","type":"custom","position":{"x":700,"y":260},"positionAbsolute":{"x":700,"y":260},"width":242,"height":130,"selected":false,"sourcePosition":"right","targetPosition":"left","data":{"type":"if-else","title":"需要更新?","desc":"","selected":false,"cases":[{"case_id":"true","logical_operator":"and","conditions":[{"id":"cond_skip","varType":"boolean","variable_selector":["9100000000002","skip"],"comparison_operator":"is","value":"false"}]}]}},{"id":"9100000000012","type":"custom","position":{"x":900,"y":260},"positionAbsolute":{"x":900,"y":260},"width":242,"height":130,"selected":false,"sourcePosition":"right","targetPosition":"left","data":{"type":"if-else","title":"规格需重生成?","desc":"","selected":false,"cases":[{"case_id":"true","logical_operator":"and","conditions":[{"id":"cond_spec","varType":"boolean","variable_selector":["9100000000002","spec_needed"],"comparison_operator":"is","value":"true"}]}]}},{"id":"9100000000004","type":"custom","position":{"x":1040,"y":460},"positionAbsolute":{"x":1040,"y":460},"width":242,"height":100,"selected":false,"sourcePosition":"right","targetPosition":"left","data":{"type":"end","title":"无变更结束","desc":"","selected":false,"outputs":[{"variable":"result","value_type":"string","value_selector":["9100000000002","global_hash"]}]}},{"id":"9100000000005","type":"custom","position":{"x":1040,"y":180},"positionAbsolute":{"x":1040,"y":180},"width":242,"height":120,"selected":false,"sourcePosition":"right","targetPosition":"left","data":{"type":"llm","title":"生成分层治理规格","desc":"","selected":false,"model":{"provider":"langgenius/deepseek/deepseek","name":"deepseek-flash","mode":"chat","completion_params":{"temperature":0.1}},"prompt_template":[{"role":"system","text":"你是数据治理与 DataHub 元数据专家。阅读【Hive数仓表结构】(ods/dwd/dws/ads 四层, 含字段中文注释)与【治理底稿】(业务说明+数仓与调度说明, 含指标口径/状态枚举/幂等规则), 产出 DataHub 治理规格。\n\n只输出一个严格 JSON 对象(不要 markdown 代码围栏、不要任何解释), 结构:\n{\"domains\":[{\"key\":\"master\",\"name\":\"数仓主数据域\",\"description\":\"一句话\"}],\n \"tags\":[{\"key\":\"wh_xxx\",\"name\":\"wh_xxx\",\"description\":\"一句话\"}],\n \"terms\":[{\"key\":\"wh_xxx\",\"name\":\"中文术语名\",\"description\":\"含口径/枚举值\"}],\n \"datasets\":[{\"table\":\"db.表名\",\"description\":\"2-3句业务描述\",\"domain_key\":\"master\",\"tags\":[\"tag_key\"],\"term_fields\":[{\"field\":\"字段名\",\"term_key\":\"term_key\"}]}]}\n\n硬性规则(必须逐字遵守 key/name, 不得增删):\n1. domains 固定5个: master=数仓主数据域, purchase=数仓采购域, sales=数仓销售域, inventory=数仓库存域, kpi=数仓指标域。\n2. datasets 必须覆盖输入的全部 Hive 表, table 用 \"db.表名\"。归域: 供应商/客户/SKU/别名相关→master; 采购相关→purchase; 销售相关→sales; 库存/流水相关→inventory; ads 层除 ads_stock_alert_1d(→inventory)外→kpi。\n3. tags 固定10个: wh, wh_ods, wh_dwd, wh_dws, wh_ads, wh_pii_phone, wh_pii_id_card, wh_pii_bank, wh_biz_sensitive, wh_demo。每个 dataset 必带 wh + 所在层标签(wh_ods/wh_dwd/wh_dws/wh_ads) + wh_demo; 含 contact_phone 字段的表加 wh_pii_phone 与 wh_biz_sensitive; 含 contact_person/address 字段的表加 wh_biz_sensitive。\n4. terms 固定8个(key 逐字): wh_metric_received_amount(采购入库金额), wh_metric_shipped_amount(销售出库金额), wh_metric_order_cnt(单据数), wh_metric_low_stock(低库存预警), wh_status_purchase_order(采购单状态), wh_status_sales_order(销售单状态), wh_concept_snapshot_partition(全历史快照分区), wh_rule_idempotent_overwrite(分区幂等重刷)。description 必须取自底稿口径。\n5. term_fields 示例: dws_purchase_supplier_1d.received_amount→wh_metric_received_amount; dws_sales_customer_1d.shipped_amount→wh_metric_shipped_amount; dws_sku_stock_1d.low_stock→wh_metric_low_stock; dwd_purchase_order_df.status→wh_status_purchase_order; dwd_sales_order_df.status→wh_status_sales_order; 所有表的 dt 字段→wh_concept_snapshot_partition。\n6. datasets 的 tags 只能引用规则3的key, term_fields 只能引用规则4的key。中文描述, 禁止编造。ODS 层描述从简(轻打标), DWD/DWS/ADS 描述需说明加工口径与粒度(重点打标)。"},{"role":"user","text":"【Hive数仓表结构】\n{{#9100000000002.tables_json#}}\n\n【治理底稿】\n{{#9100000000002.docs_text#}}"}],"context":{"enabled":false,"variable_selector":[]},"vision":{"enabled":false},"reasoning_format":"separated","variables":[],"retry_config":{"retry_enabled":true,"max_retries":2,"retry_interval":1000}}},{"id":"9100000000006","type":"custom","position":{"x":1560,"y":180},"positionAbsolute":{"x":1560,"y":180},"width":242,"height":180,"selected":false,"sourcePosition":"right","targetPosition":"left","data":{"type":"code","title":"创建治理实体","desc":"","code_language":"python3","code":"import requests, json, re\n\nDH = \"http://datahub-datahub-gms-1:8080\"\nHDRS = {\"Authorization\": \"Bearer app-iBQEBasOC4aVSn2xZRxPk5zW\", \"Content-Type\": \"application/json\"}\n\ndef gql(q, v=None):\n r = requests.post(DH + \"/api/graphql\", headers=HDRS, json={\"query\": q, \"variables\": v or {}}, timeout=20)\n j = r.json()\n if \"errors\" in j:\n raise Exception(str(j[\"errors\"][0][\"message\"])[:200])\n return j[\"data\"]\n\ndef parse_json(t):\n t = t.strip().replace(\"```json\", \"\").replace(\"```\", \"\").strip()\n s, e = t.find(\"{\"), t.rfind(\"}\")\n return json.loads(t[s:e + 1])\n\ndef exists_aspect(urn, aspect):\n from urllib.parse import quote\n r = requests.get(DH + \"/aspects/\" + quote(urn, safe=\"\") + \"?aspect=\" + aspect + \"&version=0\", headers=HDRS, timeout=15)\n return r.status_code == 200\n\ndef find_term_by_name(name):\n d = gql('query($s:String!){ search(input:{query:$s, type:GLOSSARY_TERM, count:20}) { searchResults { entity { ... on GlossaryTerm { urn properties { name } } } } } }', {\"s\": name})\n for sr in d[\"search\"][\"searchResults\"]:\n e = sr.get(\"entity\") or {}\n p = e.get(\"properties\") or {}\n if p.get(\"name\") == name:\n return e[\"urn\"]\n return None\n\ndef main(spec_text: str, prev_spec_json: str, docs_hash: str, prev_state_json: str):\n if spec_text and str(spec_text).strip():\n spec = parse_json(str(spec_text))\n elif prev_spec_json and str(prev_spec_json).strip():\n spec = json.loads(str(prev_spec_json))\n else:\n raise Exception(\"无可用治理规格(LLM输出与缓存规格均为空)\")\n prev = json.loads(prev_state_json) if prev_state_json else {}\n urns = {\"domains\": {}, \"tags\": {}, \"terms\": {}}\n created = {\"domains\": 0, \"tags\": 0, \"terms\": 0}\n dl = gql('query{ listDomains(input:{start:0,count:100}) { domains { urn properties { name } } } }')\n existing = {}\n for d in dl[\"listDomains\"][\"domains\"]:\n p = d.get(\"properties\") or {}\n if p.get(\"name\"):\n existing[p[\"name\"]] = d[\"urn\"]\n for dom in spec.get(\"domains\", []):\n if dom[\"name\"] in existing:\n urns[\"domains\"][dom[\"key\"]] = existing[dom[\"name\"]]\n else:\n u = gql('mutation($i:CreateDomainInput!){ createDomain(input:$i) }', {\"i\": {\"name\": dom[\"name\"], \"description\": dom.get(\"description\", \"\")}})[\"createDomain\"]\n urns[\"domains\"][dom[\"key\"]] = u\n created[\"domains\"] += 1\n for tg in spec.get(\"tags\", []):\n u = \"urn:li:tag:\" + tg[\"key\"]\n if not exists_aspect(u, \"tagProperties\"):\n gql('mutation($i:CreateTagInput!){ createTag(input:$i) }', {\"i\": {\"id\": tg[\"key\"], \"name\": tg[\"key\"], \"description\": tg.get(\"description\", \"\")}})\n created[\"tags\"] += 1\n urns[\"tags\"][tg[\"key\"]] = u\n reuse_terms = prev.get(\"term_urns\", {}) if prev.get(\"docs_hash\") == docs_hash else {}\n for tm in spec.get(\"terms\", []):\n key = tm[\"key\"]\n if key in reuse_terms:\n urns[\"terms\"][key] = reuse_terms[key]\n continue\n u = \"urn:li:glossaryTerm:\" + key\n if exists_aspect(u, \"glossaryTerm\"):\n urns[\"terms\"][key] = u\n continue\n found = find_term_by_name(tm[\"name\"])\n if found:\n urns[\"terms\"][key] = found\n continue\n try:\n gql('mutation($i:CreateGlossaryEntityInput!){ createGlossaryTerm(input:$i) }', {\"i\": {\"id\": key, \"name\": tm[\"name\"], \"description\": tm.get(\"description\", \"\")}})\n created[\"terms\"] += 1\n urns[\"terms\"][key] = u\n except Exception:\n found2 = find_term_by_name(tm[\"name\"])\n if found2:\n urns[\"terms\"][key] = found2\n else:\n raise\n return {\n \"urns_json\": json.dumps(urns),\n \"spec_json\": json.dumps(spec, ensure_ascii=False),\n \"created_json\": json.dumps(created),\n \"spec_source\": \"llm\" if (spec_text and str(spec_text).strip()) else \"cached\",\n }","variables":[{"variable":"spec_text","value_selector":["9100000000005","text"],"value_type":"string"},{"variable":"prev_spec_json","value_selector":["9100000000002","prev_spec_json"],"value_type":"string"},{"variable":"docs_hash","value_selector":["9100000000002","docs_hash"],"value_type":"string"},{"variable":"prev_state_json","value_selector":["9100000000002","prev_state_json"],"value_type":"string"}],"outputs":{"urns_json":{"type":"string","children":null},"spec_json":{"type":"string","children":null},"created_json":{"type":"string","children":null},"spec_source":{"type":"string","children":null}},"selected":false}},{"id":"9100000000007","type":"custom","position":{"x":1760,"y":180},"positionAbsolute":{"x":1760,"y":180},"width":242,"height":180,"selected":false,"sourcePosition":"right","targetPosition":"left","data":{"type":"code","title":"组装同步任务(含ODS轻打标)","desc":"","code_language":"python3","code":"import json, re\n\nSENS_RULES = [\n (\"wh_pii_phone\", r\"phone|mobile\", r\"电话|手机\"),\n (\"wh_pii_id_card\", r\"id_card|idcard|identity_no\", r\"身份证\"),\n (\"wh_pii_bank\", r\"bank|account_no\", r\"银行|账号\"),\n (\"wh_biz_sensitive\", r\"contact_person|address\", r\"联系人|地址\"),\n]\n\ndef urn_of(name):\n if name.startswith(\"mysql.\"):\n return \"urn:li:dataset:(urn:li:dataPlatform:mysql,\" + name[len(\"mysql.\"):] + \",PROD)\"\n return \"urn:li:dataset:(urn:li:dataPlatform:hive,\" + name + \",PROD)\"\n\ndef main(tables_json: str, lineage_json: str, spec_json: str, urns_json: str, changed_tables: list):\n tables = json.loads(tables_json)\n edges = json.loads(lineage_json)\n spec = json.loads(spec_json)\n urns = json.loads(urns_json)\n ds_spec = {d[\"table\"]: d for d in spec.get(\"datasets\", [])}\n tag_map, dom_map, term_map = urns[\"tags\"], urns[\"domains\"], urns[\"terms\"]\n ups = {}\n for e in edges:\n ups.setdefault(e[\"downstream\"], []).append(urn_of(e[\"upstream\"]))\n ops = []\n for t in tables:\n full = t[\"db\"] + \".\" + t[\"table\"]\n if full not in changed_tables:\n continue\n d = ds_spec.get(full, {})\n layer = t[\"db\"]\n cols = [{\"name\": c[\"name\"], \"type\": c[\"type\"], \"comment\": c[\"comment\"]} for c in t[\"columns\"]]\n cols += [{\"name\": c[\"name\"], \"type\": c[\"type\"], \"comment\": (c[\"comment\"] or \"\") + \"(分区键)\"} for c in t[\"partition_keys\"]]\n tag_keys = list(d.get(\"tags\", []))\n for k in [\"wh\", \"wh_\" + layer, \"wh_demo\"]:\n if k in tag_map and k not in tag_keys:\n tag_keys.append(k)\n sens_fields = []\n if layer == \"ods\":\n for c in cols:\n for key, pat, zh in SENS_RULES:\n if re.search(pat, c[\"name\"], re.I) or re.search(zh, c[\"comment\"] or \"\"):\n if key in tag_map and key not in tag_keys:\n tag_keys.append(key)\n if c[\"name\"] not in sens_fields:\n sens_fields.append(c[\"name\"])\n term_fields = [{\"field\": f[\"field\"], \"term_urn\": term_map[f[\"term_key\"]]} for f in d.get(\"term_fields\", []) if f.get(\"term_key\") in term_map]\n ops.append(json.dumps({\n \"table\": full, \"layer\": layer, \"location\": t.get(\"location\", \"\"),\n \"comment\": t.get(\"comment\") or \"\", \"columns\": cols,\n \"description\": d.get(\"description\") or t.get(\"comment\") or \"\",\n \"domain_urn\": dom_map.get(d.get(\"domain_key\", \"\")),\n \"tag_urns\": [tag_map[k] for k in tag_keys if k in tag_map],\n \"term_fields\": term_fields,\n \"sensitive_fields\": sens_fields,\n \"upstreams\": ups.get(full, []),\n }, ensure_ascii=False))\n return {\"ops\": ops}","variables":[{"variable":"tables_json","value_selector":["9100000000002","tables_json"],"value_type":"string"},{"variable":"lineage_json","value_selector":["9100000000002","lineage_json"],"value_type":"string"},{"variable":"spec_json","value_selector":["9100000000006","spec_json"],"value_type":"string"},{"variable":"urns_json","value_selector":["9100000000006","urns_json"],"value_type":"string"},{"variable":"changed_tables","value_selector":["9100000000002","changed_tables"],"value_type":"array[string]"}],"outputs":{"ops":{"type":"array[string]","children":null}},"selected":false}},{"id":"9100000000008","type":"custom","position":{"x":2120,"y":160},"positionAbsolute":{"x":2120,"y":160},"width":620,"height":220,"selected":false,"sourcePosition":"right","targetPosition":"left","zIndex":1,"data":{"type":"iteration","title":"逐表同步DataHub+血缘","desc":"","selected":false,"is_parallel":false,"parallel_nums":4,"error_handle_mode":"continue-on-error","iterator_selector":["9100000000007","ops"],"iterator_input_type":"array[string]","output_selector":["9100000000009","result"],"output_type":"array[string]","is_iterating":true,"flatten_output":false,"start_node_id":"9100000000008start","width":620,"height":220}},{"id":"9100000000008start","type":"custom-iteration-start","parentId":"9100000000008","position":{"x":24,"y":68},"positionAbsolute":{"x":2144,"y":228},"width":44,"height":48,"selected":false,"draggable":false,"selectable":false,"sourcePosition":"right","targetPosition":"left","zIndex":1002,"data":{"type":"iteration-start","title":"","desc":"","isInIteration":true,"selected":false}},{"id":"9100000000009","type":"custom","position":{"x":2200,"y":228},"positionAbsolute":{"x":2200,"y":228},"width":242,"height":180,"selected":false,"sourcePosition":"right","targetPosition":"left","parentId":"9100000000008","zIndex":1002,"data":{"type":"code","title":"执行单表同步","desc":"","code_language":"python3","code":"import requests, json, time\nfrom urllib.parse import quote\n\nDH = \"http://datahub-datahub-gms-1:8080\"\nHDRS = {\"Authorization\": \"Bearer app-iBQEBasOC4aVSn2xZRxPk5zW\", \"Content-Type\": \"application/json\"}\n\ndef type_union(t):\n t = (t or \"\").lower()\n if any(k in t for k in [\"datetime\", \"timestamp\"]) or t in (\"date\", \"year\"):\n return {\"com.linkedin.schema.DateType\": {}}\n if t == \"time\":\n return {\"com.linkedin.schema.TimeType\": {}}\n if any(k in t for k in [\"int\", \"decimal\", \"float\", \"double\", \"numeric\"]):\n return {\"com.linkedin.schema.NumberType\": {}}\n if t == \"boolean\":\n return {\"com.linkedin.schema.BooleanType\": {}}\n if any(k in t for k in [\"binary\", \"json\"]):\n return {\"com.linkedin.schema.BytesType\": {}}\n return {\"com.linkedin.schema.StringType\": {}}\n\ndef propose(aspect_name, urn, value):\n body = {\"proposal\": {\"entityType\": \"dataset\", \"entityUrn\": urn, \"changeType\": \"UPSERT\", \"aspectName\": aspect_name,\n \"aspect\": {\"contentType\": \"application/json\", \"value\": json.dumps(value)}}}\n r = requests.post(DH + \"/aspects?action=ingestProposal\", headers=HDRS, json=body, timeout=25)\n return (\"ok\" if r.status_code == 200 and \"success\" in r.text else \"err\", r.text[:100])\n\ndef gql(q, v):\n r = requests.post(DH + \"/api/graphql\", headers=HDRS, json={\"query\": q, \"variables\": v}, timeout=25)\n j = r.json()\n if \"errors\" in j:\n return (\"err\", str(j[\"errors\"][0][\"message\"])[:100])\n return (\"ok\", \"\")\n\ndef main(item: str):\n op = json.loads(item)\n full = op[\"table\"]\n urn = \"urn:li:dataset:(urn:li:dataPlatform:hive,\" + full + \",PROD)\"\n ts = int(time.time() * 1000)\n who = {\"actor\": \"urn:li:corpuser:datahub\", \"time\": ts}\n fields = [{\"fieldPath\": c[\"name\"], \"type\": {\"type\": type_union(c[\"type\"])}, \"nativeType\": c[\"type\"],\n \"nativeDataType\": c[\"type\"], \"nullable\": True,\n \"description\": c[\"comment\"] or c[\"name\"]} for c in op[\"columns\"]]\n sm = {\"platform\": \"urn:li:dataPlatform:hive\", \"schemaName\": full,\n \"platformSchema\": {\"com.linkedin.schema.OtherSchema\": {\"rawSchema\": op.get(\"location\", \"\")}},\n \"fields\": fields, \"hash\": \"\", \"version\": 0, \"created\": who, \"lastModified\": who}\n dp = {\"schemaName\": full, \"description\": op[\"description\"],\n \"customProperties\": {\"database\": full.split(\".\")[0], \"table\": full.split(\".\")[1],\n \"layer\": op[\"layer\"], \"location\": op.get(\"location\", \"\"),\n \"storage\": \"minio\", \"sensitive_fields\": \",\".join(op.get(\"sensitive_fields\", []))},\n \"qualifiedName\": \"hive.\" + full, \"platform\": \"urn:li:dataPlatform:hive\",\n \"version\": 0, \"created\": who, \"lastModified\": who}\n r1 = propose(\"schemaMetadata\", urn, sm)\n r2 = propose(\"datasetProperties\", urn, dp)\n r3 = gql(\"mutation($e:String!,$d:String!){ setDomain(entityUrn:$e, domainUrn:$d) }\", {\"e\": urn, \"d\": op[\"domain_urn\"]}) if op.get(\"domain_urn\") else (\"skip\", \"\")\n r4 = gql(\"mutation($i:AddTagsInput!){ addTags(input:$i) }\", {\"i\": {\"tagUrns\": op[\"tag_urns\"], \"resourceUrn\": urn}}) if op.get(\"tag_urns\") else (\"skip\", \"\")\n r5 = (\"skip\", \"\")\n if op.get(\"upstreams\"):\n r5 = propose(\"upstreamLineage\", urn, {\"upstreams\": [{\"dataset\": u, \"type\": \"TRANSFORMED\", \"created\": who, \"lastModified\": who} for u in op[\"upstreams\"]]})\n r6 = (\"skip\", \"\")\n if op.get(\"term_fields\"):\n r6 = propose(\"editableSchemaMetadata\", urn, {\n \"editableSchemaFieldInfo\": [{\"fieldPath\": f[\"field\"], \"glossaryTerms\": [f[\"term_urn\"]]} for f in op[\"term_fields\"]],\n \"created\": who, \"lastModified\": who})\n r7 = (\"skip\", \"\")\n if op.get(\"sensitive_fields\"):\n sens_urns = [t for t in op[\"tag_urns\"] if any(k in t for k in [\"pii_\", \"biz_sensitive\"])]\n if sens_urns:\n st = []\n for fn in op[\"sensitive_fields\"]:\n fu = \"urn:li:schemaField:(\" + urn + \",\" + fn + \")\"\n st.append(gql(\"mutation($i:AddTagsInput!){ addTags(input:$i) }\", {\"i\": {\"tagUrns\": sens_urns, \"resourceUrn\": fu}})[0])\n r7 = (\"ok\" if all(x == \"ok\" for x in st) else \"err\", \",\".join(st))\n return {\"result\": json.dumps({\"table\": full, \"schemaMetadata\": r1[0], \"datasetProperties\": r2[0],\n \"domain\": r3[0], \"tags\": r4[0], \"lineage\": r5[0],\n \"field_terms\": r6[0], \"field_tags\": r7[0],\n \"detail\": [r1[1], r2[1], r3[1], r4[1], r5[1], r6[1], r7[1]]}, ensure_ascii=False)}","variables":[{"variable":"item","value_selector":["9100000000008","item"],"value_type":"string"}],"outputs":{"result":{"type":"string","children":null}},"selected":false,"isInIteration":true,"iteration_id":"9100000000008"}},{"id":"9100000000010","type":"custom","position":{"x":2980,"y":180},"positionAbsolute":{"x":2980,"y":180},"width":242,"height":220,"selected":false,"sourcePosition":"right","targetPosition":"left","data":{"type":"code","title":"回写同步状态","desc":"","code_language":"python3","code":"import requests, json, base64\nfrom datetime import datetime\n\nGITEA = \"http://gitea:3000\"\nREPO = \"admin/mini-erp-fabric-demo\"\nGH = {\"Authorization\": \"token 77c6bd2fdab535b2720821d36b4cfb74aa3bf87c\", \"Content-Type\": \"application/json\"}\n\ndef main(global_hash: str, docs_hash: str, table_hashes_json: str, urns_json: str, created_json: str, spec_json: str,\n prev_state_json: str, state_sha: str, sync_results: list, changed_tables: list):\n prev = json.loads(prev_state_json) if prev_state_json else {}\n results = [json.loads(x) for x in sync_results if x]\n ok_tables = [r[\"table\"] for r in results if r.get(\"schemaMetadata\") == \"ok\" and r.get(\"datasetProperties\") == \"ok\"]\n state = {\n \"global_hash\": global_hash,\n \"docs_hash\": docs_hash,\n \"table_hashes\": json.loads(table_hashes_json),\n \"term_urns\": json.loads(urns_json)[\"terms\"],\n \"last_created\": json.loads(created_json),\n \"last_ok_tables\": ok_tables,\n \"spec_json\": spec_json,\n \"updated_at\": datetime.now().strftime(\"%Y-%m-%d %H:%M:%S\"),\n \"runs\": int(prev.get(\"runs\", 0)) + 1,\n }\n content = base64.b64encode(json.dumps(state, ensure_ascii=True, indent=2).encode(\"utf-8\")).decode()\n url = GITEA + \"/api/v1/repos/\" + REPO + \"/contents/05_sync/warehouse_datahub_state.json\"\n payload = {\"content\": content, \"branch\": \"main\", \"message\": \"chore(sync): 更新数仓 DataHub 同步状态快照\"}\n if state_sha:\n payload[\"sha\"] = state_sha\n r = requests.put(url, headers=GH, json=payload, timeout=20)\n else:\n r = requests.post(url, headers=GH, json=payload, timeout=20)\n status = \"ok\" if r.status_code in (200, 201) else \"err\"\n summary = {\"state_saved\": status, \"written_ok\": ok_tables, \"http\": r.status_code, \"resp\": r.text[:150] if status == \"err\" else \"\"}\n return {\"status\": json.dumps(summary, ensure_ascii=False)}","variables":[{"variable":"global_hash","value_selector":["9100000000002","global_hash"],"value_type":"string"},{"variable":"docs_hash","value_selector":["9100000000002","docs_hash"],"value_type":"string"},{"variable":"table_hashes_json","value_selector":["9100000000002","table_hashes_json"],"value_type":"string"},{"variable":"urns_json","value_selector":["9100000000006","urns_json"],"value_type":"string"},{"variable":"created_json","value_selector":["9100000000006","created_json"],"value_type":"string"},{"variable":"spec_json","value_selector":["9100000000006","spec_json"],"value_type":"string"},{"variable":"prev_state_json","value_selector":["9100000000002","prev_state_json"],"value_type":"string"},{"variable":"state_sha","value_selector":["9100000000002","state_sha"],"value_type":"string"},{"variable":"sync_results","value_selector":["9100000000008","output"],"value_type":"array[string]"},{"variable":"changed_tables","value_selector":["9100000000002","changed_tables"],"value_type":"array[string]"}],"outputs":{"status":{"type":"string","children":null}},"selected":false}},{"id":"9100000000011","type":"custom","position":{"x":3340,"y":180},"positionAbsolute":{"x":3340,"y":180},"width":242,"height":120,"selected":false,"sourcePosition":"right","targetPosition":"left","data":{"type":"end","title":"同步结果","desc":"","selected":false,"outputs":[{"variable":"summary","value_type":"string","value_selector":["9100000000010","status"]},{"variable":"created","value_type":"string","value_selector":["9100000000006","created_json"]},{"variable":"spec_source","value_type":"string","value_selector":["9100000000006","spec_source"]}]}}],"edges":[{"id":"9100000000001-source-9100000000002-target","source":"9100000000001","target":"9100000000002","sourceHandle":"source","targetHandle":"target","type":"custom","zIndex":0,"data":{"sourceType":"start","targetType":"code","isInIteration":false,"isInLoop":false}},{"id":"9100000000002-source-9100000000003-target","source":"9100000000002","target":"9100000000003","sourceHandle":"source","targetHandle":"target","type":"custom","zIndex":0,"data":{"sourceType":"code","targetType":"if-else","isInIteration":false,"isInLoop":false}},{"id":"9100000000003-true-9100000000012-target","source":"9100000000003","target":"9100000000012","sourceHandle":"true","targetHandle":"target","type":"custom","zIndex":0,"data":{"sourceType":"if-else","targetType":"if-else","isInIteration":false,"isInLoop":false}},{"id":"9100000000003-false-9100000000004-target","source":"9100000000003","target":"9100000000004","sourceHandle":"false","targetHandle":"target","type":"custom","zIndex":0,"data":{"sourceType":"if-else","targetType":"end","isInIteration":false,"isInLoop":false}},{"id":"9100000000012-true-9100000000005-target","source":"9100000000012","target":"9100000000005","sourceHandle":"true","targetHandle":"target","type":"custom","zIndex":0,"data":{"sourceType":"if-else","targetType":"llm","isInIteration":false,"isInLoop":false}},{"id":"9100000000012-false-9100000000006-target","source":"9100000000012","target":"9100000000006","sourceHandle":"false","targetHandle":"target","type":"custom","zIndex":0,"data":{"sourceType":"if-else","targetType":"code","isInIteration":false,"isInLoop":false}},{"id":"9100000000005-source-9100000000006-target","source":"9100000000005","target":"9100000000006","sourceHandle":"source","targetHandle":"target","type":"custom","zIndex":0,"data":{"sourceType":"llm","targetType":"code","isInIteration":false,"isInLoop":false}},{"id":"9100000000006-source-9100000000007-target","source":"9100000000006","target":"9100000000007","sourceHandle":"source","targetHandle":"target","type":"custom","zIndex":0,"data":{"sourceType":"code","targetType":"code","isInIteration":false,"isInLoop":false}},{"id":"9100000000007-source-9100000000008-target","source":"9100000000007","target":"9100000000008","sourceHandle":"source","targetHandle":"target","type":"custom","zIndex":0,"data":{"sourceType":"code","targetType":"iteration","isInIteration":false,"isInLoop":false}},{"id":"9100000000008start-source-9100000000009-target","source":"9100000000008start","target":"9100000000009","sourceHandle":"source","targetHandle":"target","type":"custom","zIndex":0,"data":{"sourceType":"iteration-start","targetType":"code","isInIteration":false,"isInLoop":false}},{"id":"9100000000008-source-9100000000010-target","source":"9100000000008","target":"9100000000010","sourceHandle":"source","targetHandle":"target","type":"custom","zIndex":0,"data":{"sourceType":"iteration","targetType":"code","isInIteration":false,"isInLoop":false}},{"id":"9100000000010-source-9100000000011-target","source":"9100000000010","target":"9100000000011","sourceHandle":"source","targetHandle":"target","type":"custom","zIndex":0,"data":{"sourceType":"code","targetType":"end","isInIteration":false,"isInLoop":false}}],"viewport":{"x":0,"y":0,"zoom":0.5}},"features":{"file_upload":{"enabled":false,"allowed_file_upload_methods":["local_file","remote_url"],"allowed_file_types":["image"],"allowed_file_extensions":[]},"opening_statement":"","suggested_questions":[]},"conversation_variables":[],"environment_variables":[]}