spark sql on hive配置及其使用
2017-01-20 18:10
435 查看
一: spark on hive
配置
1. 切换到spar的conf目录下使用vi hive-site.xml创建hive-site.xml. 并填写如下内容
<configuration>
<property>
<name>hive.metastore.uris</name>
<value>thrift://master:9083</value>
<description>thrift URI for the remote metastore.Used by metastore client to connect to remote metastore. </description>
</property>
</configuration>
因为用sparksql操作hive实际上是把hive 当做数据仓库。数据仓库肯定有元数据和数据本身。要访问真正的数据就要访问他的元数据。所以只需要配置hive.metastore.uris
即可。(不需在每台机器上配置)
二:启动集群
1. 启动dfs 服务 start-dfs.sh
2. 启动hive 数据仓库服务 hive --service metastore >metastore.log 2>& 1&
3. 启动spark服务 start-all.sh
4. 启动sparkshell ./spark-shell –master spark://master:7077
三:案例实战
1. Spark on hive 实战
在spark-shell 模式下
Val hiveContext= new org.apache.spark.sql.hive.HiveContext(sc)
hiveContext.sql(“use hive”) //使用hive 数据库
hiveContext.sql("show tables").collect.foreach(println) // 查询数据库中的表
hiveContext.sql(“select count(*) from sogouq1”).collect.foreach(println)(注意此时使用的是spark的引擎)
hiveContext.sql(“select count(*) from sogouq2 where website like '%baidu%'”).collect.foreach(println)
hiveContext.sql(“select count(*) from sogouq2 where s_seq=1 and c_seq=1 and website like '%baidu%'”).collect.foreach(println)
2. 不基于hive 的实战代码,在spark-shell 模式下
scala> sqlContext
res8: org.apache.spark.sql.SQLContext =
org.apache.spark.sql.hive.HiveContext@35920655(可以创建很多hiveContext,hivecongtext连接的是数据仓库,程序本身(spark中的job),job在程序中并行运行,如果都hive的数据,如果用句柄,对句柄的占用比较麻烦,所有用多个实例。从查询的角度来说,有多个实例也很正常)
val df =sqlcontext.read.json(“library/examples/src/main/resources/people.json”) //读取json 数据
df.show()
df.printSchema
df.select(“name”).show()
df.select(df(“name”),df(“age”)+1).show()
配置
1. 切换到spar的conf目录下使用vi hive-site.xml创建hive-site.xml. 并填写如下内容
<configuration>
<property>
<name>hive.metastore.uris</name>
<value>thrift://master:9083</value>
<description>thrift URI for the remote metastore.Used by metastore client to connect to remote metastore. </description>
</property>
</configuration>
因为用sparksql操作hive实际上是把hive 当做数据仓库。数据仓库肯定有元数据和数据本身。要访问真正的数据就要访问他的元数据。所以只需要配置hive.metastore.uris
即可。(不需在每台机器上配置)
二:启动集群
1. 启动dfs 服务 start-dfs.sh
2. 启动hive 数据仓库服务 hive --service metastore >metastore.log 2>& 1&
3. 启动spark服务 start-all.sh
4. 启动sparkshell ./spark-shell –master spark://master:7077
三:案例实战
1. Spark on hive 实战
在spark-shell 模式下
Val hiveContext= new org.apache.spark.sql.hive.HiveContext(sc)
hiveContext.sql(“use hive”) //使用hive 数据库
hiveContext.sql("show tables").collect.foreach(println) // 查询数据库中的表
hiveContext.sql(“select count(*) from sogouq1”).collect.foreach(println)(注意此时使用的是spark的引擎)
hiveContext.sql(“select count(*) from sogouq2 where website like '%baidu%'”).collect.foreach(println)
hiveContext.sql(“select count(*) from sogouq2 where s_seq=1 and c_seq=1 and website like '%baidu%'”).collect.foreach(println)
2. 不基于hive 的实战代码,在spark-shell 模式下
scala> sqlContext
res8: org.apache.spark.sql.SQLContext =
org.apache.spark.sql.hive.HiveContext@35920655(可以创建很多hiveContext,hivecongtext连接的是数据仓库,程序本身(spark中的job),job在程序中并行运行,如果都hive的数据,如果用句柄,对句柄的占用比较麻烦,所有用多个实例。从查询的角度来说,有多个实例也很正常)
val df =sqlcontext.read.json(“library/examples/src/main/resources/people.json”) //读取json 数据
df.show()
df.printSchema
df.select(“name”).show()
df.select(df(“name”),df(“age”)+1).show()
相关文章推荐
- 【Spark篇】---SparkSQL on Hive的配置和使用
- 第57课:Spark SQL on Hive配置及实战
- hive-on-spark配置和使用方法
- 大数据IMF传奇行动绝密课程第57课:Spark SQL on Hive配置及实战
- Spark SQL on Hive配置
- SparkSQL on Hive配置与实战
- spark使用hive出错,添加以下配置
- SparkSQL On Yarn with Hive,操作和访问Hive表
- SparkSQL On Yarn with Hive,操作和访问Hive表
- SparkSQL On Yarn with Hive,操作和访问Hive表
- SparkSQL与Hive on Spark的比较
- SparkSQL On Yarn with Hive,操作和访问Hive表
- SparkSQL 初步应用(HiveContext使用)
- SQL中on和where的使用及其差异
- SparkSQL On Yarn with Hive,操作和访问Hive表
- SparkSQL On Yarn with Hive,操作和访问Hive表
- SparkSQL On Yarn with Hive,操作和访问Hive表
- Spark(Hive) SQL中UDF的使用(Python)
- Spark(Hive) SQL中UDF的使用(Python)
- 第57课 spark sql on hive实战