通过"FileStatus.getPath()"可查看指定HDFS中某个目录下所有文件。
package hdfsTest; import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FSDataOutputStream; import org.apache.hadoop.fs.FileStatus; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; public class OperatingFiles { //initialization static Configuration conf = new Configuration(); static FileSystem hdfs; static { String path = "/usr/java/hadoop-1.0.3/conf/"; conf.addResource(new Path(path + "core-site.xml")); conf.addResource(new Path(path + "hdfs-site.xml")); conf.addResource(new Path(path + "mapred-site.xml")); path = "/usr/java/hbase-0.90.3/conf/"; conf.addResource(new Path(path + "hbase-site.xml")); try { hdfs = FileSystem.get(conf); } catch (IOException e) { e.printStackTrace(); } } //create a direction public void createDir(String dir) throws IOException { Path path = new Path(dir); hdfs.mkdirs(path); System.out.println("new dir \t" + conf.get("fs.default.name") + dir); } //copy from local file to HDFS file public void copyFile(String localSrc, String hdfsDst) throws IOException{ Path src = new Path(localSrc); Path dst = new Path(hdfsDst); hdfs.copyFromLocalFile(src, dst); //list all the files in the current direction FileStatus files[] = hdfs.listStatus(dst); System.out.println("Upload to \t" + conf.get("fs.default.name") + hdfsDst); for (FileStatus file : files) { System.out.println(file.getPath()); } } //create a new file public void createFile(String fileName, String fileContent) throws IOException { Path dst = new Path(fileName); byte[] bytes = fileContent.getBytes(); FSDataOutputStream output = hdfs.create(dst); output.write(bytes); System.out.println("new file \t" + conf.get("fs.default.name") + fileName); } //list all files public void listFiles(String dirName) throws IOException { Path f = new Path(dirName); FileStatus[] status = hdfs.listStatus(f); System.out.println(dirName + " has all files:"); for (int i = 0; i< status.length; i++) { System.out.println(status[i].getPath().toString()); } } //judge a file existed? and delete it! public void deleteFile(String fileName) throws IOException { Path f = new Path(fileName); boolean isExists = hdfs.exists(f); if (isExists) { //if exists, delete boolean isDel = hdfs.delete(f,true); System.out.println(fileName + " delete? \t" + isDel); } else { System.out.println(fileName + " exist? \t" + isExists); } } public static void main(String[] args) throws IOException { OperatingFiles ofs = new OperatingFiles(); System.out.println("\n=======create dir======="); String dir = "/test"; ofs.createDir(dir); System.out.println("\n=======copy file======="); String src = "/home/ictclas/Configure.xml"; ofs.copyFile(src, dir); System.out.println("\n=======create a file======="); String fileContent = "Hello, world! Just a test."; ofs.createFile(dir+"/word.txt", fileContent); } }
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FSDataInputStream; import org.apache.hadoop.fs.FSDataOutputStream; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path; public class HDFSClient { public HDFSClient() { } public void addFile(String source, String dest) throws IOException { Configuration conf = new Configuration(); // Conf object will read the HDFS configuration parameters from these // XML files. conf.addResource(new Path("/opt/hadoop-0.20.0/conf/core-site.xml")); conf.addResource(new Path("/opt/hadoop-0.20.0/conf/hdfs-site.xml")); FileSystem fileSystem = FileSystem.get(conf); // Get the filename out of the file path String filename = source.substring(source.lastIndexOf('/') + 1, source.length()); // Create the destination path including the filename. if (dest.charAt(dest.length() - 1) != '/') { dest = dest + "/" + filename; } else { dest = dest + filename; } // System.out.println("Adding file to " + destination); // Check if the file already exists Path path = new Path(dest); if (fileSystem.exists(path)) { System.out.println("File " + dest + " already exists"); return; } // Create a new file and write data to it. FSDataOutputStream out = fileSystem.create(path); InputStream in = new BufferedInputStream(new FileInputStream( new File(source))); byte[] b = new byte[1024]; int numBytes = 0; while ((numBytes = in.read(b)) > 0) { out.write(b, 0, numBytes); } // Close all the file descripters in.close(); out.close(); fileSystem.close(); } public void readFile(String file) throws IOException { Configuration conf = new Configuration(); conf.addResource(new Path("/opt/hadoop-0.20.0/conf/core-site.xml")); FileSystem fileSystem = FileSystem.get(conf); Path path = new Path(file); if (!fileSystem.exists(path)) { System.out.println("File " + file + " does not exists"); return; } FSDataInputStream in = fileSystem.open(path); String filename = file.substring(file.lastIndexOf('/') + 1, file.length()); OutputStream out = new BufferedOutputStream(new FileOutputStream( new File(filename))); byte[] b = new byte[1024]; int numBytes = 0; while ((numBytes = in.read(b)) > 0) { out.write(b, 0, numBytes); } in.close(); out.close(); fileSystem.close(); } public void deleteFile(String file) throws IOException { Configuration conf = new Configuration(); conf.addResource(new Path("/opt/hadoop-0.20.0/conf/core-site.xml")); FileSystem fileSystem = FileSystem.get(conf); Path path = new Path(file); if (!fileSystem.exists(path)) { System.out.println("File " + file + " does not exists"); return; } fileSystem.delete(new Path(file), true); fileSystem.close(); } public void mkdir(String dir) throws IOException { Configuration conf = new Configuration(); conf.addResource(new Path("/opt/hadoop-0.20.0/conf/core-site.xml")); FileSystem fileSystem = FileSystem.get(conf); Path path = new Path(dir); if (fileSystem.exists(path)) { System.out.println("Dir " + dir + " already not exists"); return; } fileSystem.mkdirs(path); fileSystem.close(); } public static void main(String[] args) throws IOException { if (args.length < 1) { System.out.println("Usage: hdfsclient add/read/delete/mkdir" + " [<local_path> <hdfs_path>]"); System.exit(1); } HDFSClient client = new HDFSClient(); if (args[0].equals("add")) { if (args.length < 3) { System.out.println("Usage: hdfsclient add <local_path> " + "<hdfs_path>"); System.exit(1); } client.addFile(args[1], args[2]); } else if (args[0].equals("read")) { if (args.length < 2) { System.out.println("Usage: hdfsclient read <hdfs_path>"); System.exit(1); } client.readFile(args[1]); } else if (args[0].equals("delete")) { if (args.length < 2) { System.out.println("Usage: hdfsclient delete <hdfs_path>"); System.exit(1); } client.deleteFile(args[1]); } else if (args[0].equals("mkdir")) { if (args.length < 2) { System.out.println("Usage: hdfsclient mkdir <hdfs_path>"); System.exit(1); } client.mkdir(args[1]); } else { System.out.println("Usage: hdfsclient add/read/delete/mkdir" + " [<local_path> <hdfs_path>]"); System.exit(1); } System.out.println("Done!"); } }
from:http://smallwildpig.iteye.com/blog/1705039 Java对HDFS的操作
http://blog.rajeevsharma.in/2009/06/using-hdfs-in-java-0200.html Using HDFS in java (0.20.0)