A summary of the three methods: HDFs by byte copy-by-row copy (in Java IO also has the character copy, for the moment not mentioned)
Because HDFs comes with, do not know why some occasions can not be used, each time the number of downloads is not necessarily, so consider themselves in accordance with the Java way to replicate, there are 2nd, 3 ways.
Have time to study the IO, for example, for special files, file duplication will occur in different sizes. Over here
void Downloadfromhdfs (String hdfssrc, String localdst)//String hdfsdst = "hdfs://54.0.88.53:8020/user/flume/ syslognetwork/";//String localdir =" d://flume//";//download Single file public static Boolean Downloadfromhdfs (String hdfssrc, String localdst) {Configuration conf = new configuration (); Path DST = new path (HDFSSRC); try {path SRC = new Path (HDFSSRC); String Filename = Src.getname (). toString (); String local = Localdst + Filename; Path Dst = new Path (local); FileSystem fs = Filesystem.get (Uri.create (HDFSSRC), conf); Fsdatainputstream in = Fs.open (SRC); OutputStream output = new FileOutputStream (new File); Ioutils.copybytes (in, Output, 4096, true); System.out.print ("Download successed."); catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace (); System.out.print ("Download failed."); return false;} return true;} Download directory for all files, method 1:ioutils.copybytes or Copytolocalpublic static Boolean Downfromhdfsdir (String hdfssrc, String localdst) Throws IOException {Configuration conf = new CoNfiguration (); Path Dstpath = new Path (HDFSSRC); int i = 1; FileSystem fs = Filesystem.get (Uri.create (HDFSSRC), conf); try {String subpath = ""; filestatus[] flist = Fs.liststatus (Dstpath); for (Filestatus f:flist) {if (null! = f) {subpath = new StringBuffer (). Appen D (F.getpath (). GetParent ()). Append ("/"). Append (F.getpath (). GetName ()). ToString (); if (F.isdir ()) {Downfromhdfsdir ( Subpath, LOCALDST);} else {System.out.println ("/t/t" + subpath);//Hdfs://54.0.88.53:8020/path DST = new Path (subpath); i++; Fsdatainputstream in = null;outputstream output = null;try {Path SRC = new Path (subpath); String Filename = Src.getname (). toString (); String local = Localdst + Filename; Path Dst = new Path (local); FileSystem HDFs = Filesystem.get (Uri.create (subpath), conf); in = Hdfs.open (SRC); output = new FileOutputStream (New File local);//true-whether to close the data stream, or false to close//ioutils.copybytes (in, output, 4096, false) at finally; Ioutils.copybytes (in, Output, conf); Output.flush (); System.out.print ("Download successed."); CatcH (IOException e) {//TODO auto-generated catch Blocke.printstacktrace (); System.out.print ("Download failed."); Finally {Ioutils.closestream (in); Ioutils.closestream (output);}}}} catch (Exception e) {} finally {System.out.println ("The number of files is:" + i);} return true;} Download all files under Directory Method 2: Copy public static Boolean DownFromHdfsDir2 (String hdfssrc, String localdst) throws IOException {by byte Configuration conf = new configuration (); Path Dstpath = new Path (HDFSSRC); int i = 1; FileSystem fs = Filesystem.get (Uri.create (HDFSSRC), conf); try {String subpath = ""; filestatus[] flist = Fs.liststatus (Dstpath); for (Filestatus f:flist) {if (null! = f) {subpath = new StringBuffer (). Appen D (F.getpath (). GetParent ()). Append ("/"). Append (F.getpath (). GetName ()). ToString (); if (F.isdir ()) {Downfromhdfsdir ( Subpath, LOCALDST);} else {System.out.println ("/t/t" + subpath);//Hdfs://54.0.88.53:8020/path DST = new Path (subpath); i++;try {Path SRC = new Path (subpath); String Filename = Src.getname (). toString (); String LoCal = Localdst + Filename; Path Dst = new Path (local); FileSystem Localfs = filesystem.getlocal (conf); FileSystem HDFs = Filesystem.get (Uri.create (subpath), conf); Fsdatainputstream in = Hdfs.open (SRC); Fsdataoutputstream output = Localfs.create (Dst), byte[] buf = new Byte[1024];int readbytes = 0;while ((readbytes = In.read ( BUF)) > 0) {output.write (buf, 0, readbytes);} In.close (); Output.close ();} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace (); System.out.print ("Download failed."); Finally {}}}} ' catch (Exception e) {} finally {System.out.println ("the number of the files is:" + i);} return true;} Download all files in the directory, Method 2: Copy public static Boolean downFromHdfsDir3 (String hdfssrc, String localdst) throws IOException {by row Configuration conf = new configuration (); Path Dstpath = new Path (HDFSSRC); int i = 1; FileSystem fs = Filesystem.get (Uri.create (HDFSSRC), conf); try {String subpath = ""; filestatus[] flist = Fs.liststatus (Dstpath); for (Filestatus f:flist) {if (null! = f) {subpath= new StringBuffer (). Append (F.getpath (). GetParent ()). Append ("/"). Append (F.getpath (). GetName ()). ToString (); F.isdir ()) {Downfromhdfsdir (subpath, LOCALDST);} else {System.out.println ("/t/t" + subpath);//hdfs://54.0.88.53:8020 /path DST = new Path (subpath), i++;try {path SRC = new Path (subpath); String Filename = Src.getname (). toString (); String local = Localdst + Filename; Path Dst = new Path (local); FileSystem Localfs = filesystem.getlocal (conf); FileSystem HDFs = Filesystem.get (Uri.create (subpath), conf); Fsdatainputstream in = Hdfs.open (SRC); BufferedReader read = new BufferedReader (new InputStreamReader (in)); BufferedWriter output=new BufferedWriter (new FileWriter (local)); String line = null;while (line = Read.readline ())! = null) {output.append (line); Output.newline (); Output.flush ();} In.close (); Read.close (); Output.close ();} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace (); System.out.print ("Download failed."); Finally {}}}}} catch (Exception e) {} finally {SystEm.out.println ("The number of the files is:" + i);} return true;}
Read the entire file at once
OutputStream: (reads the entire file at once) bytes private static String ReadHdfsFile2 (FileSystem FS, path Path, String CharSet) throws IOException { Fsdatainputstream hdfsinstream = fs.open (path); Bytearrayoutputstream BOS = new Bytearrayoutputstream (); byte[] Iobuffer = new byte[1024]; int readlen = Hdfsinstream.read (iobuffer); while ( -1! = Readlen) { bos.write (iobuffer, 0, Readlen); Readlen = Hdfsinstream.read (Iobuffer); } Hdfsinstream.close (); return new String (Bos.tobytearray (), CharSet);}
HDFs read file to local summary