编写Scala工程代码,将MySQL的shtd_store库中表CUSTOMER、NATION、PART、PARTSUPP、REGION、SUPPLIER的数据全量抽取到Hive的ods库中对应表customer,nation,part,partsupp,region,supplier中,将表ORDERS、LINEITEM的数据增量抽取到Hive的ods库中对应表ORDERS,LINEITEM中。

本题直白一点将就是使用sparksql抽取mysql库中数据到hive中,拆开来看分为三步,第一步为连接hive,第二步为连接mysql并将数据抽取出来,第三步为将抽取出来的数据传到hive中所对应的表

首先第一步:连接hive

val spark = SparkSession.builder().master("local[*]").appName("抽取数据").enableHiveSupport().config("spark.sql.warehouse.dir", "hdfs://master:50070/usr/hive/warehouse").config("hive.metastore.uris", "thrift://master:9083").getOrCreate()

第二步:连接mysql并将数据拿到

spark.read.format("jdbc").option("driver", "com.mysql.jdbc.Driver").option("url", "jdbc:mysql://localhost:3306/spark-sql").option("user", "root").option("password", "123456").option("dbtable", "user").load().createTempView("data")spark.sql("select * from data").show()

最好show一下看看数据是否抽取成功

第三部:将数据传给hive中对应的表

spark.sql("use study")//静态分区spark.sql("""|create table if not exists customer(|id int,|name string,|age int|)|partitioned by(time string)|row format delimited fields terminated by '\t'|;|""".stripMargin)println("*************")spark.sql("""|insert overwrite table customer partition (time='1001')|select id,name,age|from data;|""".stripMargin)spark.sql("select * from customer").show()

完整代码:

