js---数据转换

文章描述了一个数据库迁移项目中,不同数据源和目标数据库之间的表结构、字段映射以及元数据管理。涉及到MySQL、Hive等数据源,以及数据表之间的层次关系和字段关系的定义。
摘要由CSDN通过智能技术生成
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
	<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
	<script>
		var arr = [{
	"id": "1065733167",
	"name": "mysql_sys",
	"sourceDS": "mysql_sys",
	"sourceDB": "sys",
	"sourceTable": "beijing",
	"targetDS": "mysql_sys",
	"targetDB": "sys",
	"targetTable": "beijing",
	"sourceUrl": null,
	"readerColumns": null,
	"writerColumns": null,
	"metaDB": "1065733167",
	"metaTable": "beijing",
	"metadataFields": ["name", "id", "age"],
	"hierarchy": null,
	"parentId": "0",
	"createUser": null,
	"children": null
}, {
	"id": "61c2c9122c4864494cd553a4",
	"name": "czy",
	"sourceDS": "mysql_sys",
	"sourceDB": "sys",
	"sourceTable": "beijing",
	"targetDS": "mysql_czy123",
	"targetDB": "czy",
	"targetTable": "beijing",
	"sourceUrl": null,
	"readerColumns": null,
	"writerColumns": null,
	"metaDB": "61c2c9122c4864494cd553a4",
	"metaTable": "beijing",
	"metadataFields": ["name", "transition_time", "id", "age"],
	"hierarchy": null,
	"parentId": "1065733167",
	"createUser": "baixl",
	"children": null
}, {
	"id": "-1617758516",
	"name": "mysql_czy",
	"sourceDS": "hive_czy",
	"sourceDB": "czy",
	"sourceTable": "beijing",
	"targetDS": "mysql_czy",
	"targetDB": "czy",
	"targetTable": "liw002",
	"sourceUrl": null,
	"readerColumns": ["id", "age"],
	"writerColumns": ["name", "id", "age"],
	"metaDB": null,
	"metaTable": null,
	"metadataFields": null,
	"hierarchy": "DW层",
	"parentId": "61c2c9122c4864494cd553a4",
	"createUser": "superadmin",
	"children": null
}, {
	"id": "61a987f3a420774789fba455",
	"name": "czy",
	"sourceDS": "czy",
	"sourceDB": "czy",
	"sourceTable": "beijing",
	"targetDS": "czy",
	"targetDB": "czy",
	"targetTable": "beijing",
	"sourceUrl": null,
	"readerColumns": null,
	"writerColumns": null,
	"metaDB": "61a987f3a420774789fba455",
	"metaTable": "beijing",
	"metadataFields": ["name", "id", "age"],
	"hierarchy": null,
	"parentId": "61c2c9122c4864494cd553a4",
	"createUser": "baixl",
	"children": null
}, {
	"id": "391334892",
	"name": "czy",
	"sourceDS": null,
	"sourceDB": "czy",
	"sourceTable": "liw002",
	"targetDS": null,
	"targetDB": "etldb",
	"targetTable": "liw002",
	"sourceUrl": null,
	"readerColumns": ["name", "id", "age"],
	"writerColumns": ["name", "id", "age"],
	"metaDB": null,
	"metaTable": null,
	"metadataFields": null,
	"hierarchy": null,
	"parentId": "-1617758516",
	"createUser": "superadmin",
	"children": null
}]
var arr1 = [{
	"id": "1065733167",
	"name": "mysql_sys",
	"sourceDS": "mysql_sys",
	"sourceDB": "sys",
	"sourceTable": "beijing",
	"targetDS": "mysql_sys",
	"targetDB": "sys",
	"targetTable": "beijing",
	"sourceUrl": null,
	"readerColumns": null,
	"writerColumns": null,
	"metaDB": "1065733167",
	"metaTable": "beijing",
	"metadataFields": ["name", "id", "age"],
	"hierarchy": null,
	"parentId": "0",
	"createUser": null,
	"children": null
}, {
	"id": "61c2c9122c4864494cd553a4",
	"name": "czy",
	"sourceDS": "mysql_sys",
	"sourceDB": "sys",
	"sourceTable": "beijing",
	"targetDS": "mysql_czy123",
	"targetDB": "czy",
	"targetTable": "beijing",
	"sourceUrl": null,
	"readerColumns": null,
	"writerColumns": null,
	"metaDB": "61c2c9122c4864494cd553a4",
	"metaTable": "beijing",
	"metadataFields": ["name", "transition_time", "id", "age"],
	"hierarchy": null,
	"parentId": "1065733167",
	"createUser": "baixl",
	"children": null
}, {
	"id": "-1617758516",
	"name": "mysql_czy",
	"sourceDS": "hive_czy",
	"sourceDB": "czy",
	"sourceTable": "beijing",
	"targetDS": "mysql_czy",
	"targetDB": "czy",
	"targetTable": "liw002",
	"sourceUrl": null,
	"readerColumns": ["id", "age"],
	"writerColumns": ["name", "id", "age"],
	"metaDB": null,
	"metaTable": null,
	"metadataFields": null,
	"hierarchy": "DW层",
	"parentId": "61c2c9122c4864494cd553a4",
	"createUser": "superadmin",
	"children": null
}, {
	"id": "61a987f3a420774789fba455",
	"name": "czy",
	"sourceDS": "czy",
	"sourceDB": "czy",
	"sourceTable": "beijing",
	"targetDS": "czy",
	"targetDB": "czy",
	"targetTable": "beijing",
	"sourceUrl": null,
	"readerColumns": null,
	"writerColumns": null,
	"metaDB": "61a987f3a420774789fba455",
	"metaTable": "beijing",
	"metadataFields": ["name", "id", "age"],
	"hierarchy": null,
	"parentId": "61c2c9122c4864494cd553a4",
	"createUser": "baixl",
	"children": null
}, {
	"id": "391334892",
	"name": "czy",
	"sourceDS": null,
	"sourceDB": "czy",
	"sourceTable": "liw002",
	"targetDS": null,
	"targetDB": "etldb",
	"targetTable": "liw002",
	"sourceUrl": null,
	"readerColumns": ["name", "id", "age"],
	"writerColumns": ["name", "id", "age"],
	"metaDB": null,
	"metaTable": null,
	"metadataFields": null,
	"hierarchy": null,
	"parentId": "-1617758516",
	"createUser": "superadmin",
	"children": null
}]

		for (var i = 0; i < arr.length; i++) {
  let metaDB = arr[i].metaDB;
  if (metaDB == "" || metaDB == undefined || metaDB == null) {
    arr[i].writerColumns = [];
  } else {
    arr[i].metadataFields = [];
  }
}

