win7(64位)
cygwin 1.7.9-1
jdk-6u25-windows-x64.zip
hadoop-0.20.2.tar.gz
1.安装jdk,并置java环境变量包括:JAVA_HOME,PATH,CLASSPATH
2.安装Hadoop,版本为0.20.2,我是直接放到/home目录下,并解压
tar –zxvf
hadoop-0.20.2.tar.gz
3.配置Hadoop,需要修改hadoop的配置文件,它们位于conf子目录下,分别是hadoop-env.sh、core-site.xml、hdfs-site.xml
和mapred-site.xml
(1) 修改hadoop-env.sh:
只需要将JAVA_HOME 修改成JDK 的安装目录即可
export
JAVA_HOME=/cygdrive/d/java/jdk1.6.0_25
(注意:路径不能是windows 风格的目录d:\java\jdk1.6.0_25,而是LINUX
风格/cygdrive/d/java/jdk1.6.0_25)
(2) 修改core-site.xml:(指定namenode)
<configuration>
<property>
<name>fs.default.name</name>
<value>hdfs://localhost:9000</value>
</property>
</configuration>
(3)修改hdfs-site.xml(指定副本为1)
<configuration>
<property>
<name>dfs.replication</name>
<value>1</value>
</property>
</configuration>
(4) 修改mapred-site.xml (指定jobtracker)
<configuration>
<property>
<name>mapred.job.tracker</name>
<value>localhost:9001</value>
</property>
</configuration>
4.验证安装是否成功,并运行Hadoop
(1) 验证安装
$ bin/hadoop
Usage: hadoop [--config confdir] COMMAND
where COMMAND is one of:
namenode -format format the DFS filesystem
secondarynamenoderun the DFS secondary namenode
namenode run the DFS namenode
datanode run a DFS datanode
dfsadmin run a DFS admin client
mradmin run a Map-Reduce admin client
fsck run a DFS filesystem checking utility
fs run a generic filesystem user client
balancer run a cluster balancing utility
jobtracker run the MapReduce job Tracker node
pipesrun a Pipes job
tasktracker run a MapReduce task Tracker node
job manipulate MapReduce jobs
queueget information regarding JobQueues
version print the version
jar <jar> run a jar file
distcp <srcurl><desturl>copy file or directories recursively
archive -archiveName NAME <src>* <dest>create a hadoop archive
daemonlogget/set the log level for each daemon
or
CLASSNAMErun the class named CLASSNAME
Most commands print help when invoked w/o parameters.
(2) 格式化并启动Hadoop
bin/hadoop namenode –format
bin/start-all.sh
(3) 查看Hadoop
命令行查看:
$ jps
1608 NameNode
6572 Jps
6528 JobTracker
(注意:win7下cygwin中DateNode和TaskTracker进程是无法显示的,好像是cygwin的问题)
在上一篇博文中,散仙已经讲了Hadoop的单机伪分布的部署,本篇,散仙就说下,如何eclipse中调试hadoop2.2.0,如果你使用的还是hadoop1.x的版本,那么,也没事,散仙在以前的博客里,也写过eclipse调试1.x的hadoop程序,两者最大的不同之处在于使用的eclipse插件不同,hadoop2.x与hadoop1.x的API,不太一致,所以插件也不一样,我们只需要使用分别对应的插件即可.
下面开始进入正题:
序号 名称 描述
1 eclipse Juno Service Release 4.2的本
2 *** 作系统 Windows7
3 hadoop的eclipse插件 hadoop-eclipse-plugin-2.2.0.jar
4 hadoop的集群环境 虚拟机Linux的Centos6.5单机伪分布式
5 调试程序 Hellow World
遇到的几个问题如下:
Java代码
java.io.IOException: Could not locate executable null\bin\winutils.exe in the Hadoop binaries.
解决办法:
在org.apache.hadoop.util.Shell类的checkHadoopHome()方法的返回值里写固定的
本机hadoop的路径,散仙在这里更改如下:
Java代码
private static String checkHadoopHome() {
// first check the Dflag hadoop.home.dir with JVM scope
//System.setProperty("hadoop.home.dir", "...")
String home = System.getProperty("hadoop.home.dir")
// fall back to the system/user-global env variable
if (home == null) {
home = System.getenv("HADOOP_HOME")
}
try {
// couldn't find either setting for hadoop's home directory
if (home == null) {
throw new IOException("HADOOP_HOME or hadoop.home.dir are not set.")
}
if (home.startsWith("\"") && home.endsWith("\"")) {
home = home.substring(1, home.length()-1)
}
// check that the home setting is actually a directory that exists
File homedir = new File(home)
if (!homedir.isAbsolute() || !homedir.exists() || !homedir.isDirectory()) {
throw new IOException("Hadoop home directory " + homedir
+ " does not exist, is not a directory, or is not an absolute path.")
}
home = homedir.getCanonicalPath()
} catch (IOException ioe) {
if (LOG.isDebugEnabled()) {
LOG.debug("Failed to detect a valid hadoop home directory", ioe)
}
home = null
}
//固定本机的hadoop地址
home="D:\\hadoop-2.2.0"
return home
}
第二个异常,Could not locate executable D:\Hadoop\tar\hadoop-2.2.0\hadoop-2.2.0\bin\winutils.exe in the Hadoop binaries. 找不到win上的执行程序,可以去下载bin包,覆盖本机的hadoop跟目录下的bin包即可
第三个异常:
Java代码
Exception in thread "main" java.lang.IllegalArgumentException: Wrong FS: hdfs://192.168.130.54:19000/user/hmail/output/part-00000, expected: file:///
at org.apache.hadoop.fs.FileSystem.checkPath(FileSystem.java:310)
at org.apache.hadoop.fs.RawLocalFileSystem.pathToFile(RawLocalFileSystem.java:47)
at org.apache.hadoop.fs.RawLocalFileSystem.getFileStatus(RawLocalFileSystem.java:357)
at org.apache.hadoop.fs.FilterFileSystem.getFileStatus(FilterFileSystem.java:245)
at org.apache.hadoop.fs.ChecksumFileSystem$ChecksumFSInputChecker.<init>(ChecksumFileSystem.java:125)
at org.apache.hadoop.fs.ChecksumFileSystem.open(ChecksumFileSystem.java:283)
at org.apache.hadoop.fs.FileSystem.open(FileSystem.java:356)
at com.netease.hadoop.HDFSCatWithAPI.main(HDFSCatWithAPI.java:23)
出现这个异常,一般是HDFS的路径写的有问题,解决办法,拷贝集群上的core-site.xml和hdfs-site.xml文件,放在eclipse的src根目录下即可。
第四个异常:
Java代码
Exception in thread "main" java.lang.UnsatisfiedLinkError: org.apache.hadoop.io.nativeio.NativeIO$Windows.access0(Ljava/lang/StringI)Z
出现这个异常,一般是由于HADOOP_HOME的环境变量配置的有问题,在这里散仙特别说明一下,如果想在Win上的eclipse中成功调试Hadoop2.2,就需要在本机的环境变量上,添加如下的环境变量:
(1)在系统变量中,新建HADOOP_HOME变量,属性值为D:\hadoop-2.2.0.也就是本机对应的hadoop目录
(2)在系统变量的Path里,追加%HADOOP_HOME%/bin即可
以上的问题,是散仙在测试遇到的,经过对症下药,我们的eclipse终于可以成功的调试MR程序了,散仙这里的Hellow World源码如下:
Java代码
package com.qin.wordcount
import java.io.IOException
import org.apache.hadoop.fs.FileSystem
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.mapred.JobConf
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
/***
*
* Hadoop2.2.0测试
* 放WordCount的例子
*
* @author qindongliang
*
* hadoop技术交流群: 376932160
*
*
* */
public class MyWordCount {
/**
* Mapper
*
* **/
private static class WMapper extends Mapper<LongWritable, Text, Text, IntWritable>{
private IntWritable count=new IntWritable(1)
private Text text=new Text()
@Override
protected void map(LongWritable key, Text value,Context context)
throws IOException, InterruptedException {
String values[]=value.toString().split("#")
//System.out.println(values[0]+"========"+values[1])
count.set(Integer.parseInt(values[1]))
text.set(values[0])
context.write(text,count)
}
}
/**
* Reducer
*
* **/
private static class WReducer extends Reducer<Text, IntWritable, Text, Text>{
private Text t=new Text()
@Override
protected void reduce(Text key, Iterable<IntWritable> value,Context context)
throws IOException, InterruptedException {
int count=0
for(IntWritable i:value){
count+=i.get()
}
t.set(count+"")
context.write(key,t)
}
}
/**
* 改动一
* (1)shell源码里添加checkHadoopHome的路径
* (2)974行,FileUtils里面
* **/
public static void main(String[] args) throws Exception{
// String path1=System.getenv("HADOOP_HOME")
// System.out.println(path1)
// System.exit(0)
JobConf conf=new JobConf(MyWordCount.class)
//Configuration conf=new Configuration()
//conf.set("mapred.job.tracker","192.168.75.130:9001")
//读取person中的数据字段
// conf.setJar("tt.jar")
//注意这行代码放在最前面,进行初始化,否则会报
/**Job任务**/
Job job=new Job(conf, "testwordcount")
job.setJarByClass(MyWordCount.class)
System.out.println("模式: "+conf.get("mapred.job.tracker"))
// job.setCombinerClass(PCombine.class)
// job.setNumReduceTasks(3)//设置为3
job.setMapperClass(WMapper.class)
job.setReducerClass(WReducer.class)
job.setInputFormatClass(TextInputFormat.class)
job.setOutputFormatClass(TextOutputFormat.class)
job.setMapOutputKeyClass(Text.class)
job.setMapOutputValueClass(IntWritable.class)
job.setOutputKeyClass(Text.class)
job.setOutputValueClass(Text.class)
String path="hdfs://192.168.46.28:9000/qin/output"
FileSystem fs=FileSystem.get(conf)
Path p=new Path(path)
if(fs.exists(p)){
fs.delete(p, true)
System.out.println("输出路径存在,已删除!")
}
FileInputFormat.setInputPaths(job, "hdfs://192.168.46.28:9000/qin/input")
FileOutputFormat.setOutputPath(job,p )
System.exit(job.waitForCompletion(true) ? 0 : 1)
}
}
控制台,打印日志如下:
Java代码
INFO - Configuration.warnOnceIfDeprecated(840) | mapred.job.tracker is deprecated. Instead, use mapreduce.jobtracker.address
模式: local
输出路径存在,已删除!
INFO - Configuration.warnOnceIfDeprecated(840) | session.id is deprecated. Instead, use dfs.metrics.session-id
INFO - JvmMetrics.init(76) | Initializing JVM Metrics with processName=JobTracker, sessionId=
WARN - JobSubmitter.copyAndConfigureFiles(149) | Hadoop command-line option parsing not performed. Implement the Tool interface and execute your application with ToolRunner to remedy this.
WARN - JobSubmitter.copyAndConfigureFiles(258) | No job jar file set. User classes may not be found. See Job or Job#setJar(String).
INFO - FileInputFormat.listStatus(287) | Total input paths to process : 1
INFO - JobSubmitter.submitJobInternal(394) | number of splits:1
INFO - Configuration.warnOnceIfDeprecated(840) | user.name is deprecated. Instead, use mapreduce.job.user.name
INFO - Configuration.warnOnceIfDeprecated(840) | mapred.output.value.class is deprecated. Instead, use mapreduce.job.output.value.class
INFO - Configuration.warnOnceIfDeprecated(840) | mapred.mapoutput.value.class is deprecated. Instead, use mapreduce.map.output.value.class
INFO - Configuration.warnOnceIfDeprecated(840) | mapreduce.map.class is deprecated. Instead, use mapreduce.job.map.class
INFO - C
若在windows的Eclipse工程中直接启动mapreduc程序,需要先把hadoop集群的配置目录下的xml都拷贝到src目录下,让程序自动读取集群的地址后去进行分布式运行(您也可以自己写java代码去设置job的configuration属性)。若不拷贝,工程中bin目录没有完整的xml配置文件,则windows执行的mapreduce程序全部通过本机的jvm执行,作业名也是带有“local"字眼的作业,如 job_local2062122004_0001。 这不是真正的分布式运行mapreduce程序。
估计得研究org.apache.hadoop.conf.Configuration的源码,反正xml配置文件会影响执行mapreduce使用的文件系统是本机的windows文件系统还是远程的hdfs系统还有影响执行mapreduce的mapper和reducer的是本机的jvm还是集群里面机器的jvm
二、 本文的结论
第一点就是: windows上执行mapreduce,必须打jar包到所有slave节点才能正确分布式运行mapreduce程序。(我有个需求是要windows上触发一个mapreduce分布式运行)
第二点就是: Linux上,只需拷贝jar文件到集群master上,执行命令hadoop jarPackage.jar MainClassName即可分布式运行mapreduce程序。
第三点就是: 推荐使用附一,实现了自动打jar包并上传,分布式执行的mapreduce程序。
附一、 推荐使用此方法:实现了自动打jar包并上传,分布式执行的mapreduce程序:
请先参考博文五篇:
Hadoop作业提交分析(一)~~(五)
引用博文的附件中EJob.java到你的工程中,然后main中添加如下方法和代码。
public static File createPack() throws IOException {
File jarFile = EJob.createTempJar("bin")
ClassLoader classLoader = EJob.getClassLoader()
Thread.currentThread().setContextClassLoader(classLoader)
return jarFile
}
在作业启动代码中使用打包:
Job job = Job.getInstance(conf, "testAnaAction")
添加:
String jarPath = createPack().getPath()
job.setJar(jarPath)
即可实现直接run as java application 在windows跑分布式的mapreduce程序,不用手工上传jar文件。
附二、得出结论的测试过程
(未有空看书,只能通过愚笨的测试方法得出结论了)
一. 直接通过windows上Eclipse右击main程序的java文件,然后"run as application"或选择hadoop插件"run on hadoop"来触发执行MapReduce程序的测试。
1,如果不打jar包到进集群任意linux机器上,它报错如下:
[work] 2012-06-25 15:42:47,360 - org.apache.hadoop.mapreduce.Job -10244 [main] INFO org.apache.hadoop.mapreduce.Job - map 0% reduce 0%
[work] 2012-06-25 15:42:52,223 - org.apache.hadoop.mapreduce.Job -15107 [main] INFO org.apache.hadoop.mapreduce.Job - Task Id : attempt_1403517983686_0056_m_000000_0, Status : FAILED
Error: java.lang.RuntimeException: java.lang.ClassNotFoundException: Class bookCount.BookCount$BookCountMapper not found
at org.apache.hadoop.conf.Configuration.getClass(Configuration.java:1720)
at org.apache.hadoop.mapreduce.task.JobContextImpl.getMapperClass(JobContextImpl.java:186)
at org.apache.hadoop.mapred.MapTask.runNewMapper(MapTask.java:721)
at org.apache.hadoop.mapred.MapTask.run(MapTask.java:339)
at org.apache.hadoop.mapred.YarnChild$2.run(YarnChild.java:162)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.Subject.doAs(Subject.java:415)
at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1491)
at org.apache.hadoop.mapred.YarnChild.main(YarnChild.java:157)
Caused by: java.lang.ClassNotFoundException: Class bookCount.BookCount$BookCountMapper not found
at org.apache.hadoop.conf.Configuration.getClassByName(Configuration.java:1626)
at org.apache.hadoop.conf.Configuration.getClass(Configuration.java:1718)
... 8 more
# Error:后重复三次
2012-06-25 15:44:53,234 - org.apache.hadoop.mapreduce.Job -37813 [main] INFO org.apache.hadoop.mapreduce.Job - map 100% reduce 100%
现象就是:报错,无进度,无运行结果。
2,拷贝jar包到“只是”集群master的$HADOOP_HOME/share/hadoop/mapreduce/目录上,直接通过windows的eclipse "run as application"和通过hadoop插件"run on hadoop"来触发执行,它报错同上。
现象就是:报错,无进度,无运行结果。
3,拷贝jar包到集群某些slave的$HADOOP_HOME/share/hadoop/mapreduce/目录上,直接通过windows的eclipse "run as application"和通过hadoop插件"run on hadoop"来触发执行
和报错:
Error: java.lang.RuntimeException: java.lang.ClassNotFoundException: Class bookCount.BookCount$BookCountMapper not found
at org.apache.hadoop.conf.Configuration.getClass(Configuration.java:1720)
at org.apache.hadoop.mapreduce.task.JobContextImpl.getMapperClass(JobContextImpl.java:186)
和报错:
Error: java.lang.RuntimeException: java.lang.ClassNotFoundException: Class bookCount.BookCount$BookCountReducer not found
现象就是:有报错,但仍然有进度,有运行结果。
4,拷贝jar包到集群所有slave的$HADOOP_HOME/share/hadoop/mapreduce/目录上,直接通过windows的eclipse "run as application"和通过hadoop插件"run on hadoop"来触发执行:
现象就是:无报错,有进度,有运行结果。
第一点结论就是: windows上执行mapreduce,必须打jar包到所有slave节点才能正确分布式运行mapreduce程序。
二 在Linux上的通过以下命令触发MapReduce程序的测试。
hadoop jar $HADOOP_HOME/share/hadoop/mapreduce/bookCount.jar bookCount.BookCount
1,只拷贝到master,在master上执行。
现象就是:无报错,有进度,有运行结果。
2,拷贝随便一个slave节点,在slave上执行。
现象就是:无报错,有进度,有运行结果。
但某些节点上运行会报错如下,且运行结果。:
14/06/25 16:44:02 INFO mapreduce.JobSubmitter: Cleaning up the staging area /tmp/hadoop-yarn/staging/hduser/.staging/job_1403517983686_0071
Exception in thread "main" java.lang.NoSuchFieldError: DEFAULT_MAPREDUCE_APPLICATION_CLASSPATH
at org.apache.hadoop.mapreduce.v2.util.MRApps.setMRFrameworkClasspath(MRApps.java:157)
at org.apache.hadoop.mapreduce.v2.util.MRApps.setClasspath(MRApps.java:198)
at org.apache.hadoop.mapred.YARNRunner.createApplicationSubmissionContext(YARNRunner.java:443)
at org.apache.hadoop.mapred.YARNRunner.submitJob(YARNRunner.java:283)
at org.apache.hadoop.mapreduce.JobSubmitter.submitJobInternal(JobSubmitter.java:415)
at org.apache.hadoop.mapreduce.Job$10.run(Job.java:1268)
at org.apache.hadoop.mapreduce.Job$10.run(Job.java:1265)
at java.security.AccessController.doPrivileged(Native Method)
at javax.security.auth.Subject.doAs(Subject.java:415)
at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1491)
at org.apache.hadoop.mapreduce.Job.submit(Job.java:1265)
at org.apache.hadoop.mapreduce.Job.waitForCompletion(Job.java:1286)
at com.etrans.anaSpeed.AnaActionMr.run(AnaActionMr.java:207)
at org.apache.hadoop.util.ToolRunner.run(ToolRunner.java:70)
at com.etrans.anaSpeed.AnaActionMr.main(AnaActionMr.java:44)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.hadoop.util.RunJar.main(RunJar.java:212)
第二点结论就是: Linux上,只需拷贝jar文件到集群master上,执行命令hadoop jarPackage.jar MainClassName即可分布式运行mapreduce程序。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)