您好,登录后才能下订单哦!
本篇内容主要讲解“如何使用Java API操作Hadoop环境搭建”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“如何使用Java API操作Hadoop环境搭建”吧!
本教程演示均为windows环境下的操作,使用unix/linux系统请绕道。
首先,在服务器上部署安装好Hadoop,下载安装包传送门 hdp下载地址,在这里不再过多演示。
配置windows中的Hadoop环境
 1.将linux服务器上部署的Hadoop安装包下载一份到windows下,保证是英文安装目录。
2.配置系统环境变量,这里以hadoop2.6.5为例。实际配置中请替换成自己的版本。
3.在系统Path变量中添加
4.下载相关依赖包,传送门 winutils地址
然后将对应版本的winutils拷贝至安装hadoop的bin目录下,将hadoop.dll添加到C:\Windows\System32文件夹下。
使用IDEA编写代码测试操作Hadoop服务器
 1.新建maven工程
2.添加maven依赖
        <dependency>
             <groupId>org.apache.hadoop</groupId>
             <artifactId>hadoop-common</artifactId>
             <version>2.6.5</version>
         </dependency>
         <dependency>
             <groupId>org.apache.hadoop</groupId>
             <artifactId>hadoop-client</artifactId>
             <version>2.6.5</version>
         </dependency>
         <dependency>
             <groupId>org.apache.hadoop</groupId>
             <artifactId>hadoop-hdfs</artifactId>
             <version>2.6.5</version>
         </dependency>
 3.编写测试方法调试
   public static void main(String[] args) {
         //设定hadoop环境变量,以防配置时出错,系统环境变量配置正确也可省略
         System.setProperty("hadoop.home.dir", "D:\\Bigdata\\hadoop\\hadoop-2.6.5");
         FileSystem fs = null;
         try {
             //hdfs访问路径
             URI uri = new URI("hdfs://192.168.7.103:9000/");
  
             Configuration conf = new Configuration();
             //获取hdfs客户端
             fs = FileSystem.get(uri, conf, "root");
  
             testFileInfoList(fs);
 //          testCoprFromLocalFile(fs);
  
         } catch (Exception e){
             System.out.println("执行出错...");
         } finally {
             try {
                 fs.close();
             } catch (IOException e) {
                 e.printStackTrace();
             }
         }
     }
  
     /**
      * 获取/路径下所有文件目录
      */
     public static void printRootFile(FileSystem fs) throws IOException {
  
         FileStatus[] listStatus = fs.listStatus(new Path("/"));
  
         Arrays.asList(listStatus).stream().map(FileStatus::getPath).forEach(System.out::println);
  
     }
  
     /**
      * 创建目录
      */
     public static void mkdir(FileSystem fs) throws IOException {
         fs.mkdirs(new Path("/test1/mkdirs"));
     }
  
     /**
      * 测试从hdfs上下载文件
      * @param fs
      * @throws IOException
      */
     public static void testDownLoad(FileSystem fs) throws IOException {
  
         Path localPath = new Path("C:\\Users\\Desktop\\lpthw.pdf");
  
         System.out.println(localPath);
  
         fs.copyToLocalFile(new Path("/test1/file/lpthw.pdf"), localPath);
     }
  
     /**
      * 测试上传本地文件到hdfs
      * @param fs
      */
     public static void testCoprFromLocalFile(FileSystem fs) throws IOException {
  
         fs.copyFromLocalFile(new Path("C:\\Users\\Desktop\\lpthw.pdf"), new Path("/test1/file/lpthw.pdf"));
  
         System.out.println("done...");
     }
  
     /**
      * 测试文件删除
      * @param fs
      * @throws IOException
      */
     public static void testDelete(FileSystem fs) throws IOException {
  
         fs.delete(new Path("/client/"), true);
  
         System.out.println("done...");
     }
  
     /**
      * 获取文件详细信息
      * @param fs
      * @throws IOException
      */
     public static void testFileInfoList(FileSystem fs) throws IOException {
         RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"), true);
  
         while (listFiles.hasNext()){
             LocatedFileStatus fileStatus = listFiles.next();
  
             System.out.println("文件名:" + fileStatus.getPath().getName());
  
             System.out.println("长度:" + fileStatus.getLen());
  
             System.out.println("权限:" + fileStatus.getPermission());
  
             BlockLocation[] blockLocations = fileStatus.getBlockLocations();
  
             System.out.println("存储的块信息:" + fileStatus.getGroup());
  
             for (BlockLocation blockLocation : blockLocations) {
                 // 获取块存储的主机节点
                 String[] hosts = blockLocation.getHosts();
  
                 for (String host : hosts) {
                     System.out.println(host);
                 }
             }
  
             System.out.println("=============================");
  
         }
  
         System.out.println("done...");
  
     }
 ————————————————
下载地址
https://archive.apache.org/dist/hadoop/common/
https://github.com/cdarlint/winutils
到此,相信大家对“如何使用Java API操作Hadoop环境搭建”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。