idea编写spark连接hive

  • 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>

猜你喜欢

转载自blog.csdn.net/bb23417274/article/details/87969927