Hadoop開發(fā)--IDEA(三)

一、插件安裝

  1. 源碼地址
    https://github.com/fangyuzhong2016/HadoopIntellijPlugin
    代碼下載
git clone https://github.com/fangyuzhong2016/HadoopIntellijPlugin.git

注意:從Github上下載的源碼需要經(jīng)過編譯才能使用

  1. 編譯
    ①、目前 Intellij plugin for hadoop 的源碼使用maven 進(jìn)行編譯和打包,因此在編譯之前請確保安裝JDK1.8和maven3 以上版本。
    ②、Intellij plugin for hadoop插件基于 IntelliJ IDEA Ultimate 2017.2 版本進(jìn)行開發(fā)的,因此需要安裝IntelliJ IDEAUltimate 2017 以上版本
    ③、進(jìn)入源碼目錄 ../HadoopIntellijPlugin/ 修改 pom.xml 文件,主要修改hadoop的版本和IntelliJ IDEA 安裝的路徑,設(shè)置如下:
# 修改properties 版本內(nèi)容與idea安裝路徑
    <!--設(shè)置hadoop版本-->
    <hadoop.2.version>3.0.0-alpha2</hadoop.2.version>
    <!--設(shè)置Intellij-IDEA的安裝路徑-->
    <IntellijIde.dir>C:\Program Files\JetBrains\IntelliJ IDEA 2018.2</IntellijIde.dir>

④、執(zhí)行mvn 命令:
先執(zhí)行

C:\Users\Administrator>d:
D:\>cd HadoopIntellijPlugin
D:\HadoopIntellijPlugin>mvn clean
mvn clean
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  40.727 s
[INFO] Finished at: 2019-11-21T13:54:47+08:00
[INFO] ------------------------------------------------------------------------

然后執(zhí)行

D:\HadoopIntellijPlugin>mvn assembly:assembly
[INFO] Reading assembly descriptor: assembly.xml
[INFO] artifact net.minidev:json-smart: checking for updates from aliyun-repos
[INFO] artifact net.minidev:json-smart: checking for updates from central
[INFO] artifact net.minidev:json-smart: checking for updates from dynamodb-local-oregon
[INFO] artifact net.minidev:json-smart: checking for updates from apache.snapshots.https
[INFO] artifact net.minidev:json-smart: checking for updates from repository.jboss.org
[INFO] HadoopIntellijPlugin/lib/HadoopIntellijPlugin-1.0.jar already added, skipping
[INFO] Building zip: D:\HadoopIntellijPlugin\target\HadoopIntellijPlugin-1.0.zip
[INFO] HadoopIntellijPlugin/lib/HadoopIntellijPlugin-1.0.jar already added, skipping
[INFO]
[INFO] <<< maven-assembly-plugin:2.2-beta-5:assembly (default-cli) < package @ HadoopIntellijPlugin <<<
[INFO]
[INFO]
[INFO] --- maven-assembly-plugin:2.2-beta-5:assembly (default-cli) @ HadoopIntellijPlugin ---
[INFO] Reading assembly descriptor: assembly.xml
[INFO] HadoopIntellijPlugin/lib/HadoopIntellijPlugin-1.0.jar already added, skipping
[INFO] Building zip: D:\HadoopIntellijPlugin\target\HadoopIntellijPlugin-1.0.zip
[INFO] HadoopIntellijPlugin/lib/HadoopIntellijPlugin-1.0.jar already added, skipping
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  01:00 min
[INFO] Finished at: 2019-11-21T16:28:22+08:00
[INFO] ------------------------------------------------------------------------

編譯完成后 在.../target/HadoopIntellijPlugin-1.0.zip 即為該插件的安裝包,然后安裝到 IntelliJ 中即可。
D:\HadoopIntellijPlugin\target


生成文件
  1. 安裝HadoopIntellijPlugin


    安裝plugin

    安裝plugin
  2. 修改GUI
    再次打開IDEA。由于用的是idea的框架。所以idea的ui要改成idea的動態(tài)生成插入,在這里設(shè)置:在Setting里面找到GUI Designer


    修改GUI

    GUI文件系統(tǒng)
  3. HDFS設(shè)置

    設(shè)置

    設(shè)置

    參數(shù)填寫:
    只需要填寫HDFS地址即可
    注意:測試功能似乎不完善,會提示連接失敗,但是直接點確定就可以正常使用。

  4. 每次更改文件,可能都需要以用戶登錄權(quán)限,比較麻煩。這個可以配置,在hdfs-site.xml來配置

<property>
    <name>dfs.permissions</name>
    <value>false</value>
</property>

常見問題:

  1. 測試提示失敗,但可以正常使用。


    連接失敗

    在源碼的:

com.fangyuzhong.intelliJ.hadoop.fsconnection.ConnectionManager類中。

二、Intellij plugin for hadoop 插件配置和源碼的相關(guān)說明

1、插件的源碼說明

插件的源碼結(jié)構(gòu)

