package org.tony.hdfs;
import java.io.IOException; import java.net.URI;
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FSDataInputStream; import org.apache.hadoop.fs.FSDataOutputStream; import org.apache.hadoop.fs.FileStatus; import org.apache.hadoop.fs.FileSystem; import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hdfs.DistributedFileSystem; import org.apache.hadoop.hdfs.protocol.DatanodeInfo;
public class HDFSTest { public static void main(String[] args) throws Exception { //uploadLocalFile2HDFS(\ //createNewHDFSFile(\ //String str = new String(readHDFSFile(\ //System.out.println(str); //mkdir(\ //deleteDir(\ //listAll(\
//getDateNodeHost(); //testRename(); testUpload(); } //???HDFS????????н???????? public static void getDateNodeHost() throws IOException{ Configuration conf = getConf();
FileSystem fs=FileSystem.get(conf);
DistributedFileSystem hdfs = (DistributedFileSystem)fs; DatanodeInfo[] dataNodeStats = hdfs.getDataNodeStats(); for(int i=0;i System.out.println(\ } } /* * upload the local file to the hds * ·?????·?? */ /*public static void uploadLocalFile2HDFS(String s, String d) throws IOException { Configuration conf = getConf(); FileSystem hdfs = FileSystem.get(conf); Path src = new Path(s); Path dst = new Path(d); hdfs.copyFromLocalFile(src, dst); hdfs.close(); }*/ /* * create a new file in the hdfs. * notice that the toCreateFilePath is the full path * and write the content to the hdfs file. */ public static void createNewHDFSFile(String toCreateFilePath, String content) throws IOException { Configuration conf = getConf(); FileSystem hdfs = FileSystem.get(conf); FSDataOutputStream os = hdfs.create(new Path(toCreateFilePath)); os.write(content.getBytes(\ os.close(); hdfs.close(); } /* * delete the hdfs file * notice that the dst is the full path name */ public static boolean deleteHDFSFile(String dst) throws IOException { Configuration conf = getConf(); FileSystem hdfs = FileSystem.get(conf); Path path = new Path(dst); boolean isDeleted = hdfs.delete(path); hdfs.close(); return isDeleted; } /* * read the hdfs file content * notice that the dst is the full path name */ public static byte[] readHDFSFile(String dst) throws Exception { Configuration conf = getConf(); FileSystem fs = FileSystem.get(conf); // check if the file exists Path path = new Path(dst); if ( fs.exists(path) ) { FSDataInputStream is = fs.open(path); // get the file info to create the buffer FileStatus stat = fs.getFileStatus(path); // create the buffer byte[] buffer = new byte[Integer.parseInt(String.valueOf(stat.getLen()))]; is.readFully(0, buffer); is.close(); fs.close(); return buffer; } else { throw new Exception(\ } } /* * make a new dir in the hdfs * the dir may like '/tmp/testdir' */ public static void mkdir(String dir) throws IOException { Configuration conf = getConf(); FileSystem fs = FileSystem.get(conf); fs.mkdirs(new Path(dir)); fs.close(); } /* * delete a dir in the hdfs * dir may like '/tmp/testdir' */ public static void deleteDir(String dir) throws IOException { Configuration conf = getConf(); FileSystem fs = FileSystem.get(conf); fs.delete(new Path(dir)); fs.close(); } //?????????? hdfs????????? private static Configuration getConf(){ Configuration conf = new Configuration(); // ??仰??????Щ???????hadoop????????е???? conf.set(\ conf.set(\ return conf; } /** * @Title: listAll * @Description: ?г???????????? * @return void ???????? * @throws */ public static void listAll(String dir) throws IOException { Configuration conf = getConf(); FileSystem fs = FileSystem.get(conf); FileStatus[] stats = fs.listStatus(new Path(dir)); for(int i = 0; i < stats.length; ++i) { if (!stats[i].isDir()) { // regular file System.out.println(stats[i].getPath().toString()); } else { // dir System.out.println(stats[i].getPath().toString()); } // else if(stats[i].()) // { // // is s symlink in linux // System.out.println(stats[i].getPath().toString()); // } } fs.close(); } public static void testRename() throws Exception{ Configuration conf = getConf(); FileSystem fs = FileSystem.get(conf); Path dst = new Path(\ Path frpath = new Path(\ Path topath = new Path(\ fs.rename(frpath, topath); FileStatus files[] = fs.listStatus(dst); for(FileStatus file : files){ System.out.println(file.getPath()); } } public static void testUpload() throws Exception{ Configuration conf = getConf(); FileSystem fs = FileSystem.get(URI.create(\