使用Java Api操作HDFS過程詳解
如題 我就是一個標(biāo)題黨 就是使用JavaApi操作HDFS,使用的是MAVEN,操作的環(huán)境是Linux
首先要配置好Maven環(huán)境,我使用的是已經(jīng)有的倉庫,如果你下載的jar包 速度慢,可以改變Maven 下載jar包的鏡像站改為 阿里云。
貼一下 pom.xml
使用到的jar包
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>
<!-- hadoop Client -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>${hadoop.version}</version>
</dependency>
</dependencies>
然后就是操作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的 云盤 ,可以對文件進(jìn)行 上傳 、刪除、移動目錄 、查看目錄,但是不可以對文件的內(nèi)容進(jìn)行修改!
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java兩個變量的互換(不借助第3個變量)具體實現(xiàn)方法
這篇文章主要介紹了Java兩個變量的互換(不借助第3個變量)具體實現(xiàn)方法,需要的朋友可以參考下2014-02-02
Java多線程批量數(shù)據(jù)導(dǎo)入的方法詳解
這篇文章主要介紹了Java多線程批量數(shù)據(jù)導(dǎo)入的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,下面小編和大家來一起學(xué)習(xí)下吧2019-06-06
SpringBoot整合mybatis-plus快速入門超詳細(xì)教程
mybatis-plus 是一個 Mybatis 的增強(qiáng)工具,在 Mybatis 的基礎(chǔ)上只做增強(qiáng)不做改變,為簡化開發(fā)、提高效率而生,本文給大家分享SpringBoot整合mybatis-plus快速入門超詳細(xì)教程,一起看看吧2021-09-09

