1. package com.hdfs;
    2. import org.apache.hadoop.fs.FSDataInputStream;
    3. import org.apache.hadoop.fs.FileStatus;
    4. import org.apache.hadoop.fs.FileSystem;
    5. import org.apache.hadoop.conf.Configuration;
    6. import org.apache.hadoop.fs.Path;
    7. import java.io.IOException;
    8. public class FileSystemOperationDemo1 {
    9. public static void main(String[] args) throws IOException {
    10. //getter connection
    11. Configuration conf = new Configuration();
    12. //create core path
    13. Path corePath = new Path("/usr/local/hadoop/etc/hadoop/core-site.xml");
    14. //create hdfspath
    15. Path hdfsSiteXmlPath = new Path("/usr/local/hadoop/etc/hadoop/hdfs-site.xml");
    16. //add resource to connection
    17. conf.addResource(corePath);
    18. conf.addResource(hdfsSiteXmlPath);
    19. Path hdfsPath = new Path("hdfs://192.168.92.132:8020/TestDir/");
    20. FileSystem fs = hdfsPath.getFileSystem(conf);
    21. //Determine whether the directory or file is exists
    22. boolean b = fs.exists(hdfsPath);
    23. if (!b) {
    24. fs.mkdirs(hdfsPath);//create file instance
    25. }
    26. //print hostname
    27. java.lang.String hostname = conf.get("fs.default.name");
    28. System.out.println(hostname);
    29. //copy HDFS to Localhost
    30. fs.copyToLocalFile(new Path("hdfs://192.168.92.132:8020/profile"), new Path("/"));
    31. fs.copyFromLocalFile(new Path("/etc/profile"), new Path("hdfs://192.168.92.132:8020/TestDir"));
    32. FileStatus status = fs.getFileStatus(new Path("hdfs://192.168.92.132:8020/profile"));
    33. //getter absolute path
    34. System.out.println("path:" + status.getPath());
    35. //getter relative path
    36. System.out.println("path:" + status.getPath().toUri().getPath());
    37. //get block size
    38. System.out.println("blockSize:" + status.getBlockSize());
    39. //get group
    40. System.out.println("Group:" + status.getGroup());
    41. //get Owner
    42. System.out.println("Owner:" + status.getOwner());
    43. // getter all statuses
    44. FileStatus[] statuses = fs.listStatus(new Path("hdfs://192.168.92.132:8020/TestDir"));
    45. for (FileStatus fileStatus : statuses) {
    46. System.out.println("relative path:" + fileStatus.getPath().toUri().getPath());
    47. }
    48. FSDataInputStream fdis = fs.open(new Path("hdfs://192.168.92.132:8020/profile"));
    49. byte[] buff = new byte[128];
    50. int length = 0;
    51. while ((length = fdis.read(buff, 0, 128)) != -1) {
    52. System.out.println(length);
    53. System.out.println(new java.lang.String(buff,0,length));
    54. }
    55. System.out.println(fdis.getPos());
    56. fdis.seek(10);
    57. while ((length = fdis.read(buff, 0, 128)) != -1) {
    58. System.out.println(new java.lang.String(buff, 0, length));
    59. }
    60. fdis.seek(0);
    61. byte[] buff2 = new byte[128];
    62. fdis.read(buff2, 0, 128);
    63. System.out.println(new java.lang.String(buff2));
    64. System.out.println(buff2.length);
    65. fs.close();
    66. }
    67. }