Java操作hdfs笔记

本文详细介绍了一个Java类,用于演示如何使用Apache Hadoop进行HDFS的基本操作,包括文件上传、下载、目录创建、删除以及文件列表展示等。通过具体的方法实现,如testUpload、testDownload、testMkDir、testdel和testList,读者可以了解如何与HDFS进行交互。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package day01;

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

import java.net.URI;
import java.util.Iterator;
import java.util.Map.Entry;


public class hdfsDemo {

    FileSystem fs = null;
    Configuration conf = null;

    /**
     * 初始化一个hdfs FileSystem
     *
     * @throws Exception
     */
    @Before
    public void init() throws Exception {
        conf = new Configuration();
        conf.set("dfs.replication", "5");
        fs = FileSystem.get(new URI("hdfs://myspark:9000"), conf, "sparkuser");
    }

    /**
     * 上传文件到hdfs
     *
     * @throws Exception
     */
    @Test
    public void testUpload() throws Exception {
        fs.copyFromLocalFile(new Path("E:/data/access.log"), new Path("/hdfs_test"));
        fs.close();
    }

    /**
     * 从hdfs下载一个文件到本地
     *
     * @throws Exception
     */
    @Test
    public void testDownload() throws Exception {
        fs.copyToLocalFile(new Path("/hdfs_test/access.log"), new Path("E:/data/"));
        fs.close();
    }

    /**
     * 获取配置信息并打印
     */
    @Test
    public void showConf() {
        Iterator<Entry<String, String>> iterator = conf.iterator();
        while (iterator.hasNext()) {
            Entry<String, String> entry = iterator.next();
            System.out.println(entry.getKey() + "---" + entry.getValue());
        }
    }

    /**
     * 创建hdfs目录
     *
     * @throws Exception
     */
    @Test
    public void testMkDir() throws Exception {
        fs.mkdirs(new Path("/abc/cba"));
        fs.close();
    }

    /**
     * 删除hdfs目录或文件
     *
     * @throws Exception
     */
    @Test
    public void testdel() throws Exception {
        fs.delete(new Path("/hdfs_test/access.log"));
        fs.close();
    }

    /**
     * 列出hdfs的文件路径、文件名
     * listStatus和listFiles的使用
     * @throws Exception
     */
    @Test
    public void testList() throws Exception {
        FileStatus[] fileStatuses = fs.listStatus(new Path("/"));
        for (FileStatus fileStatus : fileStatuses) {
            System.err.println(fileStatus.getPath() + "++++" + fileStatus.toString());
        }
        RemoteIterator<LocatedFileStatus> remoteIterator = fs.listFiles(new Path("/"), true);
        while (remoteIterator.hasNext()) {
            LocatedFileStatus next = remoteIterator.next();
            String name = next.getPath().getName();
            Path path = next.getPath();
            System.out.println(name + "---" + path.toString());
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

柏舟飞流

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值