前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >javaAPI操作hadoop hdfs

javaAPI操作hadoop hdfs

作者头像
许喜朝
发布2020-09-29 09:44:32
6100
发布2020-09-29 09:44:32
举报
写在之前

在开始操作之前请确保已经正确安装启动hadoop并且能够连接到

依赖

代码语言:javascript
复制
<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13</version>
    </dependency>
    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-client</artifactId>
        <version>3.0.3</version>
    </dependency>
    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-common</artifactId>
        <version>3.0.3</version>
    </dependency>
    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-hdfs</artifactId>
        <version>3.0.3</version>
    </dependency>
</dependencies>

读取文件

代码语言:javascript
复制
public void test1() throws Exception{
        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(new URI("hdfs://192.168.19.4:9000"), conf);
        InputStream in = fs.open(new Path("/park/test.txt"));
        OutputStream out = new FileOutputStream("test.txt");
        IOUtils.copyBytes(in,out,conf);

}

写文件到hdfs

代码语言:javascript
复制
public void test2() throws Exception{
        Configuration conf = new Configuration();
        //设置副本数
        conf.set("dfs.replication","1");
        FileSystem fs = FileSystem.get(new URI("hdfs://192.168.19.4:9000"),conf,"root");
        ByteArrayInputStream in = new ByteArrayInputStream("helloworld".getBytes());
        OutputStream out = fs.create(new Path("/park/hello.txt"));
        IOUtils.copyBytes(in,out,conf);
}

删除文件

代码语言:javascript
复制
public void test3() throws Exception{
        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(new URI("hdfs://192.168.19.4:9000"),conf,"root");
        fs.delete(new Path("park/hello.txt"),true);
        fs.close();

}

创建文件

代码语言:javascript
复制
public void test4() throws Exception{
        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(new URI("hdfs://192.168.19.4:9000"),conf,"root");
        fs.mkdirs(new Path("/hello"));
        fs.close();
}

查询指定目录

代码语言:javascript
复制
public void test5() throws Exception{
        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(new URI("hdfs://192.168.19.4:9000"),conf,"root");
        FileStatus[] ls = fs.listStatus(new Path("/"));//查询hdfs根目录
        for (FileStatus l : ls) {
                System.out.println(l.getPath());
        }
}

递归查看指定目录下的所有文件

代码语言:javascript
复制
public void test6() throws Exception{
        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(new URI("hdfs://192.168.19.4:9000"),conf,"root");
        RemoteIterator<LocatedFileStatus> rt = fs.listFiles(new Path("/"),true);
        while (rt.hasNext()){
                System.out.println(rt.next());
        }
}

重命名

代码语言:javascript
复制
public void test7() throws Exception{
        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(new URI("hdfs://192.168.19.4:9000"),conf,"root");
        fs.rename(new Path("/park"),new Path("/park1"));
}

获取文件块信息

代码语言:javascript
复制
public void test8() throws Exception{
        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(new URI("hdfs://192.168.19.4:9000"),conf,"root");
        BlockLocation[] data = fs.getFileBlockLocations(new Path("/park1/hello.txt"),0,Integer.MAX_VALUE);
        for (BlockLocation datum : data) {
                System.out.println(datum);
        }

}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-09-28 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 写在之前
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档