spark sql on hive配置及其使用

来源:互联网 发布:java.io.eofexception 编辑:程序博客网 时间:2024/04/20 00:57

 spark on hive 配置

1. 切换sparconf目录下使用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(可以创建很多hiveContexthivecongtext连接的是数据仓库,程序本身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()

 

 

 

 

0 0
原创粉丝点击