Hadoop学习笔记 6 - eclipse远程连接Hadoop

本文详细介绍如何在Eclipse中配置Hadoop环境并运行WordCount示例程序,包括下载插件、设置环境变量、配置Hadoop位置等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

eclipse连接Hadoop

1.下载Hadoop-eclipse-plugin-2.6.0.jar包放在eclipse目录的plugin文件夹下

jar包

2.下载hadoop-2.6.0解压,设置环境变量

新建一个HADOOP_HOME,路径为解压的路径
HADOOP_HOME

3.在Path变量后加上:%HADOOP_HOME%\bin

PATH

4.重启eclipse,点击Window>>Preference>>Hadoop Map/Reduce

eclipse1
地址为解压的路径

5.点击Window>>Show Viwe>>Other>>MapReduce Tools>>Map/Reduce Locations

eclipse2
右键New Hadoop Location
eclipse3
这两个要和core-site.xml中的一样,点击finish
右上角出现DFS Locations
eclipse4

运行一个WordCount项目

在eclipse中Ctrl+N,选择Map/Reduce Project
WordCount1
WordCount2
新建Class
WordCount3
编写WordCount代码:

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.LongWritable;
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.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;

public class WordCount {
        public static class WordCountMap extends
                        Mapper<LongWritable, Text, Text, IntWritable> {
                private final IntWritable one = new IntWritable(1);
                private Text word = new Text();

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

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

        public static void main(String[] args) throws Exception {
                Configuration conf = new Configuration();
                Job job = new Job(conf);
                job.setJarByClass(WordCount.class);
                job.setJobName("wordcount");
                job.setOutputKeyClass(Text.class);
                job.setOutputValueClass(IntWritable.class);
                job.setMapperClass(WordCountMap.class);
                job.setReducerClass(WordCountReduce.class);
                job.setInputFormatClass(TextInputFormat.class);
                job.setOutputFormatClass(TextOutputFormat.class);
                FileInputFormat.addInputPath(job, new Path("hdfs://192.168.2.172:9000/WordCountInput"));//和namenode的IP一致,此目录需手动创建并在其中放入要处理的文件
                FileOutputFormat.setOutputPath(job, new Path("hdfs://192.168.2.172:9000/WordCountOutput"));//此目录不需创建,会自动生成
                job.waitForCompletion(true);
        }
}

新建两个文件file1,file2
WordCount4
在hdfs上新建一个目录WordCountInput
WordCount5
将file1,file2上传至此目录
WordCount6
在eclipse上运行,右键WordCount>>Run As>>Run Configuration
WordCount7
WordCount8

运行成功!

在HDFS本地运行

这里直接使用Hadoop自带的wordcount程序:
hdfs1
hdfs2

可以看到运行成功后生成的文件
hdfs3

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值