我是靠谱客的博主 听话八宝粥,这篇文章主要介绍spark多数据源hive和mysql联合使用,现在分享给大家,希望可以做个参考。

1、需要将mysql驱动放入spark的jars包下面

复制代码
1
2
spark-2.6.0-bin-2.6.0-cdh5.16.2/jars/

2、准备数据
2.1、mysql库
在这里插入图片描述
2.2、准备hive数据
在这里插入图片描述
3、编写代码

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class MoreDatabasesSpark { public static void main(String[] args) { SparkSession session = SparkSession.builder().appName("SparkSessionApp") .master("spark://172.10.70.196:7077") .config("hive.metastore.uris", "thrift://172.10.70.196:9083") //直接连接hive .enableHiveSupport() .getOrCreate(); // Dataset<Row> peopleData = session.sql("select * from test_db.helloword"); peopleData.registerTempTable("helloword"); Dataset<Row> sexData = session.read().format("jdbc").option("url","jdbc:mysql://172.10.70.196:3306/bank_a?createDatabaseIfNotExist=true&useSSL=true&serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8") .option("dbtable", "test_data") .option("user", "admin") .option("password", "admin") .option("driver","com.mysql.cj.jdbc.Driver") .load(); sexData.registerTempTable("sex"); session.sql("select a.id,a.name,b.memo from helloword a left join sex b on a.id = b.val").show(); session.stop(); } }

4、将打包好的jar包上传服务器,并且运行

复制代码
1
2
./spark-submit --class org.example.SparkSqlToHive /usr/local/src/test/SparkTest-1.0-SNAPSHOT.jar

5、运行结果
在这里插入图片描述

最后

以上就是听话八宝粥最近收集整理的关于spark多数据源hive和mysql联合使用的全部内容,更多相关spark多数据源hive和mysql联合使用内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(64)

评论列表共有 0 条评论

立即
投稿
返回
顶部