eclipse远程连接Hadoop
发布日期:2021-05-20 12:02:13 浏览次数:34 分类:精选文章

本文共 6425 字,大约阅读时间需要 21 分钟。

Windows环境下Hadoop安装与Map/Reduce程序开发

搭建Hadoop环境并开发Map/Reduce程序是现代大数据处理的基础技能。本文将详细指导Windows环境下Hadoop的安装配置,以及使用Eclipse工具开发和运行Map/Reduce程序。


一、前期准备

Hadoop在Windows环境下的安装包为hadoop-2.7.3.tar.gz,压缩包包含必要的winutils.exe和hadoop.dll文件。

  • Java Development Kit (JDK) 配置

    确保安装了正确版本的JDK。

    • 32位系统:安装JDK 32位版本。
    • 64位系统:安装JDK 64位版本。
    • 不同版本位数必须匹配。
  • 环境变量配置

    • 路径变量:编辑系统路径,添加Hadoop安装目录。
    • HADOOP_HOME:设置为Hadoop安装目录,如D:\hadoop

  • 二、安装Hadoop插件

  • Eclipse安装

    首先确保已成功安装Eclipse。

  • 安装Hadoop插件

    下载hadoop-eclipse-plugin-2.7.3.jar,将其拖入Eclipse的Plugins文件夹内。

  • 插件激活

    重启Eclipse,确保插件正确加载。


  • 三、配置Hadoop环境

  • 环境变量配置

    • 打开系统属性,编辑Path变量,添加Hadoop安装目录。
    • 设置HADOOP_HOME环境变量,并在Path中添加%HADOOP_HOME%bin
  • Hadoop文件复制

    winutils.exehadoop.dll文件复制到C:\Windows\System32目录,与bin目录的同时复制到Hadoop安装目录的bin下。


  • 四、Map/Reduce配置

  • Eclipse设置

    • Eclipse > Windows > Preferences > Hadoop Map/Reduce
    • 设置Hadoop安装路径,与实际安装目录一致。
  • 配置Map/Reduce位置

    • 描述:配置Map/Reduce Master和DFS Master的主机名和端口,与Linux环境下的core-site.xml一致。
    • 注意:确保宿主机名正确,建议使用管理员身份编辑C:\Windows\System32\drivers\etc\hosts文件添加必要主机名。
  • 环境测试

    • 若配置成功,点击左侧的DFS Locations,右侧应显示HDFS集群目录,表明服务可用。

  • 五、Map/Reduce程序开发

    1. 创建项目

  • 打开Eclipse,选择File > New > Other,点击MapReduce Project选项。

  • 在项目创建界面中,设置项目名称并点击确定。

  • 2. 编写Map/Reduce程序(以WordCount为例)

  • 创建源文件

    在项目的src目录下创建包com.hadoop.test,编写以下类:

    package com.hadoop.test;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
    { private final IntWritable one = new IntWritable(1); private Text word = new Text(); @Override protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { String line = value.toString(); StringTokenizer tokenizer = new StringTokenizer(line); while (tokenizer.hasMoreTokens()) { word.set(tokenizer.nextToken()); context.write(word, one); } } } public static class WordCountReduce extends Reducer
    { @Override protected void reduce(Text key, Iterable
    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 = Job.create(jobConf); 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(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); job.waitForCompletion(true); }}
  • 文件上传

    word.txt文件上传至HDFS输入目录:

    hadoop fs -put word.txt /input

    或者通过Eclipse右键上传本地文件至HDFS目录。

  • 程序运行

    右键单击项目,选择Run as > Run Configurations,输入命令参数并启动程序。


  • 六、常见错误解决

  • winutils.exe或hadoop.dll版本不匹配

    下载正确的32位/64位版本,放在Hadoop的bin目录下。

  • UnsatisfiedLinkError

    确保复制的文件与JDK版本匹配,特别是hadoop.dll

  • 权限问题

    Using hadoop fs -chmod 777 /input 确保目录权限为777。


  • 七、遇到问题解决方法

  • 检查系统、JDK、Hadoop版本是否匹配。
  • 确保Hadoop环境变量正确配置。
  • 查看相关日志,排除路径或权限问题。

  • WordCount源码

    package com.hadoop.test;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
    { private final IntWritable one = new IntWritable(1); private Text word = new Text(); @Override protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { String line = value.toString(); StringTokenizer tokenizer = new StringTokenizer(line); while (tokenizer.hasMoreTokens()) { word.set(tokenizer.nextToken()); context.write(word, one); } } } public static class WordCountReduce extends Reducer
    { @Override protected void reduce(Text key, Iterable
    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 = Job.create(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(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); job.waitForCompletion(true); }}

    通过以上步骤,您已成功搭建Hadoop环境,并能够在Eclipse中开发并运行Map/Reduce程序。遇到问题时,请根据错误信息逐步排查并参考各个解决方法,确保最终实现Hadoop环境的顺利运行。

    上一篇:eclipse远程上传hdfs文件报错
    下一篇:Hadoop集群启动常见异常

    发表评论

    最新留言

    感谢大佬
    [***.8.128.20]2025年05月02日 22时27分35秒