spark打包jar连接hive数据源完整过程

1:启动hive.metastore

nohup hive --service metastore &

2:启动spark中thriftserver
说明:经过测试如果直接启动hive中的hiveserver2然后启动thriftserver会报冲突,猜测spark中已经包含了hiveserver2
所以无需在hive中启动hiveserver2,主要目的是方便在datagrid中远程访问如图:
在这里插入图片描述

./start-thriftserver.sh

3:启动spark master,和slaver主要用于打包好的jar包执行

./start-master.sh -h 本机IP地址
./start-slave.sh spark://本机IP地址:7077

查看是否启动成功 jps -m 如下图所示
在这里插入图片描述
4:编写java测试代码

public class SparkSqlToHive {
    public static void main(String[] args) {
        SparkSession session = SparkSession.builder().appName("SparkSessionApp")
                .config("hive.metastore.uris", "thrift://127.0.0.1:9083")
                //直接连接hive
                .enableHiveSupport()
                .getOrCreate();

        session.sql("show databases").show();
    }
}

注意如果事先在开发工具中开发打jar包麻烦,可以直接远程连接调试。代码如下

public class SparkSqlToHive {
    public static void main(String[] args) {
        SparkSession session = SparkSession.builder().appName("SparkSessionApp")
                .master("spark://172.10.70.196:7077")
                .config("hive.metastore.uris", "thrift://172.10.70.196:9083")
                //直接连接hive
                .enableHiveSupport()
                .getOrCreate();

        session.sql("show databases").show();
    }
}

5:打包 pom文件实例

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.example</groupId>
  <artifactId>SparkTest</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>SparkTest</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.apache.spark</groupId>
      <artifactId>spark-sql_2.11</artifactId>
      <version>2.4.8</version>
    </dependency>
    <dependency>
      <groupId>org.apache.spark</groupId>
      <artifactId>spark-hive_2.11</artifactId>
      <version>2.4.8</version>
    </dependency>
    <dependency>
      <groupId>org.spark-project.hive</groupId>
      <artifactId>hive-jdbc</artifactId>
      <version>1.2.1.spark2</version>
    </dependency>
  </dependencies>

  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>net.alchim31.maven</groupId>
          <artifactId>scala-maven-plugin</artifactId>
          <version>3.2.2</version>
        </plugin>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.5.1</version>
        </plugin>
      </plugins>
    </pluginManagement>
    <plugins>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <executions>
          <execution>
            <phase>compile</phase>
            <goals>
              <goal>compile</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>2.4.3</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <filters>
                <filter>
                  <artifact>*:*</artifact>
                  <excludes>
                    <exclude>META-INF/*.SF</exclude>
                    <exclude>META-INF/*.DSA</exclude>
                    <exclude>META-INF/*.RSA</exclude>
                  </excludes>
                </filter>
              </filters>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

6:将打包好的jar包上传服务器运行

spark-submit --class org.example.SparkSqlToHive --master spark://第三步启动master指定的IP地址:7077 /usr/local/src/test/SparkTest-1.0-SNAPSHOT.jar

7:查看执行结果
在这里插入图片描述
在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

sunnyboy_4

你的鼓励是我创作的动力

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

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

打赏作者

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

抵扣说明:

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

余额充值