使用Java Api操作HDFS过程详解

网友投稿 256 2022-12-26


使用Java Api操作HDFS过程详解

如题 我就是一个标题党 就是使用javaApi操作HDFS,使用的是MAVEN,操作的环境是linux

首先要配置好Maven环境,我使用的是已经有的仓库,如果你下载的jar包 速度慢,可以改变Maven 下载jar包的镜像站改为 阿里云。

贴一下 pom.xml

使用到的jar包

junit

junit

4.10

org.apache.hadoop

hadoop-client

${hadoop.version}

然后就是操作HDFS的代码

package com.zuoyan.hadoop.hdfs;

import java.io.File;

import java.io.FileInputStream;

import org.apache.hadoop.conf.Configuration;

import org.apache.hadoop.fs.FSDataInputStream;

import org.apache.hadoop.fs.FSDataOutputStream;

import org.apache.hadoop.fs.FileSystem;

import org.apache.hadoop.fs.Path;

import org.apache.hadoop.io.IOUtils;

/**

* use java api operate hdfs

*

* @author beifeng

*

*/

public class HdfsApp {

// get FileSystem

public static FileSystem getFileSystem() throws Exception {

Configuration conf = new Configuration();

FileSystem fileSystem = FileSystem.get(conf);

return fileSystem;

}

public static void read(String fileName) throws Exception {

FileSystem fileSystem = getFileSystem();

// read Path

Path readPath = new Path(fileName);

FSDataInputStream inStream = fileSystem.open(readPath);

try {

IOUtils.copyBytes(inStream, System.out, 4096, false);

} catch (Exception e) {

// TODO: handle exception

e.printStackTrace();

} finally {

// if Exception close Stream

IOUtils.closeStream(inStream);

}

}

public static void main(String[] args) throws Exception{

//String fileName = "/user/beifeng/mapreduce/wordcount/input/wc.input";

//read(fileName);

FileSystem fileSystem = getFileSystem();

//write path

String putFileName = "/user/beifeng/put-wc.input";

Path writePath = new Path(putFileName);

FSDataOutputStream outputStream = fileSystem.create(writePath);

FileInputStream inputStream = new FileInputStream(

new File("/opt/modules/hadoop-2.5.0/wc.input"));

try {

IOUtils.copyBytes(inputStream, outputStream, 4096,false);

} catch (Exception e) {

// TODO: handle exception

inputStream.close();

outputStream.close();

}

}

}

思路

可以使用Java操作hdfs的api 制作一个基于HDFS的 云盘 ,可以对文件进行 上传 、删除、移动目录 、查看目录,但是不可以对文件的内容进行修改!


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Springboot项目使用拦截器方法详解
下一篇:账务系统接口设计图(账务系统接口设计图片)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~