package hadoop.test; import java.io.IOException; import java.net.URI; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.BlockLocation; 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.io.IOUtils; public class FileSystemTest { private static final String hadoop_url = "hdfs://localhost:9000"; private static FileSystem fs = null; static { Configuration configuration = new Configuration(); try { fs = FileSystem.get(URI.create(hadoop_url), configuration); } catch (IOException ex) { ex.printStackTrace(); } } //创建目录 public static void mkdirs() throws IOException { Path path = new Path("/mytest"); boolean flag = fs.mkdirs(path); System.out.println(flag ? "success" : "failure"); } //创建文件 如果文件存在会覆盖 public static void mkFile() throws IOException { Path path = new Path("/mytest/test.txt"); // fs.create(path); FSDataOutputStream outputStream = fs.create(path); outputStream.write("Hello World !".getBytes()); IOUtils.closeStream(outputStream); } //重命名 文件/文件夹 public static void rename() throws IOException { Path oldFile = new Path("/mytest/test.txt"); Path newFile = new Path("/mytest/t.txt"); boolean flag = fs.rename(oldFile, newFile); } //删除 文件/文件夹 public static void delete() throws IOException { Path path = new Path("/mytest/t.txt"); boolean flag = fs.delete(path); } //上传/下载文件到HDFS public static void upload() throws IOException { Path path = new Path("/mytest/a.txt"); Path src = new Path("/home/zcwangjb/t.txt"); fs.copyFromLocalFile(src, path); } //查看文件在集群中的位置 public static void location() throws IOException { Path path = new Path("/mytest/a.txt"); boolean flag = fs.exists(path); //文件是否存在 if (flag) { FileStatus fileStatus = fs.getFileStatus(path); //fileStatus. BlockLocation[] blockLocations = fs.getFileBlockLocations(fileStatus, 0, fileStatus.getLen()); for (int i = 0; i < blockLocations.length; i++) { String[] hosts = blockLocations[i].getHosts(); System.out.println("host:" + i + "====" + hosts[0]); } } } public static void main(String[] args) throws IOException { FileSystemTest test = new FileSystemTest(); //mkdirs(); mkFile(); // rename(); //delete(); // upload(); location(); } }