本文实例环境:
Ubuntu14.0.4 Hadoop 2.7.2
运行在本地模式
本文是根据对网上【作者:给力星】的文件的实践,作者原文在:
Hadoop 2.x 版本中 jar 不再集中在一个 hadoop-core*.jar 中,而是分成多个 jar,如使用 Hadoop 2.6.0 运行 WordCount 实例至少需要如下三个 jar:
实际上,通过命令 hadoop classpath
我们可以得到运行 Hadoop 程序所需的全部 classpath 信息。
我们要将 Hadoop 的 classhpath 信息添加到 CLASSPATH 变量中,在 ~/.bashrc 中增加如下几行:
export HADOOP_HOME=/usr/local/hadoop
export CLASSPATH=$($HADOOP_HOME/bin/hadoop classpath):$CLASSPATH
另外:这里我是将整个hadoop classpath(就是把执行此命令后的输出的全部内容)的内容添加到CLASSPATH中:
必须要把以上两个变量加到~/.bashrc文件中,保存后,别忘了执行 source ~/.bashrc
使变量生效。
1、编译:通过 javac
命令编译 WordCount.java :
cb@Standalone14:~/Documents/javaT$ ls WordCount.java cb@Standalone14:~/Documents/javaT$ javac WordCount.java cb@Standalone14:~/Documents/javaT$ ls WordCount.class WordCount$IntSumReducer.class WordCount.java WordCount$TokenizerMapper.class
2、打包:把 .class 文件打包成 jar:
cb@Standalone14:~/Documents/javaT$ jar -cvf WordCount.jar ./WordCount*.class added manifest adding: WordCount.class(in = 1943) (out= 1013)(deflated 47%) adding: WordCount$IntSumReducer.class(in = 1793) (out= 751)(deflated 58%) adding: WordCount$TokenizerMapper.class(in = 1790) (out= 765)(deflated 57%) cb@Standalone14:~/Documents/javaT$ ls WordCount.class WordCount$IntSumReducer.class WordCount.jar WordCount.java WordCount$TokenizerMapper.class
3、执行:执行jar文件中的MapReduce程序:
cb@Standalone14:~/Documents/javaT$ hadoop dfs -cat /user/cb/output/part-r-00000 DEPRECATED: Use of this script to execute hdfs command is deprecated. Instead use the hdfs command for it. echo 1 game 1 of 1 rainbow 1 the 2 waitting 1