hive—用json表示db模式

axkjgtzd  于 2021-06-26  发布在  Hive
关注(0)|答案(1)|浏览(329)

假设我的数据库中有两个表, employee 以及 car 如此定义。
员工:

  1. +--------------+------------+
  2. | col_name | data_type |
  3. +--------------+------------+
  4. | eid | int |
  5. | name | string |
  6. | salary | int |
  7. | destination | string |
  8. +--------------+------------+

汽车:

  1. +------------+----------------+
  2. | col_name | data_type |
  3. +------------+----------------+
  4. | cid | int |
  5. | name | string |
  6. | model | string |
  7. | cylinders | int |
  8. | price | int |
  9. +------------+----------------+

我想将这个模式导出到一个json对象,这样我就可以基于这个表填充一个html下拉菜单——例如 table 菜单上应该有 employee 以及 car . 选择 employee 将用对应于该表的列名和类型填充另一个下拉列表。
考虑到这个用例,数据库的最佳json表示形式是这样的吗?

  1. {
  2. "employee": {
  3. "salary": "int",
  4. "destination": "string",
  5. "eid": "int",
  6. "name": "string"
  7. },
  8. "car": {
  9. "price": "int",
  10. "model": "string",
  11. "cylinders": "int",
  12. "name": "string",
  13. "cid": "int"
  14. }
  15. }

编辑:还是这样更合适?

  1. {
  2. "employee": [
  3. {
  4. "type": "int",
  5. "colname": "eid"
  6. },
  7. {
  8. "type": "string",
  9. "colname": "name"
  10. },
  11. {
  12. "type": "int",
  13. "colname": "salary"
  14. },
  15. {
  16. "type": "string",
  17. "colname": "destination"
  18. }
  19. ],
  20. "car": [
  21. {
  22. "type": "int",
  23. "colname": "cid"
  24. },
  25. {
  26. "type": "string",
  27. "colname": "name"
  28. },
  29. {
  30. "type": "string",
  31. "colname": "model"
  32. },
  33. {
  34. "type": "int",
  35. "colname": "cylinders"
  36. },
  37. {
  38. "type": "int",
  39. "colname": "price"
  40. }
  41. ]
  42. }
jecbmhm3

jecbmhm31#

在第一个示例中,所有数据都存储在对象中。假设结构存储在var中 mytables ,你可以用 Object.keys(mytables) ,返回 ['employee', 'car'] . 内柱等效值: Object.keys(mytables['employee'].cols) 退货 ['salary','destination','eid','name'] .
在第二个示例中,我建议也将表作为列存储在数组中,如

  1. [name: 'employee',
  2. cols: [ {
  3. "type": "int",
  4. "colname": "cid"
  5. }, ...]

然后您可以通过访问 mytables[i].name ```
for (t in tables){
console.log(tables[t].name);
for (c in tables[t].cols)
console.log(" - ",tables[t].cols[c].colname, ": ", tables[t].cols[c].type);
}

展开查看全部

相关问题