Spark兼容Hive入门解析
2016-05-21 22:46
441 查看
最近使用spark兼容hive进行开发,所以静下心来简易的看了一遍。个人感觉有关与技术学习去官网是最正确的选择。所以我从官方网站入手开始讲,其他详细内容我不做过多介绍,这里直接开门见山。
官方网站有如下两点很重要:
Spark SQL also supports reading and writing data stored in Apache Hive. However, since
Hive has a large number of dependencies, it is not included in the default Spark assembly. Hive support is enabled by adding the
to Spark’s build. This command builds a new assembly jar that includes Hive. Note that this Hive assembly jar must also be present on all of the worker nodes, as they will need access to the Hive serialization and deserialization libraries (SerDes) in order
to access data stored in Hive.
spark sql同样可以支持读取并且写入到hive中数据。然而,由于hive拥有许多依赖,hive并没有被包含在默认的组件里面。hive插件可以通过增加-Phive 和 -Phive-thriftserver 标签在你build本spark的时候。这个build命令会产生一个新的插件包含hive。注意,这个hive控件包必须被放在所有的worker的节点下,因为他们需要hive序列化和反序列化(SerDes)的权限从而获得拿取存储在hive中数据的权限。
Configuration of Hive is done by placing your
security configuration),
file in
the
and
need to be available on the driver and all executors launched by the YARN cluster. The convenient way to do this is adding them through the
and
配置hive需要把你的hive-site.xml,core-site.xml,hdfs-site.xml 放入到你的conf/文件夹下面。注意,当你在yarn的集群上运行查询(命令)的时候,存放在lib目录下的datanucleus的jar包以及存放在conf下的hive-site.xml 配置文件需要对driver以及所有的yarn集群启动的executors有效。最简单的方式就是通过加入--jar
和--file操作在spark-submit命令中。
以上是官方的解释[我其实很不明白csdn赋值内容为什么总在一行,自己调格式很难看],那么我本地进行了一下实验。首先我这里假设你已经具备了上述的所有条件,我的spark的conf、lib文件夹如下:
这里的hdfs-site.xml、core-site.xml 是从hadoop的配置文件中传过来的,而hive-site.xml文件是从hive的配置文件中传过来的。那么我们启动我们的hdfs、spark、mysql,具体操作我不解释。spark的启动后,我们使用如下命令进入shell中:
启动后我们进入spark的shell命令窗口。在命令窗口中我们使用如下命令:
工作空间有些杂乱,大家不必关系这里有什么表,只要记住这里没有person表,因为接下来我就要创建person的表,此时我在spark的shell中创建表格:
没毛病,person表已经加入,我们查看hdfs,person表格也已经出现,如下图:
这样说明我们spark兼容hive已经可以使用,更多详细内容大家可以上spark官方网站查看,好了,到此关于spark兼容hive入门介绍就到此为止了。
感谢开源,让技术的大门向你我展开。
官方网站有如下两点很重要:
Spark SQL also supports reading and writing data stored in Apache Hive. However, since
Hive has a large number of dependencies, it is not included in the default Spark assembly. Hive support is enabled by adding the
-Phiveand
-Phive-thriftserverflags
to Spark’s build. This command builds a new assembly jar that includes Hive. Note that this Hive assembly jar must also be present on all of the worker nodes, as they will need access to the Hive serialization and deserialization libraries (SerDes) in order
to access data stored in Hive.
spark sql同样可以支持读取并且写入到hive中数据。然而,由于hive拥有许多依赖,hive并没有被包含在默认的组件里面。hive插件可以通过增加-Phive 和 -Phive-thriftserver 标签在你build本spark的时候。这个build命令会产生一个新的插件包含hive。注意,这个hive控件包必须被放在所有的worker的节点下,因为他们需要hive序列化和反序列化(SerDes)的权限从而获得拿取存储在hive中数据的权限。
Configuration of Hive is done by placing your
hive-site.xml,
core-site.xml(for
security configuration),
hdfs-site.xml(for HDFS configuration)
file in
conf/. Please note when running the query on a YARN cluster (
clustermode),
the
datanucleusjars under the
libdirectory
and
hive-site.xmlunder
conf/directory
need to be available on the driver and all executors launched by the YARN cluster. The convenient way to do this is adding them through the
--jarsoption
and
--fileoption of the
spark-submitcommand.
配置hive需要把你的hive-site.xml,core-site.xml,hdfs-site.xml 放入到你的conf/文件夹下面。注意,当你在yarn的集群上运行查询(命令)的时候,存放在lib目录下的datanucleus的jar包以及存放在conf下的hive-site.xml 配置文件需要对driver以及所有的yarn集群启动的executors有效。最简单的方式就是通过加入--jar
和--file操作在spark-submit命令中。
以上是官方的解释[我其实很不明白csdn赋值内容为什么总在一行,自己调格式很难看],那么我本地进行了一下实验。首先我这里假设你已经具备了上述的所有条件,我的spark的conf、lib文件夹如下:
这里的hdfs-site.xml、core-site.xml 是从hadoop的配置文件中传过来的,而hive-site.xml文件是从hive的配置文件中传过来的。那么我们启动我们的hdfs、spark、mysql,具体操作我不解释。spark的启动后,我们使用如下命令进入shell中:
bin/spark-shell \ --master spark://sun:7077 \ --executor-memory 1g \ --total-executor-cores 2 \ --driver-class-path /home/hadoop/app/spark1.6/lib/mysql-connector-java-5.1.35-bin.jar这里简单介绍一下,第二列是指定我的spark的master节点所在,第三列指的是我为每个executor分配多大内存,记住是每个。第四列指的是我总共分配多少个内核,最后一个指定mysql的驱动包所在。
启动后我们进入spark的shell命令窗口。在命令窗口中我们使用如下命令:
import org.apache.spark.sql.hive.HiveContext val hiveContext = new HiveContext(sc)此时我们已经成功的连接到hive上,如何证明我们确实已经可以使用spark-hive了呢。我们来查看一下hive在mysql表中的所有表格信息,如下图所示:
工作空间有些杂乱,大家不必关系这里有什么表,只要记住这里没有person表,因为接下来我就要创建person的表,此时我在spark的shell中创建表格:
hiveContext.sql("create table person(id int,name varchar(20),age int)")运行完毕后,我们查看mysql表格,如下:
没毛病,person表已经加入,我们查看hdfs,person表格也已经出现,如下图:
这样说明我们spark兼容hive已经可以使用,更多详细内容大家可以上spark官方网站查看,好了,到此关于spark兼容hive入门介绍就到此为止了。
感谢开源,让技术的大门向你我展开。
相关文章推荐
- Spark RDD API详解(一) Map和Reduce
- 使用spark和spark mllib进行股票预测
- 分享Hive的一份胶片资料
- Spark随谈——开发指南(译)
- Spark,一种快速数据分析替代方案
- Hadoop生态上几个技术的关系与区别:hive、pig、hbase 关系与区别
- hadoop的hdfs文件操作实现上传文件到hdfs
- java连接hdfs ha和调用mapreduce jar示例
- java实现将ftp和http的文件直接传送到hdfs
- eclipse 开发 spark Streaming wordCount
- Understanding Spark Caching
- ClassNotFoundException:scala.PreDef$
- Windows 下Spark 快速搭建Spark源码阅读环境
- 在Hadoop2.5.0下利用Java读写HDFS
- HDFS 文件操作
- Spark中将对象序列化存储到hdfs
- 使用java代码提交Spark的hive sql任务,run as java application
- Spark机器学习(一) -- Machine Learning Library (MLlib)
- Spark机器学习(二) 局部向量 Local-- Data Types - MLlib