ÄÚÈÝ·¢²¼¸üÐÂʱ¼ä : 2025/8/1 4:38:23ÐÇÆÚÒ» ÏÂÃæÊÇÎÄÕµÄÈ«²¿ÄÚÈÝÇëÈÏÕæÔĶÁ¡£
* ÅжÏĿ¼ÊÇ·ñΪ¿Õ * true: ¿Õ£¬false: ·Ç¿Õ */ public static boolean isDirEmpty(Configuration conf, String remoteDir) throws IOException { fs = (conf); Path dirPath = new Path(remoteDir); RemoteIterator
£¨11£© ÔÚHDFSÖУ¬½«Îļþ´ÓԴ·¾¶Òƶ¯µ½Ä¿µÄ·¾¶¡£ ShellÃüÁ ./hdfs dfs -mv text.txt text2.txt Java´úÂ룺 import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.*; import java.io.*; public class HDFSApi { /** * ÒÆ¶¯Îļþ */ public static boolean mv(Configuration conf, String remote, String remoteTo) throws IOException { fs = (conf); Path srcPath = new Path(remote); Path dstPath = new Path(remoteTo); boolean result = fs.rename(srcPath, dstPath); fs.close(); return result; } /** * Ö÷º¯Êý */ public static void main(String[] args) { Configuration conf = new Configuration(); conf.set(\ String remote = \ // Ô´ÎļþHDFS·¾¶ String remoteTo = \ // Ä¿µÄHDFS·¾¶ try { if ( HDFSApi.mv(conf, remote, remoteTo) ) { System.out.println(\½«Îļþ \ÒÆ¶¯µ½ \ } else { System.out.println(\²Ù×÷ʧ°Ü(Ô´Îļþ²»´æÔÚ»òÒÆ¶¯Ê§°Ü)\ } } catch (Exception e) { e.printStackTrace(); } } }
2. ±à³ÌʵÏÖÒ»¸öÀà¡°MyFSDataInputStream¡±£¬¸ÃÀà¼Ì³Ð¡°org.apache.hadoop.fs.FSDataInput
Stream¡±£¬ÒªÇóÈçÏ£ºÊµÏÖ°´ÐжÁÈ¡HDFSÖÐÖ¸¶¨ÎļþµÄ·½·¨¡°readLine()¡±£¬Èç¹û¶Áµ½Îļþĩ⣬Ôò·µ»Ø¿Õ£¬·ñÔò·µ»ØÎļþÒ»ÐеÄÎı¾¡£ Java´úÂ룺 import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FSDataInputStream; import org.apache.hadoop.fs.; import org.apache.hadoop.fs.Path; import java.io.*; public class MyFSDataInputStream extends FSDataInputStream { public MyFSDataInputStream(InputStream in) { super(in); } /** * ʵÏÖ°´ÐжÁÈ¡ * ÿ´Î¶ÁÈëÒ»¸ö×Ö·û£¬Óöµ½\½áÊø£¬·µ»ØÒ»ÐÐÄÚÈÝ */ public static String readline(BufferedReader br) throws IOException { char[] data = new char[1024]; int read = -1; int off = 0; // Ñ»·Ö´ÐÐʱ£¬br ÿ´Î»á´ÓÉÏÒ»´Î¶ÁÈ¡½áÊøµÄλÖüÌÐø¶ÁÈ¡£¬Òò´Ë¸Ãº¯ÊýÀoff ÿ´Î¶¼´Ó0¿ªÊ¼ while ( (read = br.read(data, off, 1)) != -1 ) { if (String.valueOf(data[off]).equals(\) { off += 1; break; } off += 1; } if (off > 0) { return String.valueOf(data); } else { return null; } } /** * ¶ÁÈ¡ÎļþÄÚÈÝ */ public static void cat(Configuration conf, String remote) throws IOException { fs = (conf); Path remotePath = new Path(remote); FSDataInputStream in = fs.open(remotePath); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String line = null; while ( (line = MyFSDataInputStream.readline(br)) != null ) { System.out.println(line); } br.close(); in.close(); fs.close(); } /** * Ö÷º¯Êý */ public static void main(String[] args) { Configuration conf = new Configuration(); conf.set(\ String remote = \ // HDFS·¾¶ try { MyFSDataInputStream.cat(conf, remote); } catch (Exception e) { e.printStackTrace(); } } }
3. ²é¿´Java°ïÖúÊÖ²á»òÆäËü×ÊÁÏ£¬Óá°java.net.URL¡±ºÍ¡°org.apache.hadoop.fs.FsURLStrea
mHandlerFactory¡±±à³ÌÍê³ÉÊä³öHDFSÖÐÖ¸¶¨ÎļþµÄÎı¾µ½ÖÕ¶ËÖС£ Java´úÂ룺 import org.apache.hadoop.fs.*; import org.apache.hadoop.io.IOUtils;