您的位置:首页 > 数据库

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()

 

 

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: