• 10. awd 层(数据集市层)


    10. awd 层(数据集市层)

    10.1 指标体系相关概念

    10.2 ads层的结构如下:

    10.3 在hive中的建表语句如下:

    1. CREATE TABLE IF NOT EXISTS ads.ads_rk_ccxx_xz_index_d(
    2. XZJD string comment'乡镇(街道)',
    3. sfyfc_num BIGINT comment '多少人有房',
    4. sfyfc_num_p DOUBLE comment '有房人口的比例',
    5. sfygc_num BIGINT comment '多少人有公司',
    6. sfygc_num_p DOUBLE comment '有公司人口的比例',
    7. sfygjj_num BIGINT comment '多少人有公积金',
    8. sfygjj_num_p DOUBLE comment '有公积金人口的比例',
    9. sfysb_num BIGINT comment '多少人有社保',
    10. sfysb_num_p DOUBLE comment '有社保人口的比例'
    11. )
    12. PARTITIONED BY (
    13. ds string comment '分区'
    14. )
    15. ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'
    16. STORED AS TEXTFILE
    17. location'/daas/motl/ads/ads_rk_ccxx_xz_index_d';

    10.4 根据需求将dws层和dim层关联起来,通过计算,得到结果。

    1. insert overwrite table ads.ads_rk_ccxx_xz_index_d partition(ds=${ds})
    2. select
    3. b.XZJD,
    4. sum(sfyfc) as sfyfc_num,
    5. round(sum(sfyfc)/count(1),4) as sfyfc_num_p,
    6. sum(case when sfygc=1 or sywgd =1 then 1 else 0 end) as sfygc_num,
    7. round(sum(case when sfygc=1 or sywgd =1 then 1 else 0 end) / count(1),4) as sfygc_num_p,
    8. sum(sfygjj) as sfygjj_num,
    9. round(sum(sfygjj)/count(1),4) as sfygjj_num_p,
    10. sum(sfysb) as sfysb_num,
    11. round(sum(sfysb)/count(1),4) as sfysb_num_p
    12. from
    13. (
    14. select * from
    15. dws.dws_population_property_info_msk_d
    16. where ds=${ds}
    17. ) as a
    18. inner join
    19. (
    20. select id,XZJD from
    21. dim.dim_user_info_d where ds=${ds}
    22. ) as b
    23. on a.id=b.id
    24. group by b.XZJD

    10.5 执行脚本如下:

    1. # 分区
    2. ds=$1
    3. # 让环境变量生效
    4. source /etc/profile
    5. # 获取脚本所有在的位置
    6. shell_home="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
    7. # 切换目录
    8. cd $shell_home
    9. # 执行脚本
    10. # 执行任务
    11. # num-executor 在项目现场一般50-100个
    12. spark-sql \
    13. --master yarn-client \
    14. --num-executors=1 \
    15. --executor-cores=2 \
    16. --executor-memory=4G \
    17. --conf spark.sql.shuffle.partitions=2 \
    18. -f ../dql/ads_rk_ccxx_xz_index_d.sql \
    19. -d ds=$ds
    20. /home/ads/dql/ads_rk_ccxx_xz_index_d.sql

    先自己测试一下

    10.6 我们将hive中的表导入到mysql中

    问题:为什么不直接从hive中查询呢?

    :在hive上通过各种计算得到的数据体量不是很大,如果用Hive会存在高延迟的情况,

    相反,将数据放到mysql中,可以进行实时查询,速度更快

    而且在mysql中可以建立索引,加速数据的查询

    注意点: 在hive中并没有时间分区字段,只是分区表,所以我们在进行数据同步到mysql中,需要加上

    10.7 现在mysql中将表建立起来

    1. DROP TABLE IF EXISTS `ads_rk_ccxx_xz_index_d`;
    2. CREATE TABLE `ads_rk_ccxx_xz_index_d` (
    3. `xzjd` varchar(255) NOT NULL,
    4. `sfyfc_num` bigint(20) DEFAULT NULL,
    5. `sfyfc_num_p` double(20,4) DEFAULT NULL,
    6. `sfygc_num` bigint(20) DEFAULT NULL,
    7. `sfygc_num_p` double(20,4) DEFAULT NULL,
    8. `sfygjj_num` bigint(20) DEFAULT NULL,
    9. `sfygjj_num_p` double(20,4) DEFAULT NULL,
    10. `sfysb_num` bigint(20) DEFAULT NULL,
    11. `sfysb_num_p` double(20,4) DEFAULT NULL,
    12. `ds` varchar(255) NOT NULL,
    13. PRIMARY KEY (`xzjd`,`ds`)
    14. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

    10.8 同步脚本如下:

    1. {
    2. "job": {
    3. "setting": {
    4. "speed": {
    5. "channel": 1
    6. },
    7. "errorLimit": {
    8. "record": 0,
    9. "percentage": 0.02
    10. }
    11. },
    12. "content": [
    13. {
    14. "reader": {
    15. "name": "hdfsreader",
    16. "parameter": {
    17. "path": "/daas/motl/ads/ads_rk_ccxx_xz_index_d/ds=${ds}",
    18. "defaultFS": "hdfs://master:9000",
    19. "column": [
    20. {
    21. "index": 0,
    22. "type": "string"
    23. },
    24. {
    25. "index": 1,
    26. "type": "long"
    27. },
    28. {
    29. "index": 2,
    30. "type": "DOUBLE"
    31. },
    32. {
    33. "index": 3,
    34. "type": "long"
    35. },
    36. {
    37. "index": 4,
    38. "type": "DOUBLE"
    39. },
    40. {
    41. "index": 5,
    42. "type": "long"
    43. },
    44. {
    45. "index": 6,
    46. "type": "DOUBLE"
    47. },
    48. {
    49. "index": 7,
    50. "type": "long"
    51. },
    52. {
    53. "index": 8,
    54. "type": "DOUBLE"
    55. },
    56. {
    57. "type": "string",
    58. "value": "${ds}"
    59. }
    60. ],
    61. "fileType": "text",
    62. "encoding": "UTF-8",
    63. "fieldDelimiter": "\t"
    64. }
    65. },
    66. "writer": {
    67. "name": "mysqlwriter",
    68. "parameter": {
    69. "writeMode": "replace",
    70. "username": "root",
    71. "password": "123456",
    72. "column": [
    73. "XZJD",
    74. "sfyfc_num",
    75. "sfyfc_num_p",
    76. "sfygc_num",
    77. "sfygc_num_p",
    78. "sfygjj_num",
    79. "sfygjj_num_p",
    80. "sfysb_num",
    81. "sfysb_num_p",
    82. "ds"
    83. ],
    84. "connection": [
    85. {
    86. "jdbcUrl": "jdbc:mysql://master:3306/bigdata17?useUnicode=true&characterEncoding=utf-8",
    87. "table": [
    88. "ads_rk_ccxx_xz_index_d"
    89. ]
    90. }
    91. ]
    92. }
    93. }
    94. }
    95. ]
    96. }
    97. }

    10.9 当然了,执行脚本就比较简单了

    1. # 分区
    2. ds=$1
    3. # 让环境变量生效
    4. source /etc/profile
    5. # 获取脚本所有在的位置
    6. shell_home="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
    7. # 切换目录
    8. cd $shell_home
    9. # 执行脚本
    10. datax.py -p "-Dds=${ds}" ../datax/ods_t_fcj_nwrs_sellbargain.json

    10 .10 结果如下:

  • 相关阅读:
    云数据库 GaussDB(for Influx) 解密第十一期:让智能电网中时序数据处理更高效
    在Windows或Mac上安装并运行LLAMA2
    简述 happens - before 八大规则
    Pinterest:从 Druid 到 StarRocks,实现 6 倍成本效益比提升
    PostgreSQL中删除具有外键的表数据
    效率工作:一键为多种资产添加统一材质(小插件)
    数据库系统原理与应用教程(058)—— MySQL 练习题(二):单选题
    线上出问题了,怎么办?
    Java - NPE(NullPointerException);Optional
    FPGA与ASIC有什么差异?二者该如何选用?
  • 原文地址:https://blog.csdn.net/weixin_48370579/article/details/126273531