①、core 包,為插件項目的核心包,公共組件庫,包括了 通用UI界面、多線程操作、Hadoop連接設(shè)置基類、Hadoop文件系統(tǒng)通用操作類、插件項目設(shè)置通用類和其他工具類
②、fsconnection 包,Hadoop文件系統(tǒng)連接實現(xiàn)類和連接相關(guān)配置實現(xiàn)類
③、fsobject 包,文件系統(tǒng)對象類的實現(xiàn)(對于HDFS來講就是 目錄樹和文件樹節(jié)點的組織方式的實現(xiàn))
④、fsbrowser包,插件的主界面實現(xiàn),包括讀取HDFS文件系統(tǒng)相關(guān)數(shù)據(jù)進(jìn)行展示、文件系統(tǒng)對象的創(chuàng)建、下載、刪除、上傳和其他一些操作
⑤、globalization包,插件多語言支持類
⑥、options 包,插件設(shè)置類
⑦、mainmenu包, 插件主菜單操作類

2、插件配置相關(guān)說明
插件配置在.../resources/目錄下,包括HadoopNavigator_en_US.properties、HadoopNavigator_zh_CN.properties 、plugin.xml
HadoopNavigator_en_US.properties 文件為插件界面的英文語言配置
HadoopNavigator_zh_CN.properties 文件為插件界面的中文語言配置
目前插件界面的語言只支持 簡體中文和英文,其他的語言,需要自行制作語言包。系統(tǒng)初始默認(rèn)的語言為操作系統(tǒng)默認(rèn)的語言。

三、插件使用

  1. 創(chuàng)建目錄(測試不好使)


    創(chuàng)建目錄
  2. 下載文件


    下載文件
  3. 上傳文件


    上傳文件

四、Hadoop編程示例

  1. 創(chuàng)建項目


    創(chuàng)建項目

    選擇Maven

    設(shè)定包名

    指定Maven

    指定項目保存路徑
  2. 添加Maven依賴

<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>com.xtsz</groupId>
    <artifactId>hadoop-exercise</artifactId>
    <version>1.0.0</version>
    <name>hadoop-exercise</name>
    <url>hadoop練習(xí)</url>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <version.hadoop>2.9.2</version.hadoop>
    </properties>

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

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <appendAssemblyId>false</appendAssemblyId>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                    <archive>
                        <manifest>
                            <!-- 此處指定main方法入口的class -->
                            <mainClass>com.xtsz.WordCount</mainClass>
                        </manifest>
                    </archive>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>assembly</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>
  1. 代碼編寫
import java.io.IOException;
import java.util.StringTokenizer;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class WordCount {
    /**
     * Mapper
     */
    public static class TokenizerMapper
            extends Mapper<Object, Text, Text, IntWritable> {

        private final static IntWritable one = new IntWritable(1);
        private Text word = new Text();

        public void map(Object key, Text value, Context context
        ) throws IOException, InterruptedException {
            StringTokenizer itr = new StringTokenizer(value.toString());
            while (itr.hasMoreTokens()) {
                word.set(itr.nextToken());
                context.write(word, one);
            }
        }
    }

    /**
     * Reducer
     */
    public static class IntSumReducer
            extends Reducer<Text, IntWritable, Text, IntWritable> {
        private IntWritable result = new IntWritable();

        public void reduce(Text key, Iterable<IntWritable> values,
                           Context context
        ) throws IOException, InterruptedException {
            int sum = 0;
            for (IntWritable val : values) {
                sum += val.get();
            }
            result.set(sum);
            context.write(key, result);
        }
    }

    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf, "word count");
        job.setJarByClass(WordCount.class);
        job.setMapperClass(TokenizerMapper.class);
        job.setCombinerClass(IntSumReducer.class);
        job.setReducerClass(IntSumReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        FileInputFormat.addInputPath(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));
        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }
}

  1. 打包可執(zhí)行jar
    使用插件:
<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
        <appendAssemblyId>false</appendAssemblyId>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
        <archive>
            <manifest>
                <!-- 此處指定main方法入口的class -->
                <mainClass>com.xtsz.WordCount</mainClass>
            </manifest>
        </archive>
    </configuration>
    <executions>
        <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
                <goal>assembly</goal>
            </goals>
        </execution>
    </executions>
</plugin>
打包可執(zhí)行jar

打包結(jié)果
  1. 上傳運行


    上傳jar文件
root@master:~# hadoop jar hadoop-exercise-1.0.0.jar  hdfs://master:9000/wordcount hdfs://master:9000/output
運行結(jié)果

運行結(jié)果

運行結(jié)果
hello   7
jerry   1
jone    1
kitty   1
marquis 1
tom 2
world   1

五、非Maven方式導(dǎo)包

1. 新建Java項目

項目創(chuàng)建

創(chuàng)建項目

項目創(chuàng)建

創(chuàng)建包

添加包名

添加類

WordCount

2. 導(dǎo)入jar包

