项目实战从0到1之Spark(6)Spark 读取mysql中的数据
发布日期:2021-05-14 00:16:48 浏览次数:41 分类:博客文章

本文共 2656 字,大约阅读时间需要 8 分钟。

Spark���������������mysql���������������

������������������������������mysql���������������������

������������

���������

[root@head42 spark]# spark-shell --jars /opt/spark/jars/mysql-connector-java-5.1.38.jar

���������

val df=spark.read.format("jdbc").option("delimiter",",").option("header","true").option("url","jdbc:mysql://192.168.56.103:3306/test").option("dbtable","customer").option("user","root").option("password","ok").load()#"dbtable":mysql������df.show

���������������java.sql.SQLException: No suitable driver

���������

[root@head42 ~]# cd /opt/hive/lib/[root@head42 lib]# cp mysql-connector-java-5.1.38.jar /opt/spark/jars/

���������������������������

============================================================

������������

���������sqoop,������sqoop

#!/bin/bashsqoop import \--connect jdbc:mysql://localhost:3306/test \	#test:mysql������������--table table_name \	--username root \--password ok \--target-dir /data/mydata13 \ #���������������������hdfs���������-m 1 #���������������

���������hive������������������������������������������������hdfs���������

create external table orders(order_id int,order_date timestamp,order_customer_id int,order_status string)row format delimitedfields terminated by ','location '/data/mydata1'

���������������������������������������spark������������������������

Spark ������hive ������������(mysql)

������������

1)������Hive metastore[root@head42 ~]# hive --service metastore &netstat -ano|grep 9083   ???2)������spark������Mysql[root@head42 ~]# spark-shell --conf spark.hadoop.hive.metastore.uris=thrift://localhost:90833���scala> spark.sql("show tables").showspark.sql("select * from database_name.table_name")//���������������������+--------+--------------+-----------+|database|     tableName|isTemporary|+--------+--------------+-----------+| default|      customer|      false|| default|text_customers|      false|+--------+--------------+-----------+���������Ok���!

������������

1���������hive���hive-site.xml���������spark���conf���������

2���������spark���hive-site.xml������

���������������
hive.metastore.uris
thrift://localhost:9083

3������������������������

[root@head42 conf]# hive --service metastore

4���������spark:

[root@head42 conf]# spark-shell

5������������

spark.sql("select * from database_name.table_name").show//������������������������������scala> spark.sql("show tables").show+--------+--------------+-----------+|database|     tableName|isTemporary|+--------+--------------+-----------+| default|      customer|      false|| default|text_customers|      false|+--------+--------------+-----------+���������OK������
上一篇:项目实战从0到1之Spark(7)读写Redis数据
下一篇:项目实战从0到1之Spark(5)Spark整合Elasticsearch-从ES读取数据

发表评论

最新留言

初次前来,多多关照!
[***.217.46.12]2025年04月18日 19时02分57秒