SparkOnHive本地运行访问hive(IDEA)

本文档介绍了如何在IDEA中配置Spark项目以本地运行访问Hive,包括添加Spark-Hive依赖、配置hive-site.xml文件,并演示了读写Hive表的操作。配置包括设置JDBC连接到MySQL的Hive元数据存储,以及插入和查询数据的示例。
摘要由CSDN通过智能技术生成

SparkOnHive本地运行访问hive(IDEA)

1. 添加依赖

        <dependency>
            <groupId>org.apache.spark</groupId>
            <artifactId>spark-hive_2.11</artifactId>
            <version>${spark.version}</version>
            <scope>provided</scope>
        </dependency>

2. 配置文件
hive-site.xml文件内容如下:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<!--
   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
-->
<configuration>
  <property>
    <name>javax.jdo.option.ConnectionURL</name>
    <value>jdbc:mysql://192.168.60.66:3306/hivedb?createDatabaseIfNotExist=true</value>
    <description>JDBC connect string for a JDBC metastore</description>
  </property>

  <property>
    <name>javax.jdo.option.ConnectionDriverName</name>
    <value>com.mysql.jdbc.Driver</value>
    <description>Driver class name for a JDBC metastore</description>
  </property>

  <property>
    <name>javax.jdo.option.ConnectionUserName</name>
    <value>bigdata</value>
    <description>username to use against metastore database</description>
  </property>

  <property>
    <name>javax.jdo.option.ConnectionPassword</name>
    <value>123456</value>
    <description>password to use against metastore database</description>
  </property>
  
</configuration>

把hive-site.xml拷贝到Rsource目录下,如下图:

在这里插入图片描述

3. 对hive中的表进行读写操作示例

object SparkOnHiveDemo {
    def main(args: Array[String]): Unit = {
		//此时又可能会报权限不足的错   
		//org.apache.hadoop.ipc.RemoteException(org.apache.hadoop.security.AccessControlException): Permission denied:
		//需要在代码首行添加用户名
        System.setProperty("HADOOP_USER_NAME", "xxx")
		
        val ss = SparkSession
            .builder()
			.setAppName("SparkOnHiveDemo")
			.setMaster("local[*]")
            .enableHiveSupport()
            .getOrCreate()
			
        import ss.implicits._
        //ss.sql("use spark001")
  
		//使用 hive的insert 语句
		ss.sql("CREATE TABLE IF NOT EXISTS spark001.tb001 ( 
				id     string, 
				name   string
				)
				ROW FORMAT DELIMITED 
				FIELDS TERMINATED BY '\u0005' 
				COLLECTION ITEMS TERMINATED BY '\u0002' 
				MAP KEYS TERMINATED BY '\u0003' 
				STORED AS  TEXTFILE;")
				
        ss.sql("insert into table tb001 values (5,'yy')")
        ss.sql("select * from tb001").show()
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不会飞的乌龟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值