Hadoop HDFS 基本API使用

该文章展示了如何在Java中使用Hadoop进行文件操作,包括添加Hadoop相关依赖,配置core-site.yaml和hdfs-site.yaml,以及进行连接、创建目录、上传文件、检查数据块位置和读取文件内容的测试。
摘要由CSDN通过智能技术生成

1、添加依赖

<!-- hadoop -->
    <dependency>
      <groupId>org.apache.hadoop</groupId>
      <artifactId>hadoop-common</artifactId>
      <version>2.6.5</version>
    </dependency>
      <dependency>
          <groupId>org.apache.hadoop</groupId>
          <artifactId>hadoop-hdfs</artifactId>
          <version>2.6.5</version>
          <scope>test</scope>
      </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.13.2</version>
      <scope>compile</scope>
    </dependency>

2、添加resource

添加core-site.yaml

添加hdfs-site.yaml

3、测试

public class HdfsTest {
    private Configuration configuration = null;
    private FileSystem fs = null;

    @Before
    public void connect() throws Exception {
        configuration = new Configuration(true);
//        fs = FileSystem.get(configuration);
        fs = FileSystem.get(URI.create("hdfs://node01:9000"), configuration, "root");
    }

    @Test
    public void mkdir() throws IOException {
        Path path = new Path("/msb01");
        if (fs.exists(path)) {
            fs.delete(path, true);
        }
        fs.mkdirs(path);
    }

    @Test
    public void upload() throws IOException {
        BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(new File("./data/hello.txt")));
        Path outfile = new Path("/msb01/out.txt");
        FSDataOutputStream outputStream = fs.create(outfile);
        IOUtils.copyBytes(inputStream, outputStream, configuration, true);
    }

    @Test
    public void blocks() throws IOException {
        Path file = new Path("/user/root/data.txt");
        FileStatus fileStatus = fs.getFileStatus(file);
        BlockLocation[] fileBlockLocations = fs.getFileBlockLocations(fileStatus, 0, fileStatus.getLen());
        for (BlockLocation b : fileBlockLocations) {
            System.out.println(b);
        }

        FSDataInputStream in = fs.open(file);
        in.seek(1048576);

        System.out.println((char)in.readByte());
        System.out.println((char)in.readByte());
        System.out.println((char)in.readByte());
        System.out.println((char)in.readByte());
        System.out.println((char)in.readByte());
        System.out.println((char)in.readByte());
        System.out.println((char)in.readByte());
        System.out.println((char)in.readByte());
        System.out.println((char)in.readByte());
        System.out.println((char)in.readByte());
        System.out.println((char)in.readByte());
        System.out.println((char)in.readByte());
    }


    @After
    public void close() throws IOException {
        fs.close();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值