idea编写spark连接hive

5 篇文章 0 订阅
  • idea下编码,测试连接hive,代码如下:
package com.ws.spark

import org.apache.spark.sql.DataFrame
import org.apache.spark.sql.hive.HiveContext
import org.apache.spark.{SparkConf, SparkContext}

object RangeTest {
  def main(args: Array[String]): Unit = {
    val sparkConf: SparkConf = new SparkConf().setAppName("RangeTest").setMaster("local")
    val sparkContext: SparkContext = new SparkContext(sparkConf)

    val hiveContext: HiveContext = new HiveContext(sparkContext)

    val dataFrame: DataFrame = hiveContext.sql("select * from ws.t_hive")

    dataFrame.show()

   // val sourceRdd: RDD[String] = sparkContext.textFile("hdfs://192.168.0.21:9000/test")

    //val result: RDD[(String, Int)] = sourceRdd.flatMap(_.split(" ")).map((_, 1)).reduceByKey(_ + _)

    //result.saveAsTextFile("hdfs://192.168.0.21:9000/result")

    sparkContext.stop()
  }

}

  • resource如下(不添加hive配置以及hadoop配置会报错)
    hdfs-site.xml(可有可无)
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<!--
  Licensed 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. See accompanying LICENSE file.
-->

<!-- Put site-specific property overrides in this file. -->

<configuration>
  <property>
	<name>dfs.replication</name>
    <value>3</value>
  </property>

  <property>
    <name>dfs.namenode.name.dir</name>
    <value>file:/usr/local/apps/hadoop-2.7.7/dfs/name</value>
  </property>

  <property>
    <name>dfs.datanode.data.dir</name>
    <value>file:/usr/local/apps/hadoop-2.7.7/dfs/data</value>
  </property>
  
    <property>
    <name>dfs.permissions</name>
    <value>false</value>
  </property>
</configuration>

hive-site.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>

<configuration>

  <property>
    <name>hive.metastore.local</name>
    <value>true</value>
    <description/>
  </property>

  <!--配置mysql-->
  <property>
    <name>javax.jdo.option.ConnectionURL</name>
    <value>jdbc:mysql://192.168.0.21:3306/hive?createDatabaseIfNotExist=true&amp;characterEncoding=UTF-8&amp;useSSL=false</value>
    <description/>
  </property>

  <property>
    <name>javax.jdo.option.ConnectionDriverName</name>
    <value>com.mysql.jdbc.Driver</value>
    <description/>
  </property>

  <property>
    <name>javax.jdo.option.ConnectionUserName</name>
    <value>root</value>
    <description/>
  </property>

  <property>
    <name>javax.jdo.option.ConnectionPassword</name>
    <value>Admin_123456</value>
    <description/>
  </property>

  <property>
    <name>hive.metastore.schema.verification.record.version</name>
    <value>false</value>
    <description>
      When true the current MS version is recorded in the VERSION table. If this is disabled and verification is
      enabled the MS will be unusable.
    </description>
  </property>

  <property>
    <name>hive.metastore.warehouse.dir</name>
    <value>/usr/local/apps/hive/warehouse</value>
    <description>location of default database for the warehouse</description>
  </property>
</configuration>
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值