Hdfs-Java002

配置Hadoop

1. 在 Windows 安装Hadoop.
2. 设置 ``HADOOP_HOME``
3. 将hadoop的bin目录下的hadoop.dll 放置到 C:/Windows/System32 目录下

maven

		<dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-common</artifactId>
            <version>3.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-hdfs</artifactId>
            <version>3.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-hdfs-client</artifactId>
            <version>3.1.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>3.1.1</version>
        </dependency>

Java接口

Configuration

通过该接口设置HDFS的信息.
例如,类型,以及链接地址

1. Configuration
	封装了Client 或者 Server的配置
2. FileSystem
	该类是文件系统的对象.可以用该对象对文件进行操作.

FileSystem

代表了HDFS的类型.文件系统的类型
例如

FileSystem fs = FileSystem.get(configuration);

get 方法从 configuration 中的一个参数:fs.defaultFS获取该HDFS文件系统的类型.
如果没有设置的话,则使用默认的系统类型
默认的来自 Hadoop 中Jar包的 core-default.xml
默认值是 file:///,是一个本地的文件系统,就不是分布式文件系统

HDFS操作

HDFS创建文件夹

FileSystem fileSystem = FileSystem.get(new URI(Constant.URL),new Configuration(),"root");
fileSystem.mkdirs(new Path("/base_msg"));

遍历所有HDFS文件

API遍历

FileSystem fileSystem = FileSystem.get(new URI(Constant.URL),new Configuration(),"root");
RemoteIterator<LocatedFileStatus> locatedFileStatusRemoteIterator = fileSystem.listFiles(new Path("/"), true);
while (locatedFileStatusRemoteIterator.hasNext()) {
    LocatedFileStatus fileStatus = locatedFileStatusRemoteIterator.next();
    System.out.println("file Path and file Name :: "+fileStatus.getPath()+"---"+fileStatus.getPath().getName());
    BlockLocation[] blockLocations = fileStatus.getBlockLocations();
    System.out.println("block size is :: " + blockLocations.length);
    for (BlockLocation blockLocation : blockLocations) {
        String[] hosts = blockLocation.getHosts();
        for (String host : hosts) {
            System.out.println(host + "--");
        }
    }
}

文件上传

FileSystem fileSystem = FileSystem.get(new URI(Constant.URL),new Configuration(),"root");
fileSystem.copyFromLocalFile(new Path("C:/Users/Administrator/Desktop/周报-XXX.doc"),new Path("/medicalRecord/XXX.doc"));
fileSystem.close();

文件下载

FileSystem fileSystem = FileSystem.get(new URI(Constant.URL),new Configuration(),"root");
FSDataInputStream in = fileSystem.open(new Path("/medicalRecord/XXX病历.doc"));
FileOutputStream out = new FileOutputStream(new File("C:/Users/Administrator/Desktop/XXX病历1.doc"));
IOUtils.copy(in,out);
// 需要关闭
IOUtils.closeQuietly(in);
IOUtils.closeQuietly(out);
System.out.println("-----第二种方法直接下载-------");
fileSystem.copyFromLocalFile(new Path("/medicalRecord/XXX病历.doc"),new Path("C:/Users/Administrator/Desktop/XXX病历22.doc"));
// 第二种只需要关闭这个即可
fileSystem.close();

只是API的调用;均可实现下载

文件合并

将一些小的文件合并上传或者合并下载

上传

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值