使用Java Api操作HDFS過程詳解
如題 我就是一個(gè)標(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 制作一個(gè)基于HDFS的 云盤 ,可以對文件進(jìn)行 上傳 、刪除、移動(dòng)目錄 、查看目錄,但是不可以對文件的內(nèi)容進(jìn)行修改!
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
教你快速搭建sona服務(wù)及idea使用sona的方法
Sonar 是一個(gè)用于代碼質(zhì)量管理的開放平臺(tái)。通過插件機(jī)制,Sonar 可以集成不同的測試工具,代碼分析工具,以及持續(xù)集成工具,本文給大家分享搭建sona服務(wù)及idea使用sona的方法,感興趣的朋友一起看看吧2021-06-06
Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(35)
下面小編就為大家?guī)硪黄狫ava基礎(chǔ)的幾道練習(xí)題(分享)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧,希望可以幫到你2021-07-07
python實(shí)戰(zhàn)之德州撲克第一步-發(fā)牌
這篇文章主要介紹了python實(shí)戰(zhàn)之德州撲克第一步-發(fā)牌,文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)python的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-04-04
詳解Java的MyBatis框架與Spring框架整合中的映射器注入
映射器注入方式可以將MyBatis與Spring映射好的XML文件實(shí)現(xiàn)配置共用,這里我們就來詳解Java的MyBatis框架與Spring框架整合中的映射器注入:2016-06-06
Maven安裝本地的jar包和創(chuàng)建帶模板的自定義項(xiàng)目的操作過程
這篇文章主要介紹了Maven安裝本地的jar包和創(chuàng)建帶模板的自定義項(xiàng)目,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-03-03

