配色: 字号:
java结合HADOOP集群文件上传下载
2017-03-31 | 阅:  转:  |  分享 
  
java结合HADOOP集群文件上传下载这篇文章主要介绍了java结合HADOOP集群文件上传下载的方法和示例,非常的实用,这里推荐给大家,
希望大家能够喜欢。对HDFS上的文件进行上传和下载是对集群的基本操作,在《HADOOP权威指南》一书中,对文件的上传和下载都有代码
的实例,但是对如何配置HADOOP客户端却是没有讲得很清楚,经过长时间的搜索和调试,总结了一下,如何配置使用集群的方法,以及自己测
试可用的对集群上的文件进行操作的程序。首先,需要配置对应的环境变量:复制代码代码如下:hadoop_HOME="/home/wo
rk/tools/java/hadoop-client/hadoop"forfin$hadoop_HOME/hadoop-
.jar;dohadoop_CLASSPATH=${hadoop_CLASSPATH}:$fdoneforfin$hado
op_HOME/lib/.jar;dohadoop_CLASSPATH=${hadoop_CLASSPATH}:$fdoneh
adoopvfs_HOME="/home/work/tools/java/hadoop-client/hadoop-vfs"for
fin$hadoopvfs_HOME/lib/.jar;dohadoop_CLASSPATH=${hadoop_CLAS
SPATH}:$fdoneexportLD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/work/t
ools/java/hadoop-client/hadoop/lib/native/Linux-amd64-64/其中LD_LIB
RARY_PATH是在调用时需要用到的库的路径,hadoop_CLASSPATH则是我们hadoop客户端里各种jar包有一点需要
注意的是最好不要使用HADOOP_HOME这个变量,这个是一个系统使用的环境变量,最好不要和它冲突编译类的方法:复制代码代码如下
:javac-classpath$CLASSPATH:$hadoop_CLASSPATHHDFSUtil.java运行的方法
:复制代码代码如下:java-classpath$CLASSPATH:$hadoop_CLASSPATHHDFSUtil但
是在实际的使用过程中,会报NoPermission之类的错误,或者你能保证代码没有问题的情况下,在运行的时候也会报一些奇奇怪怪的
错误那么问题来了,这是什么鬼?答案:这是因为没有配置对应集群的配置文件因为在《HADOOP权威指南》一书中,弱化了配置的东西,所以
在具体使用集群的时候就会出现问题,如何解决呢,这样子:复制代码代码如下:this.conf=newConfiguratio
n(false);conf.addResource("./hadoop-site.xml");conf.addResource("
./hadoop-default.xml");conf.set("fs.hdfs.impl",org.apache.hadoop
.hdfs.DistributedFileSystem.class.getName());conf.set("fs.file.im
pl",org.apache.hadoop.fs.LocalFileSystem.class.getName());为什么会这样
,书上只是很简单的:this.conf=newConfiguration();那是因为默认你的集群在本地,所以不需要做配置,
但是在实际使用的过程中,各个集群的配置是不同的,所以我们要引入集群的配置这是非常重要的一点,因为实际使用的过程中我们都是使用的HA
DOOP的客户端,而且是已经搭好环境的集群,所以我们需要做好本地的配置hadoop-site.xml和hadoop-default
.xml这两个文件在所使用的客户端的conf目录下,在addResource的时候指定好目录就行了将以上所提到的配置,全部配完之后
,这个程序才能真正运行起来,所以配置是非常重要的一环。以下是对应的工具的代码,有兴趣的看一下吧,使用的是文件流的方式来搞的,这样子
也可以打通FTP和HDFS之间文件的互传:http://www.jb51.net/article/62822.htm?123456
78910111213141516171819202122232425262728293031323334353637383940
41424344454647484950515253545556575859606162636465666768697071727
37475767778798081828384858687888990919293949596979899100101102103
10410510610710810911011111211311411511611711811912012112212312412
51261271281291301311321331341351361371381391401411421431441451461
47148149150151152153154155156157158159160161162163164165166167168
16917017117217317417517617717817918018118218318418518618718818919
0191192193194195importjava.io.BufferedInputStream;importjava.io
.FileInputStream;importjava.io.FileNotFoundException;importjava
.io.FileOutputStream;importjava.io.IOException;importjava.io.In
putStream;importjava.io.OutputStream;importjava.net.URI;import
java.net.URL;importjava.io.;importorg.apache.hadoop.conf.Confi
guration;importorg.apache.hadoop.fs.FSDataInputStream;importorg
.apache.hadoop.fs.FileSystem;importorg.apache.hadoop.fs.Path;imp
ortorg.apache.hadoop.io.IOUtils;importorg.apache.hadoop.util.Pr
ogressable;publicclassHDFSUtil{privateStringhdfs_node="";p
rivateStringhdfs_path="";privateStringfile_path="";privat
eStringhadoop_site="";privateStringhadoop_default="";priv
ateConfigurationconf=null;publicHDFSUtil(Stringhdfs_node){
this.hdfs_node=hdfs_node;}publicStringgetHdfsNode(){returnt
his.hdfs_node;}publicvoidsetHdfsPath(Stringhdfs_path){this.hdf
s_path=hdfs_path;}publicStringgetHdfsPath(){returnthis.hdfs_
path;}publicvoidsetFilePath(Stringfile_path){this.file_path=
file_path;}publicStringgetFilePath(){returnthis.file_path;}pub
licvoidsetHadoopSite(Stringhadoop_site){this.hadoop_site=had
oop_site;}publicStringgetHadoopSite(){returnthis.hadoop_site;}
publicvoidsetHadoopDefault(Stringhadoop_default){this.hadoop_d
efault=hadoop_default;}publicStringgetHadoopDefault(){return
this.hadoop_default;}publicintsetConfigure(booleanflag){if(f
lag==false){if(this.getHadoopSite()==""||this.getHadoopDef
ault()==""){return-1;}else{this.conf=newConfiguration(fals
e);conf.addResource(this.getHadoopDefault());conf.addResource(thi
s.getHadoopSite());conf.set("fs.hdfs.impl",org.apache.hadoop.hdf
s.DistributedFileSystem.class.getName());conf.set("fs.file.impl",
org.apache.hadoop.fs.LocalFileSystem.class.getName());return0;}
}this.conf=newConfiguration();return0;}publicConfigurationg
etConfigure(){returnthis.conf;}publicintupLoad(StringlocalNa
me,StringremoteName)throwsFileNotFoundException,IOException
{InputStreaminStream=null;FileSystemfs=null;try{inStream=
newBufferedInputStream(newFileInputStream(localName));fs=File
System.get(URI.create(this.hdfs_node),this.conf);OutputStreamou
tStream=fs.create(newPath(remoteName),newProgressable(){pub
licvoidprogress(){System.out.print(''.'');}});IOUtils.copyBytes(i
nStream,outStream,4096,true);inStream.close();return0;}catch
(IOExceptione){inStream.close();e.printStackTrace();return-1;}
}publicintupLoad(InputStreaminStream,StringremoteName)throw
sFileNotFoundException,IOException{FileSystemfs=null;try{fs
=FileSystem.get(URI.create(this.hdfs_node),this.conf);OutputSt
reamoutStream=fs.create(newPath(remoteName),newProgressable
(){publicvoidprogress(){System.out.print(''.'');}});IOUtils.copy
Bytes(inStream,outStream,4096,true);inStream.close();return0;
}catch(IOExceptione){inStream.close();e.printStackTrace();retu
rn-1;}}publicintdonwLoad(StringremoteName,StringlocalName,
intlines)throwsFileNotFoundException,IOException{FileOutputS
treamfos=null;InputStreamReaderisr=null;BufferedReaderbr=
null;Stringstr=null;OutputStreamWriterosw=null;BufferedWri
terbuffw=null;PrintWriterpw=null;FileSystemfs=null;Input
StreaminStream=null;try{fs=FileSystem.get(URI.create(this.h
dfs_node+remoteName),this.conf);inStream=fs.open(newPath(th
is.hdfs_node+remoteName));fos=newFileOutputStream(localName)
;osw=newOutputStreamWriter(fos,"UTF-8");buffw=newBufferedW
riter(osw);pw=newPrintWriter(buffw);isr=newInputStreamReade
r(inStream,"UTF-8");br=newBufferedReader(isr);while((str=br
.readLine())!=null&&lines>0){lines--;pw.println(str);}}cat
ch(IOExceptione){thrownewIOException("Couldn''twrite.",e);}
finally{pw.close();buffw.close();osw.close();fos.close();inStrea
m.close()}return0;}//maintotestpublicstaticvoidmain(String[
]args){Stringhdfspath=null;Stringlocalname=null;Stringhdf
snode=null;intlines=0;if(args.length==4){hdfsnode=args[
0];hdfspath=args[1];localname=args[2];lines=Integer.parseIn
t(args[3]);}else{hdfsnode="hdfs://nj01-nanling-hdfs.dmop.baidu.
com:54310";hdfspath="/app/ps/spider/wdmqa/wangweilong/test/HDFS
Util.java";localname="/home/work/workspace/project/dhc2-0/dhc/b
ase/ftp/papapa";lines=5;}HDFSUtilhdfsutil=newHDFSUtil(hdfsnode);hdfsutil.setFilePath(hdfsutil.getHdfsNode()+hdfspath);hdfsutil.setHadoopSite("./hadoop-site.xml");hdfsutil.setHadoopDefault("./hadoop-default.xml");hdfsutil.setConfigure(false);try{hdfsutil.donwLoad(hdfspath,localname,lines);}catch(IOExceptione){e.printStackTrace();}}如果想要了解FTP上文件的下载,请参考这篇文章:http://www.jb51.net/article/62819.htmftp下载工具如果想要打通FTP和HDFS文件互传,只要创建一个类,调用这两篇文章中的工具的接口就可以搞定,自己写的代码,实测有效。
献花(0)
+1
(本文系关平藏书首藏)