def main(args: Array[String]): Unit = {System.setProperty("HADOOP_USER_NAME", "root")val spark = SparkSession.builder().master("local[*]").appName("抽取数据").enableHiveSupport().config("spark.sql.warehouse.dir", "hdfs://master:50070/usr/hive/warehouse").config("hive.metastore.uris", "thrift://master:9083").getOrCreate()spark.read.format("jdbc").option("driver", "com.mysql.jdbc.Driver").option("url", "jdbc:mysql://localhost:3306/spark-sql").option("user", "root").option("password", "123456").option("dbtable", "user").load().createTempView("data")spark.sql("select * from data").show()println("***************")spark.sql("use study")//静态分区spark.sql("""|create table if not exists customer(|id int,|name string,|age int|)|partitioned by(time string)|row format delimited fields terminated by '\t'|;|""".stripMargin)println("*************")spark.sql("""|insert overwrite table customer partition (time='1001')|select id,name,age|from data;|""".stripMargin)spark.sql("select * from customer").show()println("-------------------------------------------------------------")spark.stop()}

本题不难,首先要把hive和spark环境配好

所需环境配置:

pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>atguigu-classes</artifactId><groupId>com.atguigu.bigdata</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>spark-core</artifactId><dependencies><dependency><groupId>org.apache.spark</groupId><artifactId>spark-core_2.12</artifactId><version>3.0.3</version></dependency><dependency><groupId>org.apache.spark</groupId><artifactId>spark-sql_2.12</artifactId><version>3.0.3</version></dependency><dependency><groupId>org.apache.spark</groupId><artifactId>spark-streaming_2.12</artifactId><version>3.0.3</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.27</version></dependency><dependency><groupId>org.apache.spark</groupId><artifactId>spark-hive_2.12</artifactId><version>3.0.0</version></dependency><dependency><groupId>org.apache.hive</groupId><artifactId>hive-exec</artifactId><version>1.2.1</version></dependency><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version></dependency></dependencies><properties><maven.compiler.source>8</maven.compiler.source><maven.compiler.target>8</maven.compiler.target></properties></project>

然后将hive中的hive-site.xml文件移到resources下

运行代码前一定要先把hive进程启动,然后hive的服务端也要启动

启动命令:hive --service metastore &

大数据技能大赛任务一:数据抽取相关推荐

  1. 2022年北京大数据技能大赛“隐私计算”赛道初赛完结!12强出炉

    10月14日,2022年北京大数据技能大赛"隐私计算"赛道初赛顺利结束,阶段性成果初现. 前情提要 | 速来!2022北京大数据技能大赛隐私计算赛道可以提交了! 2022年北京大数 ...

  2. 2022年大数据技能大赛国赛(模块A,B)

    2022年全国职业院校技能大赛(高职组) "大数据技术与应用" 赛项赛卷(1卷) 背景描述 大数据时代背景下,电商经营模式发生很大改变.在传统运营模式中,缺乏数据积累,人们在做出一 ...

  3. 大数据技能大赛题目(高职组,模块C和D)

    模块C:数据清洗与挖掘分析(25分) 项目背景说明 餐饮外卖平台的核心价值体现在配送,而配送的价值则依赖于商家与客户的双向选择.外卖平台通常会通过内容激活消费者和商家两个群体的活跃度.消费者会参考平台 ...

  4. 2022年大数据技能大赛国赛(模块C,D)

    模块C:数据挖掘(10分) 环境说明: 服务端登录地址详见各模块服务端说明. 补充说明:各主机可通过Asbru工具或SSH客户端进行SSH访问: Master节点MySQL数据库用户名/密码:root ...

  5. 筛选列_Excel办公实操,两大办公技能,复杂数据进行筛选,一看就会

    包含合并单元格的自动筛选 用户经常会遇到如图所示的包含合并单元格的数据列表,在默认状态下,在此类包含合并单元格的数据列表中使用筛选,并不能得到正确的筛选结果. 例如,单击A1单元格的筛选标记,在下拉的 ...

  6. 2021年广东省专业技能大赛——大数据技术与应用 真题题目及解析(1)

    2021年广东省专业技能大赛--大数据技术与应用 真题题目及解析 文章适合用户了解大数据技能大赛的考试提纲和解析步骤.文章在编写过程中难免有疏漏和错误,欢迎大佬指出文章的不足之处:更多内容请点进

  7. 2021年广东省专业技能大赛——大数据技术与应用 真题题目及解析(2)

    2021年广东省专业技能大赛--大数据技术与应用 真题题目及解析(2) 文章适合用户了解大数据技能大赛的考试提纲和解析步骤.文章在编写过程中难免有疏漏和错误,欢迎大佬指出文章的不足之处:更多内容请点进

  8. 2021年全国职业院校技能大赛大数据应用技术国赛题

    2021年全国职业院校技能大赛 高职组 "大数据技术与应用" 赛项赛卷(GZ-xxxxxxx-X卷) 任 务 书 参赛队编号: 背景描述 据央视财经报道,2020年我国O2O市场规 ...

  9. 2019 年全国职业院校技能大赛GZ-2019032 大数据技术与应用(高职组)赛题库

    2019 年全国职业院校技能大赛GZ-2019032 大数据技术与应用(高职组)赛题库 目录 任务一:Hadoop 平台及组件的部署管理 一. Hadoop 全分布部署 二. Hadoop 伪分布部署 ...

最新文章

  1. 如何向外行解释产品经理频繁更改需求为什么会令程序员烦恼?
  2. MPLS、SD-WAN孰优孰劣?
  3. 常量元素记忆口诀_人体中的常量元素与微量元素
  4. MYSQL添加约束的两种方法
  5. TFLearn的简单实例
  6. Oracle收购Talari,第一家SD-WAN公有云提供商出现
  7. C++ 多字节与宽字符串的相互转换
  8. 这些话,是乔布斯给世间留下的真正伟大礼物
  9. Tools_Procexp找文件被哪个进程占用
  10. jdk安装好了怎么使用_安装jdk怎么打开
  11. SPSS实现重复测量方差分析
  12. 容错性低是什么意思_容错率高是什么意思(容错率高还是低比较好)
  13. 产品发布会快闪创意动画PPT模板
  14. CCF NOIP2018复赛提高组一等奖获奖名单
  15. 基于P5.js的动态绘板
  16. Scratch打气球游戏 电子学会图形化编程scratch等级考试三级真题答案2019-9
  17. html适合app的登陆页面,纯CSS3创意手机APP登录界面动画特效
  18. [bzoj4094][Usaco2013 Dec]Optimal Milking 线段树
  19. 风场可视化:绘制粒子
  20. AGV的关键技术与细节

热门文章

  1. springcloud学习(七)-Sidecar(多语言支持)
  2. Matlab实现自适应动态规划多层神经网络的算例汇总
  3. Android 自定义圆形文字头像
  4. 缘灭--HashMap底层原理之1.8put源码篇(三)
  5. 第六章 大数据,6.1 双11数据大屏背后的实时计算处理(作者:藏六 黄晓锋 同杰)...
  6. Android运行时异常“Binary XML file line # : Error inflating class”
  7. 2021年安全员-B证(广西省-2021版)考试报名及安全员-B证(广西省-2021版)免费试题
  8. Intel oneAPI学习笔记之环境变量配置运行一个PyTorch程序
  9. 2020蓝桥杯省赛第一场A组(C/C++)个人题解
  10. gets()和puts()