コード例 #1
0
  protected FileSystem getNewDFS() {
    final FileSystem[] returned = new FileSystem[1];
    IHDFSFileSystem access = null;
    final String host = RemoteUtilities.getHost();
    final int port = RemoteUtilities.getPort();
    //     RemoteUtilities.getPassword()
    final String user = RemoteUtilities.getUser();
    String connStr = host + ":" + port + ":" + user + ":" + RemoteUtilities.getPassword();

    final String userDir = "/user/" + user;
    //   final String userDir = "/user" ;

    try {
      UserGroupInformation ugi = getCurrentUserGroup();
      //           if(true)    throw new UnsupportedOperationException("Uncomment when using version
      // 1.0.*");
      ugi.doAs(
          new PrivilegedExceptionAction<Void>() {

            public Void run() throws Exception {

              Configuration conf = new Configuration();
              conf.set("fs.default.name", "hdfs://" + host + ":" + port);
              conf.set("fs.defaultFS", "hdfs://" + host + ":" + port + userDir);
              //       conf.set("fs.defaultFS", "hdfs://" + host + ":" + port + userDir);
              //              conf.set("hadoop.job.ugi", user);
              conf.set("hadoop.job.ugi", user);
              //             conf.set("hadoop.job.ugi", "hdfs");

              returned[0] = FileSystem.get(conf);

              return null;
            }
          });
    } catch (Exception e) {
      throw new RuntimeException(e);
    }

    if (returned[0] == null) throw new IllegalStateException("cannot get file system");
    return returned[0];
  }
コード例 #2
0
  public static boolean isHDFSAccessible() {

    IHDFSFileSystem access = null;
    final String host = RemoteUtilities.getHost();
    final int port = RemoteUtilities.getPort();
    //     RemoteUtilities.getPassword()
    final String user = RemoteUtilities.getUser();
    String connStr = host + ":" + port + ":" + user + ":" + RemoteUtilities.getPassword();

    final String userDir = "/user/" + user;
    //   final String userDir = "/user" ;

    try {
      UserGroupInformation ugi = getCurrentUserGroup();
      //          throw new UnsupportedOperationException("Uncomment when using version 1.0.*");
      ugi.doAs(
          new PrivilegedExceptionAction<Void>() {

            public Void run() throws Exception {

              Configuration conf = new Configuration();
              conf.set("fs.default.name", "hdfs://" + host + ":" + port);
              conf.set("fs.defaultFS", "hdfs://" + host + ":" + port + userDir);
              //       conf.set("fs.defaultFS", "hdfs://" + host + ":" + port + userDir);
              //              conf.set("hadoop.job.ugi", user);
              conf.set("hadoop.job.ugi", user);
              //             conf.set("hadoop.job.ugi", "hdfs");

              FileSystem fs = FileSystem.get(conf);

              Path udir = new Path(userDir);

              FileStatus fileStatus = fs.getFileStatus(udir);
              FsPermission permission = fileStatus.getPermission();

              //           fs.setPermission(udir, ALL_ACCESS);

              fileStatus = fs.getFileStatus(udir);
              permission = fileStatus.getPermission();

              //         fs.mkdirs(new Path(userDir + "/ebi/" ),IHDFSFileSystem.FULL_ACCESS);
              //         fs.mkdirs(new Path(userDir + "/ebi/Sample2/"
              // ),IHDFSFileSystem.FULL_ACCESS);

              FileStatus[] fileStatuses = fs.listStatus(udir);
              for (int i = 0; i < fileStatuses.length; i++) {
                FileStatus fileStatuse = fileStatuses[i];
                System.err.println(fileStatuse.getPath());
              }

              //                    fs.createNewFile(new Path(userDir + "/test"));
              //
              //                    FileStatus[] status = fs.listStatus(new Path("/user/" + user));
              //                    for (int i = 0; i < status.length; i++) {
              //                        System.out.println(status[i].getPath());
              //                    }
              return null;
            }
          });
    } catch (Exception e) {
      return false;
    }

    //        if (true)
    //            return true;
    return true;
  }