赞
踩
Spark on Hive 是Hive只作为存储角色,Spark负责sql解析优化,执行。这里可以理解为Spark 通过Spark SQL 使用Hive 语句操作Hive表 ,底层运行的还是 Spark RDD。具体步骤如下:
具体实现在我之前的博文中已经讲过,在这里就不再重复了,实现很简单,可以参考:大数据Hadoop之——Spark SQL+Spark Streaming
【总结】Spark使用Hive来提供表的metadata信息。
Hive on Spark是Hive既作为存储又负责sql的解析优化,Spark负责执行。这里Hive的执行引擎变成了Spark,不再是MR,这个要实现比Spark on Hive麻烦很多, 必须重新编译你的spark和导入jar包,不过目前大部分使用的确实是spark on hive。
【总结】hive on spark大体与spark on hive结构类似,只是SQL引擎不同,但是计算引擎都是spark!
参考文档:
编译Spark源码
要使用Hive on Spark,所用的Spark版本必须不包含Hive的相关jar包,hive on spark 的官网上说“Note that you must have a version of Spark which does not include the Hive jars”。在spark官网下载的编译的Spark都是有集成Hive的,因此需要自己下载源码来编译,并且编译的时候不指定Hive。最终版本:Hadoop3.3.1+Spark2.3.0+Hive3.1.2,其实主要是spark和hive版本对应上就行,hadoop版本好像没那么严格,所以这里hadoop版本我使用当前最新版本,但是还是建议使用hive的pom.xml配置文件里配置的版本。
$ cd /opt/bigdata/hadoop/software
$ wget http://archive.apache.org/dist/hive/hive-3.1.2/apache-hive-3.1.2-src.tar.gz
$ tar -zxvf apache-hive-3.1.2-src.tar.gz
$ egrep 'spark.version|hadoop.version' apache-hive-3.1.2-src/pom.xml
下载地址:https://archive.apache.org/dist/spark/spark-2.3.0/
$ cd /opt/bigdata/hadoop/software
# 下载
$ wget http://archive.apache.org/dist/spark/spark-2.3.0/spark-2.3.0.tgz
# 解压
$ tar -zxvf spark-2.3.0.tgz
$ cd spark-2.3.0
# 开始编译,注意hadoop版本
$ ./dev/make-distribution.sh --name without-hive --tgz -Pyarn -Phadoop-2.7 -Dhadoop.version=3.3.1 -Pparquet-provided -Porc-provided -Phadoop-provided
# 或者(这里不执行下面这句,因为跟上面等价)
$ ./dev/make-distribution.sh --name "without-hive" --tgz "-Pyarn,hadoop-provided,hadoop-2.7,parquet-provided,orc-provided"
命令解释:
-Phadoop-3.3 \ -Dhadoop.version=3.3.1 \ ***指定hadoop版本为3.3.1
--name without-hive hive 是编译文件的名字参数
--tgz ***压缩成tgz格式
-Pyarn 是支持yarn
-Phadoop-2.7 是支持的hadoop版本,一开始使用的是3.3后来提示hadoop3.3不存在,只好改成2.7,编译成功
-Dhadoop.version=3.3.1 运行环境
但是发现编译卡住了,原来编译会自动下载maven和scala,存放在build目录下,如图:
自动下载完maven和scala,就开始编译了,编译耗时还是比较久,慢慢等待编译结束吧。
编译花了半个小时左右,终于编译完成了。编译的时间太漫长,下面我也会把我编译好的spark包放在网盘上供大家下载使用。
在当前目录下就有编译好的spark包
$ ll
$ tar -zxvf spark-2.3.0-bin-without-hive.tgz -C /opt/bigdata/hadoop/server/
$ cd /opt/bigdata/hadoop/server/spark-2.3.0-bin-without-hive
$ ll
【温馨提示】hive-site.xml文件里配置需要。
$ cd /opt/bigdata/hadoop/server/spark-2.3.0-bin-without-hive/
### 创建日志存放目录
$ hadoop fs -mkdir -p hdfs://hadoop-node1:8082/tmp/spark
### 在hdfs上创建存放jar包目录
$ hadoop fs -mkdir -p /spark/spark-2.4.5-jars
## 上传jars到HDFS
$ hadoop fs -put ./jars/* /spark/spark-2.4.5-jars/
如果使用了打包好的jar包,hive操作时会报如下错误:
Failed to execute spark task, with exception ‘org.apache.hadoop.hive.ql.metadata.HiveException(Failed to create Spark client for Spark session c8c46c14-4d2a-4f7e-9a12-0cd62bf097db)’
FAILED: Execution Error, return code 30041 from org.apache.hadoop.hive.ql.exec.spark.SparkTask. Failed to create Spark client for Spark session c8c46c14-4d2a-4f7e-9a12-0cd62bf097db
【温馨提示】spark-default.xml文件需要配置打包好的jar包,spark-submit会调用。
$ cd /opt/bigdata/hadoop/server/spark-2.3.0-bin-without-hive/
$ jar cv0f spark2.3.0-without-hive-libs.jar -C ./jars/ .
$ ll
### 在hdfs上创建存放jar包目录
$ hadoop fs -mkdir -p /spark/jars
## 上传jars到HDFS
$ hadoop fs -put spark2.3.0-without-hive-libs.jar /spark/jars/
如果不打包,则会报如下错误:
Exception in thread “main” java.io.FileNotFoundException: File does not exist: hdfs://hadoop-node1:8082/spark/spark-2.3.0-jars/*.jar
at org.apache.hadoop.hdfs.DistributedFileSystem$29.doCall(DistributedFileSystem.java:1756)
at org.apache.hadoop.hdfs.DistributedFileSystem 29. d o C a l l ( D i s t r i b u t e d F i l e S y s t e m . j a v a : 1749 ) a t o r g . a p a c h e . h a d o o p . f s . F i l e S y s t e m L i n k R e s o l v e r . r e s o l v e ( F i l e S y s t e m L i n k R e s o l v e r . j a v a : 81 ) a t o r g . a p a c h e . h a d o o p . h d f s . D i s t r i b u t e d F i l e S y s t e m . g e t F i l e S t a t u s ( D i s t r i b u t e d F i l e S y s t e m . j a v a : 1764 ) a t o r g . a p a c h e . s p a r k . d e p l o y . y a r n . C l i e n t D i s t r i b u t e d C a c h e M a n a g e r 29.doCall(DistributedFileSystem.java:1749) at org.apache.hadoop.fs.FileSystemLinkResolver.resolve(FileSystemLinkResolver.java:81) at org.apache.hadoop.hdfs.DistributedFileSystem.getFileStatus(DistributedFileSystem.java:1764) at org.apache.spark.deploy.yarn.ClientDistributedCacheManager 29.doCall(DistributedFileSystem.java:1749)atorg.apache.hadoop.fs.FileSystemLinkResolver.resolve(FileSystemLinkResolver.java:81)atorg.apache.hadoop.hdfs.DistributedFileSystem.getFileStatus(DistributedFileSystem.java:1764)atorg.apache.spark.deploy.yarn.ClientDistributedCacheManager$anonfun 1. a p p l y ( C l i e n t D i s t r i b u t e d C a c h e M a n a g e r . s c a l a : 71 ) a t o r g . a p a c h e . s p a r k . d e p l o y . y a r n . C l i e n t D i s t r i b u t e d C a c h e M a n a g e r 1.apply(ClientDistributedCacheManager.scala:71) at org.apache.spark.deploy.yarn.ClientDistributedCacheManager 1.apply(ClientDistributedCacheManager.scala:71)atorg.apache.spark.deploy.yarn.ClientDistributedCacheManager$anonfun 1. a p p l y ( C l i e n t D i s t r i b u t e d C a c h e M a n a g e r . s c a l a : 71 ) a t s c a l a . c o l l e c t i o n . M a p L i k e 1.apply(ClientDistributedCacheManager.scala:71) at scala.collection.MapLike 1.apply(ClientDistributedCacheManager.scala:71)atscala.collection.MapLikeclass.getOrElse(MapLike.scala:128)
at scala.collection.AbstractMap.getOrElse(Map.scala:59)
at org.apache.spark.deploy.yarn.ClientDistributedCacheManager.addResource(ClientDistributedCacheManager.scala:71)
at org.apache.spark.deploy.yarn.Client.org a p a c h e apache apachespark d e p l o y deploy deployyarn C l i e n t Client Client$distribute 1 ( C l i e n t . s c a l a : 480 ) a t o r g . a p a c h e . s p a r k . d e p l o y . y a r n . C l i e n t . p r e p a r e L o c a l R e s o u r c e s ( C l i e n t . s c a l a : 517 ) a t o r g . a p a c h e . s p a r k . d e p l o y . y a r n . C l i e n t . c r e a t e C o n t a i n e r L a u n c h C o n t e x t ( C l i e n t . s c a l a : 863 ) a t o r g . a p a c h e . s p a r k . d e p l o y . y a r n . C l i e n t . s u b m i t A p p l i c a t i o n ( C l i e n t . s c a l a : 169 ) a t o r g . a p a c h e . s p a r k . s c h e d u l e r . c l u s t e r . Y a r n C l i e n t S c h e d u l e r B a c k e n d . s t a r t ( Y a r n C l i e n t S c h e d u l e r B a c k e n d . s c a l a : 57 ) a t o r g . a p a c h e . s p a r k . s c h e d u l e r . T a s k S c h e d u l e r I m p l . s t a r t ( T a s k S c h e d u l e r I m p l . s c a l a : 164 ) a t o r g . a p a c h e . s p a r k . S p a r k C o n t e x t . < i n i t > ( S p a r k C o n t e x t . s c a l a : 500 ) a t o r g . a p a c h e . s p a r k . S p a r k C o n t e x t 1(Client.scala:480) at org.apache.spark.deploy.yarn.Client.prepareLocalResources(Client.scala:517) at org.apache.spark.deploy.yarn.Client.createContainerLaunchContext(Client.scala:863) at org.apache.spark.deploy.yarn.Client.submitApplication(Client.scala:169) at org.apache.spark.scheduler.cluster.YarnClientSchedulerBackend.start(YarnClientSchedulerBackend.scala:57) at org.apache.spark.scheduler.TaskSchedulerImpl.start(TaskSchedulerImpl.scala:164) at org.apache.spark.SparkContext.<init>(SparkContext.scala:500) at org.apache.spark.SparkContext 1(Client.scala:480)atorg.apache.spark.deploy.yarn.Client.prepareLocalResources(Client.scala:517)atorg.apache.spark.deploy.yarn.Client.createContainerLaunchContext(Client.scala:863)atorg.apache.spark.deploy.yarn.Client.submitApplication(Client.scala:169)atorg.apache.spark.scheduler.cluster.YarnClientSchedulerBackend.start(YarnClientSchedulerBackend.scala:57)atorg.apache.spark.scheduler.TaskSchedulerImpl.start(TaskSchedulerImpl.scala:164)atorg.apache.spark.SparkContext.<init>(SparkContext.scala:500)atorg.apache.spark.SparkContext.getOrCreate(SparkContext.scala:2486)
at org.apache.spark.sql.SparkSession B u i l d e r Builder Builder$anonfun 7. a p p l y ( S p a r k S e s s i o n . s c a l a : 930 ) a t o r g . a p a c h e . s p a r k . s q l . S p a r k S e s s i o n 7.apply(SparkSession.scala:930) at org.apache.spark.sql.SparkSession 7.apply(SparkSession.scala:930)atorg.apache.spark.sql.SparkSessionBuilderKaTeX parse error: Can't use function '$' in math mode at position 8: anonfun$̲7.apply(SparkSe…runMain(SparkSubmit.scala:879)
at org.apache.spark.deploy.SparkSubmit$.doRunMain 1 ( S p a r k S u b m i t . s c a l a : 197 ) a t o r g . a p a c h e . s p a r k . d e p l o y . S p a r k S u b m i t 1(SparkSubmit.scala:197) at org.apache.spark.deploy.SparkSubmit 1(SparkSubmit.scala:197)atorg.apache.spark.deploy.SparkSubmit.submit(SparkSubmit.scala:227)
at org.apache.spark.deploy.SparkSubmit$.main(SparkSubmit.scala:136)
at org.apache.spark.deploy.SparkSubmit.main(SparkSubmit.scala)
$ cd /opt/bigdata/hadoop/server/spark-2.3.0-bin-without-hive/conf
# copy一个配置文件
$ cp spark-defaults.conf.template spark-defaults.conf
spark-defaults.conf修改内容如下:
spark.master yarn
spark.home /opt/bigdata/hadoop/server/spark-2.3.0-bin-without-hive
spark.eventLog.enabled true
spark.eventLog.dir hdfs://hadoop-node1:8082/tmp/spark
spark.serializer org.apache.spark.serializer.KryoSerializer
spark.executor.memory 1g
spark.driver.memory 1g
spark.executor.extraJavaOptions -XX:+PrintGCDetails -Dkey=value -Dnumbers="one two three"
spark.yarn.archive hdfs:///spark/jars/spark2.3.0-without-hive-libs.jar
spark.yarn.jars hdfs:///spark/jars/spark2.3.0-without-hive-libs.jar
### 参数解释,不用复制到配置文件中
# spark.master指定Spark运行模式,可以是yarn-client、yarn-cluster...
# spark.home指定SPARK_HOME路径
# spark.eventLog.enabled需要设为true
# spark.eventLog.dir指定路径,放在master节点的hdfs中,端口要跟hdfs设置的端口一致(默认为8020),否则会报错
# spark.executor.memory和spark.driver.memory指定executor和dirver的内存,512m或1g,既不能太大也不能太小,因为太小运行不了,太大又会影响其他服务
$ cd /opt/bigdata/hadoop/server/spark-2.3.0-bin-without-hive/conf
$ cp spark-env.sh.template spark-env.sh
# 在spark-env.sh添加如下内容
$ vi spark-env.sh
export SPARK_DIST_CLASSPATH=$(hadoop classpath)
export HADOOP_CONF_DIR={HADOOP_HOME}/etc/hadoop/
# 加载
$ source spark-env.sh
在Yarn模式运行时,需要将以下三个包放在HIVE_HOME/lib下 :scala-library、spark-core、spark-network-common。
$ cd /opt/bigdata/hadoop/server/spark-2.3.0-bin-without-hive
# 先删
$ rm -f ../apache-hive-3.1.2-bin/lib/scala-library-*.jar
$ rm -f ../apache-hive-3.1.2-bin/lib/spark-core_*.jar
$ rm -f ../apache-hive-3.1.2-bin/lib/spark-network-common_*.jar
# copy这三个jar到hive lib目录下
$ cp jars/scala-library-*.jar ../apache-hive-3.1.2-bin/lib/
$ cp jars/spark-core_*.jar ../apache-hive-3.1.2-bin/lib/
$ cp jars/spark-network-common_*.jar ../apache-hive-3.1.2-bin/lib/
$ cd /opt/bigdata/hadoop/server/apache-hive-3.1.2-bin/conf/
#配置hive-site.xml,主要mysql数据库
$ cat << EOF > hive-site.xml
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<!-- 配置hdfs存储目录 -->
<property>
<name>hive.metastore.warehouse.dir</name>
<value>/user/hive_remote/warehouse</value>
</property>
<!-- 所连接的 MySQL 数据库的地址,hive_remote是数据库,程序会自动创建,自定义就行 -->
<property>
<name>javax.jdo.option.ConnectionURL</name>
<value>jdbc:mysql://hadoop-node1:3306/hive_remote2?createDatabaseIfNotExist=true&useSSL=false&serverTimezone=Asia/Shanghai</value>
</property>
<!-- 本地模式
<property>
<name>hive.metastore.local</name>
<value>false</value>
</property>
-->
<!-- MySQL 驱动 -->
<property>
<name>javax.jdo.option.ConnectionDriverName</name>
<value>com.mysql.jdbc.Driver</value>
</property>
<!-- mysql连接用户 -->
<property>
<name>javax.jdo.option.ConnectionUserName</name>
<value>root</value>
</property>
<!-- mysql连接密码 -->
<property>
<name>javax.jdo.option.ConnectionPassword</name>
<value>123456</value>
</property>
<!--元数据是否校验-->
<property>
<name>hive.metastore.schema.verification</name>
<value>false</value>
</property>
<property>
<name>system:user.name</name>
<value>root</value>
<description>user name</description>
</property>
<!-- host -->
<property>
<name>hive.server2.thrift.bind.host</name>
<value>hadoop-node1</value>
<description>Bind host on which to run the HiveServer2 Thrift service.</description>
</property>
<!-- hs2端口 -->
<property>
<name>hive.server2.thrift.port</name>
<value>11000</value>
</property>
<property>
<name>hive.metastore.uris</name>
<value>thrift://hadoop-node1:9083</value>
</property>
<!--Spark依赖位置,上面上传jar包的hdfs路径-->
<property>
<name>spark.yarn.jars</name>
<value>hdfs:///spark/spark-2.3.0-jars/*.jar</value>
</property>
<!--Hive执行引擎,使用spark-->
<property>
<name>hive.execution.engine</name>
<value>spark</value>
</property>
<!--Hive和spark连接超时时间-->
<property>
<name>hive.spark.client.connect.timeout</name>
<value>10000ms</value>
</property>
</configuration>
EOF
在/etc/profile添加如下配置:
export HIVE_HOME=/opt/bigdata/hadoop/server/apache-hive-3.1.2-bin
export PATH=$HIVE_HOME/bin:$PATH
export SPARK_HOME=/opt/bigdata/hadoop/server/spark-2.3.0-bin-without-hive
export PATH=$SPARK_HOME/bin:$PATH
加载
$ source /etc/profile
不清楚的可以先看一下这篇文章 大数据Hadoop之——数据仓库Hive
# 初始化,--verbose:查询详情,可以不加
$ schematool -initSchema -dbType mysql --verbose
# 先查进程是否存在,存在则kill掉
$ ss -atnlp|grep 9083
# 启动metstore服务
$ nohup hive --service metastore &
先验证编译好的spark是否ok,就用spark提供的示例:SparkPI
$ spark-submit \
--class org.apache.spark.examples.SparkPi \
--master yarn \
--deploy-mode client \
--driver-memory 1G \
--num-executors 3 \
--executor-memory 1G \
--executor-cores 1 \
/opt/bigdata/hadoop/server/spark-2.3.0-bin-without-hive/examples/jars/spark-examples_*.jar 10
从上图发现编译好的spark包是没问题的,接下来就是验证hive提交spark任务
$ mkdir /opt/bigdata/hadoop/data/spark
$ cat << EOF > /opt/bigdata/hadoop/data/spark/test1230-data
1,phone
2,music
3,apple
4,clothes
EOF
# 启动hive
$ hive
# 创建表,通过逗号分隔字段
create table test1230(id string,shop string) row format delimited fields terminated by ',';
# 从local加载数据,这里的local是指hs2服务所在机器的本地linux文件系统
load data local inpath '/opt/bigdata/hadoop/data/spark/test1230-data' into table test1230;
# 通过insert添加数据,会提交spark任务
select * from test1230;
select count(*) from test1230;
最后提供我上面编译好的spark2.3.0版本的包,下载地址如下:
链接:https://pan.baidu.com/s/1OY_Mn8UdRkTiiMktjQ3wlQ
提取码:8888
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。