HDFS-API的用法

作者: geekAppke | 来源:发表于2018-11-09 00:08 被阅读8次

API的封装,越来越趋向人类容易和简单使用

Window开发环境

(1)部署hadoop包
创建:C:\usr\

① 部署包hadoop-2.6.5:
    将tools目录下bin覆盖进去,对win支持很好!
    把bin/hadoop.dll 动态链接库,放到C:\Windows\system32,放新库,重启电脑
② 源码包:hadoop-2.6.5-src
    在eclipse中attach绑定源码包,就可查看具体class的实现~
③ 整合开发用到的jar包:hadoop-lib
    1、copy部署包C:\usr\hadoop-2.6.5\share\hadoop中除httpfs、kms外
所有目录、及其lib子目录下的jar包。共:121个
    2、开发计算程序,要引用它写API;打包不往里打!
开发把所有jar放一个目录下
不用把lib拖进项目,Build Path → Add to Build Path

(2)配置hadoop的环境变量

HADOOP_HOME=C:\usr\hadoop-2.6.5
加到PATH,%HADOOP_HOME%\bin
HADOOP_USER_NAME=root

(3)eclipse
① 安装:查看HDFS文件结构的插件,直接放到它plugins下,重启
② 添加Map/Reduce视图,Open Perspective
③ 配置hadoop部署目录

配置hadoop部署目录
(4)创建hdfs链接
active-NN

Refresh:显示服务器hdsf文件系统。方便进行上传、下载
也可以创建目录

(5)创建用户库,导入hadoop-lib中的jar包

制作用户库

(6)新建java项目

引用jar包,不占空间!
同样操作,添加单元测试,不用写main函数!:Junit 4


HDFS客户端

(1)配置文件,告诉它结点信息
或编程时直接指定!

项目 → new目录:conf/ha
    从集群下载:core-site.xml hdfs-site.xml 
    创建配置文件:点ha文件夹 → Build Path → Use as Source Folder

(2)创建、上传、查看块信息

public class TestHDFS {
    Configuration conf;
    FileSystem fs;
    
    @Before
    public void conn() throws Exception {
        // 只要创建出来,就读了配置文件
        conf = new Configuration(true);
        fs = FileSystem.get(conf);
    }
    
    @After
    public void close() throws Exception {
        fs.close();
    }
    
    @Test
    public void mkdir() throws Exception {
        Path path = new Path("/ooxx");
        if (fs.exists(path)) {
            fs.delete(path, true);
        } 
        fs.mkdirs(path);
    }
    
    @Test
    public void delete() throws Exception {
        Path path = new Path("/user/root/test.txt");
        if (fs.exists(path)) {
            fs.delete(path, true);
        } 
    }
    
    
    @Test
    public void upload() throws IOException {
        Path f = new Path("/ooxx/hello.txt");
        FSDataOutputStream output = fs.create(f);
        InputStream input = new BufferedInputStream(new FileInputStream(new File("C:\\LICENSE.txt")));
        
        IOUtils.copyBytes(input, output, conf);
    }
    
    @Test
    public void blks() throws IOException {
        // 查看块信息
        Path f = new Path("/user/root/test.txt");
        FileStatus fstat = fs.getFileStatus(f);
        BlockLocation[] blks = fs.getFileBlockLocations(fstat, 0, fstat.getLen());
        
        for (BlockLocation b : blks) {
            // 偏移量(起始位置,大小)、位置信息
            System.out.println(b);
        }
    }
}
0,1048576,node003,node004
1048576,540319,node002,node004

FileSystem
    FSdatainputstream  面向程序
    FSdataoutputstream
    FileStatus
blocklocation
    偏移量
    位置    :block副本位置信息
    大小

偏移量、距离概念

new Configuration()运行时,加载配置文件,里面有个静态块

static{
  //print deprecation warning if hadoop-site.xml is found in classpath
  ClassLoader cL = Thread.currentThread().getContextClassLoader();
  if(cL == null) {
      cL = Configuration.class.getClassLoader();
  }
  if (cL.getResource("hadoop-site.xml")!=null) {
    LOG.warn("DEPRECATED: hadoop-site.xml found in the classpath. " +
        "Usage of hadoop-site.xml is deprecated. Instead use core-site.xml, "
        + "mapred-site.xml and hdfs-site.xml to override properties of " +
        "core-default.xml, mapred-default.xml and hdfs-default.xml " +
        "respectively");
  }
  addDefaultResource("core-default.xml");
  addDefaultResource("core-site.xml");
}

相关文章

网友评论

    本文标题:HDFS-API的用法

    本文链接:https://www.haomeiwen.com/subject/ngswxqtx.html