iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > JAVA >hadoop3自学入门笔记(3)-java 操作hdfs
  • 537
分享到

hadoop3自学入门笔记(3)-java 操作hdfs

hadoop3自学入门笔记(3)-java操作hdfs 2014-10-06 21:10:39 537人浏览 无得
摘要

1.core-site.xml fs.defaultFS hdfs://192.168.3.61:9820 hadoop.tmp.dir /op

hadoop3自学入门笔记(3)-java 操作hdfs

1.core-site.xml


  
        fs.defaultFS
        hdfs://192.168.3.61:9820
    

        hadoop.tmp.dir
        /opt/hadoopdata
    

2.pom.xml




  4.0.0

  com.qmkj
  hdfsclienttest
  0.1

  hdfsclienttest
  
  Http://www.example.com

  
    UTF-8
    1.7
    1.7
  

  
    
      junit
      junit
      4.11
      test
    
    
    
      org.apache.hadoop
      hadoop-hdfs-client
      3.2.1
      provided
    
    
    
      org.apache.hadoop
      hadoop-common
      3.2.1
    
    
    
      org.apache.hadoop
      hadoop-hdfs
      3.2.1
    

  

  
    
      
        
        
          Maven-clean-plugin
          3.1.0
        
        
        
          maven-resources-plugin
          3.0.2
        
        
          maven-compiler-plugin
          3.8.0
        
        
          maven-surefire-plugin
          2.22.1
        
        
          maven-jar-plugin
          3.0.2
        
        
          maven-install-plugin
          2.5.2
        
        
          maven-deploy-plugin
          2.8.2
        
        
        
          maven-site-plugin
          3.7.1
        
        
          maven-project-info-reports-plugin
          3.0.0
        
      
    
  

3.测试代码

package com.qmkj;


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.junit.Before;
import org.junit.Test;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI;



public class AppTest {
    FileSystem fs = null;

    @Before
    public void init() throws Exception {

        Configuration conf = new Configuration();
        //设立的设置url请注意,设置core-site.xml中配置fs.defaultFS的地址
        fs = FileSystem.get(new URI("hdfs://192.168.3.61:9820"), conf, "root");

    }

    @Test
    public void testAdd() throws Exception {
        fs.copyFromLocalFile(new Path("D:\KK_Movies\kk 2020-02-20 20-45-55.mp4"), new Path("/zhanglei"));
        fs.close();
    }

    
    @Test
    public void testDownloadFileToLocal() throws IllegalArgumentException, IOException {

        // fs.copyToLocalFile(new Path("/Mysql-connector-java-5.1.28.jar"), new
        // Path("d:/"));
        fs.copyToLocalFile(false, new Path("test.txt"), new Path("e:/"), true);
        fs.close();

    }

    
    @Test
    public void testMkdirAndDeleteAndRename() throws IllegalArgumentException, IOException {

        // 创建目录
        fs.mkdirs(new Path("/zhanglei/b1/c1"));

        // 删除文件夹 ,如果是非空文件夹,参数2必须给值true ,删除所有子文件夹
        fs.delete(new Path("/b1"), true);

        // 重命名文件或文件夹
        fs.rename(new Path("/zhanglei"), new Path("/qmkj"));

    }

    
    @Test
    public void testListFiles() throws FileNotFoundException, IllegalArgumentException, IOException {


        RemoteIterator listFiles = fs.listFiles(new Path("/"), true);

        while (listFiles.hasNext()) {

            LocatedFileStatus fileStatus = listFiles.next();

            System.out.println(fileStatus.getPath().getName());
            System.out.println(fileStatus.getBlockSize());
            System.out.println(fileStatus.getPermission());
            System.out.println(fileStatus.getLen());
            BlockLocation[] blockLocations = fileStatus.getBlockLocations();
            for (BlockLocation bl : blockLocations) {
                System.out.println("block-length:" + bl.getLength() + "--" + "block-offset:" + bl.getOffset());
                String[] hosts = bl.getHosts();
                for (String host : hosts) {
                    System.out.println(host);
                }

            }

            System.out.println("--------------打印的分割线--------------");

        }

    }

    
    @Test
    public void testListAll() throws FileNotFoundException, IllegalArgumentException, IOException {
        //可以右击方法名,Run 测试一下。
        FileStatus[] listStatus = fs.listStatus(new Path("/"));

        String flag = "";
        for (FileStatus fstatus : listStatus) {

            if (fstatus.isFile()) {
                flag = "f-- ";
            } else {
                flag = "d-- ";
            }
            System.out.println(flag + fstatus.getPath().getName());
            System.out.println(fstatus.getPermission());

        }

    }

}

testDownloadFileToLocal 这里测试请注意,本地也要装hdfs才可以

更多精彩请关注公众号【lovepythoncn】

--结束END--

本文标题: hadoop3自学入门笔记(3)-java 操作hdfs

本文链接: https://www.lsjlt.com/news/4552.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作