for (var j = 0; j < arr1.length; j++) {
  let metaDB = arr1[j].metaDB;
  let data1 = arr1[j].metadataFields;
  let data2 = arr1[j].writerColumns;
  if (metaDB == "" || metaDB == undefined || metaDB == null) {
    if (data2 == "" || data2 == undefined || data2 == null || data2.length == 0) {
      let obj = {
        key: "表:" + arr1[j].targetTable,
        relation: [{ key: "表:" + arr1[j].targetTable, parentId: arr1[j].parentId == 0 ? "" : arr1[j].parentId }],
      }
      arr[j].writerColumns.push(obj)
    } else {
      for (var x = 0; x < data2.length; x++) {
        let obj = {
          key: arr1[j].writerColumns[x],
          relation: [{ key: arr1[j].writerColumns[x], parentId: arr1[j].parentId == 0 ? "" : arr1[j].parentId }],
        }
        arr[j].writerColumns.push(obj)
      }
    }
  } else {
    if (data1 == "" || data1 == undefined || data1 == null || data1.length == 0) {
      let obj = {
        key: "",
        relation: [{ key: "", parentId: arr1[j].parentId == 0 ? "" : arr1[j].parentId }],
      }
      arr[j].metadataFields.push(obj)
    } else {
      for (var x = 0; x < data1.length; x++) {
        let obj = {
          key: arr1[j].metadataFields[x],
          relation: [{ key: arr1[j].metadataFields[x], parentId: arr1[j].parentId == 0 ? "" : arr1[j].parentId }],
        }
        arr[j].metadataFields.push(obj
              }
    }
  }
}
console.log(arr)
	</script>
</body>
</html>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值