hadoop搭建与eclipse开发环境设置已验证通过Word格式.docx
《hadoop搭建与eclipse开发环境设置已验证通过Word格式.docx》由会员分享,可在线阅读,更多相关《hadoop搭建与eclipse开发环境设置已验证通过Word格式.docx(11页珍藏版)》请在冰豆网上搜索。
![hadoop搭建与eclipse开发环境设置已验证通过Word格式.docx](https://file1.bdocx.com/fileroot1/2022-12/29/c741e9bd-9a6c-47b4-8180-14307552107a/c741e9bd-9a6c-47b4-8180-14307552107a1.gif)
然后是打开“Advancedparameters”设置面板,修改相应参数。
上面的参数填写以后,也会反映到这里相应的参数:
主要关注下面几个参数:
fs.defualt.name:
mapred.job.tracker:
dfs.replication:
与hdfs-site.xml里面的dfs.replication一致。
hadoop.tmp.dir:
与core-site.xml里hadoop.tmp.dir设置一致。
hadoop.job.ugi:
并不是设置用户名与密码。
是用户与组名,所以这里填写hadoop,hadoop。
说明:
第一次设置的时候可能是没有hadoop.job.ugi和dfs.replication参数的,不要紧,确认保存。
打开ProjectExplorer中DFS Locations目录,应该可以年看到文件系统中的结构了。
但是在/hadoop/mapred/system下却没有查看权限,如下图:
而且删除文件的时候也会报错:
这个原因是我使用地本用户Administrator(我是用管理员用户登陆来地windows系统的)进行远程hadoop系统操作,没有权限。
此时再打开“Advancedparameters”设置面板,应该可以看到hadoop.job.ugi了,这个参数默认是本地操作系统的用户名,如果不幸与远程hadoop用户不一致,那就要改过来了,将hadoop加在第一个,并用逗号分隔。
如:
保存配置后,重新启动eclipse。
/hadoop/mapred/system下就一目了然了,删除文件也OK。
1.3运行hadoop程序
首先将hadoop安装包下面的所有jar包都导到eclipse工程里。
然后建立一个类:
DFSOperator.java,该类写了四个基本方法:
创建文件,删除文件,把文件内容读为字符串,将字符串写入文件。
同时有个main函数,可以修改测试:
packagecom.kingdee.hadoop;
importjava.io.BufferedReader;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.InputStreamReader;
importorg.apache.hadoop.conf.Configuration;
importorg.apache.hadoop.fs.FSDataOutputStream;
importorg.apache.hadoop.fs.FileSystem;
importorg.apache.hadoop.fs.Path;
/**
*
*Theutilitiestooperatefileonhadoophdfs.
*@authorluolihui2011-07-18
*
*/
publicclassDFSOperator{
privatestaticfinalStringROOT_PATH="
hdfs:
///"
;
privatestaticfinalintBUFFER_SIZE=4096;
/**
*construct.
publicDFSOperator(){}
*Createafileonhdfs.Therootpathis/.<
br>
*forexample:
DFSOperator.createFile("
/lory/test1.txt"
true);
*@parampaththefilenametoopen
*@paramoverwriteifafilewiththisnamealreadyexists,theniftrue,thefilewillbe
*@returntrueifdeleteissuccessfulelseIOException.
*@throwsIOException
publicstaticbooleancreateFile(Stringpath,booleanoverwrite)throwsIOException
{
//Stringuri="
//192.168.1.100:
9000"
//FileSystemfs1=FileSystem.get(URI.create(uri),conf);
Configurationconf=newConfiguration();
FileSystemfs=FileSystem.get(conf);
Pathf=newPath(ROOT_PATH+path);
fs.create(f,overwrite);
fs.close();
returntrue;
}
*Deleteafileonhdfs.Therootpathis/.<
DFSOperator.deleteFile("
/user/hadoop/output"
*@parampaththepathtodelete
*@paramrecursiveifpathisadirectoryandsettotrue,thedirectoryisdeletedelsethrowsanexception.Incaseofafiletherecursivecanbesettoeithertrueorfalse.
publicstaticbooleandeleteFile(Stringpath,booleanrecursive)throwsIOException
fs.delete(f,recursive);
*Readafiletostringonhadoophdfs.Fromstreamtostring.<
System.out.println(DFSOperator.readDFSFileToString("
/user/hadoop/input/test3.txt"
));
*@parampaththepathtoread
*@returntrueifreadissuccessfulelseIOException.
publicstaticStringreadDFSFileToString(Stringpath)throwsIOException
InputStreamin=null;
Stringstr=null;
StringBuildersb=newStringBuilder(BUFFER_SIZE);
if(fs.exists(f))
{
in=fs.open(f);
BufferedReaderbf=newBufferedReader(newInputStreamReader(in));
while((str=bf.readLine())!
=null)
{
sb.append(str);
sb.append("
\n"
);
}
in.close();
bf.close();
fs.close();
returnsb.toString();
}
else
returnnull;
*Writestringtoahadoophdfsfile.<
DFSOperator.writeStringToDFSFile("
"
Youareabadman.\nReally!
*@parampaththefilewherethestringtowritein.
*@paramstringthecontexttowriteinafile.
*@returntrueifwriteissuccessfulelseIOException.
publicstaticbooleanwriteStringToDFSFile(Stringpath,Stringstring)throwsIOException
FSDataOutputStreamos=null;
os=fs.create(f,true);
os.writeBytes(string);
os.close();
publicstaticvoidmain(String[]args)
try{
DFSOperator.createFile("
DFSOperator.deleteFile("
/dfs_operator.txt"
DFSOperator.writeStringToDFSFile("
Youareabadman.\nReally?
System.out.println(DFSOperator.readDFSFileToString("
}catch(IOExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
System.out.println("
===end==="
}
然后RunAsRunonHadoopChooseanexitsingserverfromthelistbelowfinish.
结果很简单(那个警告不管):
11/07/1618:
44:
32WARNconf.Configuration:
DEPRECATED:
hadoop-site.xmlfoundintheclasspath.Usageofhadoop-site.xmlisdeprecated.Insteadusecore-site.xml,mapred-site.xmlandhdfs-site.xmltooverridepropertiesofcore-default.xml,mapred-default.xmlandhdfs-default.xmlrespectively
Youareabadman.
Really?
===end===
也可以运行hadoop自带的WorkCount程序,找到其源代码导进来,然后设置输入输出参数,然后同样“Runonhadoop”。
具体步骤不再示范。
每“Runonhadoop”都会在workspace\.metadata\.plugins\org.apache.hadoop.eclipse下生成临时jar包。
不过第一次需要Runonhadoop,以后只需要点击那运行的绿色按钮了。
2.错误及处理
2.1安全模式问题
我在eclipse上删除DFS上的文件夹时,出现下面错误:
错误提示说得也比较明示,是NameNode在安全模式中,其解决方案也一并给出。
类似的运行hadoop程序时,有时候会报以下错误:
org.apache.hadoop.dfs.SafeModeException:
Cannotdelete/user/hadoop/input.Namenodeisinsafemode
解除安全模式:
bin/hadoopdfsadmin-safemodeleave
用户可以通过dfsadmin-safemodevalue
来操作安全模式,参数value的说明如下:
enter-进入安全模式
leave-强制NameNode离开安全模式
get-
返回安全模式是否开启的信息
wait-等待,一直到安全模式结束。
2.2开发时报错Permissiondenied
org.apache.hadoop.security.AccessControlException:
org.apache.hadoop.security.AccessControlException:
Permissiondenied:
user=Administrator,access=WRITE,inode="
test1.txt"
:
hadoop:
supergroup:
rw-r--r--
atsun.reflect.NativeConstructorAccessorImpl.newInstance0(NativeMethod)
atsun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:
39)
atsun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:
27)
atjava.lang.reflect.Constructor.newInstance(Constructor.java:
513)
atorg.apache.hadoop.ipc.RemoteException.instantiateException(RemoteException.java:
96)
atorg.apache.hadoop.ipc.RemoteException.unwrapRemoteException(RemoteException.java:
58)
atorg.apache.hadoop.hdfs.DFSClient$DFSOutputStream.<
init>
(DFSClient.java:
2710)
atorg.apache.hadoop.hdfs.DFSClient.create(DFSClient.java:
492)
atorg.apache.hadoop.hdfs.DistributedFileSystem.create(DistributedFileSystem.java:
195)
atorg.apache.hadoop.fs.FileSystem.create(FileSystem.java:
484)
465)
372)
atcom.kingdee.hadoop.DFSOperator.createFile(DFSOperator.java:
46)
atcom.kingdee.hadoop.DFSOperator.main(DFSOperator.java:
134)
解决方法是,在“Advancedparameters”设置面板,设置hadoop.job.ugi参数,将hadoop用户加上去。
变为:
然后重新在运行中”Runonhadoop”。
另一方法是改变要操作的文件的权限。
Permissiondenied:
上面的意思是:
test1.txt文件的访问权限是rw-r--r--,归属组是supergroup,归属用户是hadoop,现在使用Administrator用户对test1.txt文件进行WRITE方式访问,被拒绝了。
所以可以改变下test1.txt文件的访问权限:
$hadoopfs–chmod777/lory/test1.txt
$hadoopfs–chmod777/lory#或者上一级文件夹
当然使用-chown命令也可以。