• 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. }

  • 相关阅读:
    菜鸟逆袭成为大佬,就靠这份《数据中心设施运维指南》,啃完你就知道多香了
    本周的error记录
    深入探索Spring AI:源码分析流式回答
    【教3妹学编程-算法题】最长奇偶子数组
    如何发高质量的外链?
    Abbkine ExKine总蛋白提取试剂盒的适用性和特点介绍
    使用Colossal-AI进行优化的测试笔记
    大型连锁超市---LIDI验厂再出新规?
    git 查漏补缺
    linux上交叉编译qt库
  • 原文地址:https://blog.csdn.net/m0_69097184/article/details/132807922