javaAPI操作hadoop hdfs

写在之前

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

依赖

<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>

读取文件

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

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);
}

删除文件

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();

}

创建文件

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();
}

查询指定目录

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());
        }
}

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

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());
        }
}

重命名

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"));
}

获取文件块信息

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);
        }

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值