可以在hadoop的 share/hadoop 目錄下找到,找到module點擊右側(cè)的小加號JARS or directories…


手動導(dǎo)包

手動導(dǎo)包

手動導(dǎo)包

common:


common

comom/lib:
common/lib

hdfs:
hdfs

mapreduce:


mapreduce

yarn:
yarn

3. 代碼編寫

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;
import java.util.StringTokenizer;

public class WordCountTest {
    /**
     * Mapper
     */
    public static class TokenizerMapper
            extends Mapper<Object, Text, Text, IntWritable> {

        private final static IntWritable one = new IntWritable(1);
        private Text word = new Text();

        public void map(Object key, Text value, Context context
        ) throws IOException, InterruptedException {
            StringTokenizer itr = new StringTokenizer(value.toString());
            while (itr.hasMoreTokens()) {
                word.set(itr.nextToken());
                context.write(word, one);
            }
        }
    }

    /**
     * Reducer
     */
    public static class IntSumReducer
            extends Reducer<Text, IntWritable, Text, IntWritable> {
        private IntWritable result = new IntWritable();
        public void reduce(Text key, Iterable<IntWritable> values,
                           Context context
        ) throws IOException, InterruptedException {
            int sum = 0;
            for (IntWritable val : values) {
                sum += val.get();
            }
            result.set(sum);
            context.write(key, result);
        }
    }

    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf, "word count");
        job.setJarByClass(WordCountTest.class);
        job.setMapperClass(TokenizerMapper.class);
        job.setCombinerClass(IntSumReducer.class);
        job.setReducerClass(IntSumReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        FileInputFormat.setInputPaths(job, new Path("hdfs://192.168.71.130:9000/wordcount"));
        FileOutputFormat.setOutputPath(job, new Path("hdfs://192.168.71.130:9000/result"));
        System.exit(job.waitForCompletion(true) ? 0 : 1);
    }
}

4. 運行測試

運行測試
運行測試
結(jié)果

六、常見問題

  1. Unable to import maven project: See logs for details
    更換Maven版本為:3.5.4
  2. 清空日志
root@master:/usr/local/hadoop-2.9.2/logs# echo "">hadoop-root-namenode-master.log

  1. 打包插件
<plugin>
    <artifactId>maven-dependency-plugin</artifactId>
    <configuration>
        <!--依賴傳遞-->
        <excludeTransitive>false</excludeTransitive>
        <!-- 表示復(fù)制的jar文件添加版本信息 -->
        <stripVersion>true</stripVersion>
        <!--用來指定拷出后Libraries的存放地-->
        <outputDirectory>./lib</outputDirectory>
    </configuration>
</plugin>
  1. 可執(zhí)行Jar插件
<plugin>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
        <appendAssemblyId>false</appendAssemblyId>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
        <archive>
            <manifest>
                <!-- 此處指定main方法入口的class -->
                <mainClass>com.xtsz.WordCount</mainClass>
            </manifest>
        </archive>
    </configuration>
    <executions>
        <execution>
            <id>make-assembly</id>
            <phase>package</phase>
            <goals>
                <goal>assembly</goal>
            </goals>
        </execution>
    </executions>
</plugin>
  1. java.lang.InterruptedException
    當(dāng)關(guān)閉DFSStripedOutputStream的時候,如果在向data/parity塊刷回數(shù)據(jù)失敗的時候,streamer線程不會被關(guān)閉。同時在DFSOutputStream#closeImpl中也存在這個問題。
    DFSOutputStream#closeImpl總是會強制性地關(guān)閉線程,會引起InterruptedException。
  2. 缺少tools.jar
<dependency>
    <groupId>jdk.tools</groupId>
    <artifactId>jdk.tools</artifactId>
    <version>1.8</version>
    <scope>system</scope>
    <systemPath>${JAVA_HOME}/lib/tools.jar</systemPath>
</dependency>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 第一部分 插件的介紹 Google 在2013年5月的I/O開發(fā)者大會推出了基于IntelliJ IDEA Jav...
    Jannonx閱讀 2,416評論 0 13
  • 概述 IDEA(https://www.jetbrains.com/idea/),全稱IntelliJ IDEAJ...
    lxtyp閱讀 3,055評論 0 3
  • 前言:IntelliJ IDEA 如果說IntelliJ IDEA是一款現(xiàn)代化智能開發(fā)工具的話,Eclipse則稱...
    guodd369閱讀 6,062評論 0 8
  • 知乎上有個高熱度的問題 :“你身邊都有哪些,常見的道德綁架?”。 其中提到了中國式的八大寬容:大過年的、人都死了、...
    凡心一墨閱讀 1,959評論 15 32
  • 3.2注釋 最常用的方式為//當(dāng)需要長篇注釋時,可以在每行注釋前面標(biāo)記//,也可使用/*和 */將注釋括起來當(dāng)需要...
    隱巖閱讀 249評論 0 1

友情鏈接更多精彩內(nèi)容