Hadoop所用安装包和配置文件等我找到最方便使用的方式再上传到博客,如果有需要也欢迎找我分享。
在windows下,
我用的是破解版的文件,不需要安装,直接解压到D盘下
我的电脑——>属性——>高级系统设置——>环境变量——>系统变量——>编辑Path,增加 %HADOOP_HOME%\bin ——>新建一个变量名为 HADOOP_HOME,变量值为 hadoop安装路径的系统变量——>确定操作——>完成
找到系统变量,编辑Path
加上 %HADOOP_HOME%\bin即可
确定之后,新建一个系统变量
然后依次确定刚才的操作,配置环境变量工作完成。
路径:C:\Windows\System32
先在本地电脑写一个txt文件,内容随便输入,
如:
(检测这里将程序复制过去就可以,先不用理解,后续学习)
package com.oracle.demo.mr; 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.output.FileOutputFormat; import java.io.IOException; public class WordCount { public class WcMapper extends Mapper<LongWritable,Text,Text,IntWritable> { @Override protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { String line = value.toString(); String[] strs = line.split(" "); for (String s:strs){ Text outkey = new Text(s); IntWritable outvalue = new IntWritable(1); context.write(outkey,outvalue); } } } public class WcReduce extends Reducer<Text,IntWritable,Text,IntWritable> { @Override protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { int count = 0; for (IntWritable n:values){ count += n.get(); } context.write(key,new IntWritable(count)); } } public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException { Job job = Job.getInstance(); job.setMapOutputKeyClass(Text.class); job.setMapOutputValueClass(IntWritable.class); job.setMapOutputKeyClass(Text.class); job.setMapOutputValueClass(IntWritable.class); job.setMapperClass(com.oracle.demo.mr.WcMapper.class); job.setReducerClass(com.oracle.demo.mr.WcReduce.class); FileInputFormat.setInputPaths(job,new Path("E:\\BigData\\input.txt")); FileOutputFormat.setOutputPath(job,new Path("E:\\BigData\\output")); job.waitForCompletion(true); } }
注意:
运行之后控制台显示:
。。。
。。。
表示运行成功,没有错误
结果是:
此刻,表示Hadoop安装成功,大功告成了。
Exception in thread “main” org.apache.hadoop.mapred.FileAlreadyExistsException: Output directory file:/E:/BigData/output already exists
如何解决:之前运行的输出文件删除即可。
这篇博客是我自己安装完之后写出来的,如果过程中有什么疏漏或者疑问,欢迎和我交流。安装过程中也许会遇到一些自己解决不了的错误,不要急躁,慢慢找方法解决就好了,希望你能成为一个优秀的程序员。