idea连接hadoop集群并运行WordCount例子

前提:在linux系统中搭建好hadoop集群

  1. 安装
    解压即可,也可从linux中直接拉取过来
  2. 配置环境变量
    在这里插入图片描述可以只配箭头中的一项
  3. 创建maven项目导入依赖
<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.8.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-common</artifactId>
        <version>2.9.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-client</artifactId>
        <version>2.9.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-hdfs</artifactId>
        <version>2.9.2</version>
    </dependency>
</dependencies>
  1. 拷贝linux中配置好的hadoop配置文件:core.site.xml、hdfs.site.xml到resource目录下
    在这里插入图片描述
  2. 创建WordCount.java
public class WordCount {
        public static class TokenizerMapper extends Mapper<Object, Text, Text, IntWritable> {
            private final static IntWritable one = new IntWritable(1);
            private Text word = new Text();
            protected 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);
                }
            }
        }
        public static class IntSumReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
            private IntWritable result = new IntWritable();
            protected 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 IOException, ClassNotFoundException, InterruptedException {
            System.setProperty("HADOOP_USER_NAME", "root");
            Configuration conf = new Configuration();
            FileSystem fs = FileSystem.get(conf);
            String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
            if(otherArgs.length != 2){
                System.err.println("Usage: wordcount <in> <out>");
                System.exit(2);
            }
            Path outPath = new Path(otherArgs[1]);
            if(fs.exists(outPath)) fs.delete(outPath, true);
           // Job job = Job.getInstance(conf, "word count");
            Job job = Job.getInstance(conf);
            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(otherArgs[0]));
            FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
            System.exit(job.waitForCompletion(true) ? 0 : 1);
        }
}

注意:修改代码中的 root 为自己的hadoopUserName,这个可以在50070端口中查到!!!
在这里插入图片描述6. 配置
在这里插入图片描述
7. 运行
运行之后可以在50070端口中查看到结果:
在这里插入图片描述

可能会出现的错误:
①忘记改hadoop_user_name为自己的名儿,按照上面修改
②无法识别虚拟机的主机名,在如下文件中加入自己 虚拟机ip虚拟机主机名
在这里插入图片描述③出现空指针异常
在如下目录中加入hadoop.dll文件(可以直接百度下载到)
在这里插入图片描述

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值