• Spark 增量抽取 Mysql To Hive


    题目要求:

    1. 抽取ds_db01库中customer_inf的增量数据进入Hive的ods库中表customer_inf。根据ods.user_info表中modified_time作为增量字段,只将新增的数据抽入,字段名称、类型不变,同时添加静态分区,分区字段为etl_date,类型为String,且值为当前日期的前一天日期(分区字段格式为yyyyMMdd)。使用hive cli执行show partitions ods.customer_inf命令;

    代码实现: 

    1. package org.example
    2. import org.apache.spark.SparkConf
    3. import org.apache.spark.sql.SparkSession
    4. import java.time.LocalDate
    5. object Demo {
    6. def main(args: Array[String]): Unit = {
    7. // 创建spark
    8. val conf = new SparkConf().setMaster("local[*]").setAppName("one")
    9. .set("spark.testing.memory", "2147480000").set("dfs.client.use.datanode.hostname", "true")
    10. System.setProperty("HADOOP_USER_NAME", "root")
    11. // 连接hive
    12. val spark = SparkSession.builder()
    13. // 配置 Hive Metastore 的连接地址
    14. .config("hive.metastore.uris", "thrift://192.168.23.60:9083")
    15. // 配置 Hive 数据仓库的存储位置
    16. .config("hive.metastore.warehouse", "hdfs://192.168.23.60://9000/user/hive/warehouse")
    17. // 配置 Spark SQL 的存储分配策略为 "LEGACY"
    18. .config("spark.sql.storeAssignmentPolicy", "LEGACY")
    19. // 添加其他自定义的 Spark 配置
    20. .config(conf)
    21. // 启用对 Hive 的支持,使得可以使用 Hive 的表和查询
    22. .enableHiveSupport()
    23. // 创建 SparkSession 对象
    24. .getOrCreate()
    25. //连接mysql
    26. spark.read.format("jdbc")
    27. .option("url","jdbc:mysql://192.168.23.60:3306/ds_db01??characterEncoding=UTF-8")
    28. .option("driver","com.mysql.jdbc.Driver")
    29. .option("user","root")
    30. .option("password","123456")
    31. .option("dbtable","customer_inf")
    32. .load().createOrReplaceTempView("v") //对该表创建视图
    33. spark.sql("select * from v")
    34. // 获取当天时间的前一天
    35. val unit = java.time.LocalDate.now().plusYears(-1).plusMonths(-1).plusDays(-1).toString().replace("-", "")
    36. val unit1 = unit.toInt
    37. //全量抽取
    38. // spark.sql(
    39. // s"""
    40. // |insert overwrite table gh_test.customer_inf
    41. // |partition (etl_date="${unit}")
    42. // |select * from v
    43. // |
    44. // |""".stripMargin).show()
    45. //
    46. //spark.sql("select * from gh_test.customer_inf").show
    47. //将modified_time类型转换为yyyyMMdd
    48. spark.sql(
    49. s"""
    50. |select customer_inf_id,customer_id,customer_name,identity_card_type,identity_card_no,mobile_phone,
    51. |customer_email,gender,customer_point,register_time,birthday,customer_level,customer_money,
    52. |from_unixtime(unix_timestamp(modified_time,'yyyy-MM-dd'),'yyyyMMdd') as modified_time
    53. |from v
    54. |""".stripMargin).createOrReplaceTempView("v1")
    55. // spark.sql("select count(*) from gh_test.customer_inf").show
    56. //从mysql中增量抽取到hive
    57. spark.sql(
    58. s"""
    59. |insert overwrite table gh_test.customer_inf
    60. |partition (etl_date="${unit}")
    61. |select * from v where modified_time>"${unit1}"
    62. |""".stripMargin
    63. ).show()
    64. // spark.sql("select * from gh_test.customer_inf").show
    65. // 查询抽取后的条数
    66. spark.sql("select count(*) from gh_test.customer_inf").show
    67. // spark.sql("desc gh_test.customer_inf")
    68. }
    69. }

  • 相关阅读:
    Vuex的简介以及入门案例
    C# Winform串口助手
    只开源36小时!GitHub标星139K从Java开发到软件架构师进阶笔记
    openGauss本地Centos7.6单机安装和简单应用
    一吨托盘式单臂吊设计
    挑战杯 机器视觉人体跌倒检测系统 - opencv python
    Linux 笔记 进程监测 进程管理
    518抽奖软件,支持中途临时追加名单
    信创之国产浪潮电脑+统信UOS操作系统体验2:安装visual studio code和cmake搭建C++开发环镜
    java - 寻找一个值的二分查找、寻找左/侧边界的二分查找总结
  • 原文地址:https://blog.csdn.net/m0_69097184/article/details/132807922