1. 程式人生 > >使用idea 構建hive的自定義函式,無法打包成jar包的問題解決

使用idea 構建hive的自定義函式,無法打包成jar包的問題解決

在昨天 晚上 遇到了用idea 打包自己的自定義函式成jar,怎麼都不行,翻查了很多部落格,發現CSDN上很多部落格都存在問題,而且還搜到很多部落格錯的一樣,但是博主名字 不一樣的情況,差點把我心態搞崩。

這是當時遇到的問題,還有一個問題是 在hive裡面根據這個jar包建立函式的時候,還會報 你自己寫的主類找不到的問題。

經過很多測試,我自己終於解決了這個問題。

下面是我的解決辦法,注:錯誤是千奇百怪的,也許我的解決辦法並不能解決你的問題。

首先 這是我 自定義 函式 是使用了maven,下面是我的pom.xml 檔案

<?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>hive</groupId>
  <artifactId>hiveUdf</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>hiveUdf</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>

    <hadoop.version>2.5.0</hadoop.version>
    <hive.version>0.13.1</hive.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

    <!-- hadoop client -->
    <dependency>
      <groupId>org.apache.hadoop</groupId>
      <artifactId>hadoop-client</artifactId>
      <version>${hadoop.version}</version>
    </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.10</version>
    </dependency>

    <!-- hive client -->
    <dependency>
      <groupId>org.apache.hive</groupId>
      <artifactId>hive-jdbc</artifactId>
      <version>${hive.version}</version>
    </dependency>
    <dependency>
      <groupId>org.apache.hive</groupId>
      <artifactId>hive-exec</artifactId>
      <version>${hive.version}</version>
    </dependency>
  </dependencies>

  <build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>

        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
        <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>

      </plugins>
    </pluginManagement>
  </build>
</project>

 

然後,我們在main資料夾下,建一個resources 資原始檔夾。你自己建的時候,這個資料夾肯定是空的。

然後我們按下圖步驟點開

同樣,我們按照以下步驟。

 

點選下圖所示

 

注意,我們自定義的hive的函式 是沒有 main方法的,我自己自定義的 hive函式是這樣的

接著上面的步驟,沒有main方法,這塊紅色區域,搜不到我們寫的類

 

所以我們點選右邊的選項卡。

 

在我們點選ok 的時候會報錯。因為我們的自定義hive函式沒有main方法,所以報錯。但是是並不影響,這裡報錯是沒關係的。

 

我們在點選下彈出的錯誤提示框的 ok,再點選取消。回到剛才的地方,發現 雖然報錯,不過 還是把我們自定義的類載入進來了。

 

然後依次按我下圖所示。

選擇我們之前建立的resources 資料夾

此時我們就會發現,在resources 資料夾下 ,多了些東西。

 然後,按下圖

點選build,後就idea就開始自動為我們建立jar包了。

然後我們在out 資料夾下就能找到。

 

既然jar包 打好了,就能把它匯入你的hive裡面了。