1:創(chuàng)建一個(gè)可以上傳到hdfs文件系統(tǒng)的文件(wc.input)
2:編寫java代碼調(diào)用api完成附件的上傳相當(dāng)于執(zhí)行-put命令
3:查看執(zhí)行結(jié)果
全部代碼
package com.lizh.hadoop.hdfs;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
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;
public class HdfsApp {
public static FileSystem getFileSystem() throws IOException{
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(conf);
return fs;
}
public static void readFile(String path1) {
try {
FileSystem fs = getFileSystem();
Path path = new Path(path1);
FSDataInputStream in = fs.open(path);
IOUtils.copyBytes(in, System.out, 4000, false);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception{
//String path ="/user/beifeng/mapreduce/wordcount/input/wc.input";
//readFile(path);
//把/opt/modules/hadoop-2.5.0/wc.input 內(nèi)容寫入到 /user/beifeng/put-wc.input
//write file
FileInputStream fisin =null;
FSDataOutputStream fsoutstream =null;
try {
String filename="/user/beifeng/put-wc.input";//文件系統(tǒng)目錄
Path inputpath = new Path(filename);
FileSystem fs = getFileSystem();
fsoutstream = fs.create(inputpath);
fisin = new FileInputStream(new File("/opt/modules/hadoop-2.5.0/wc.input"));//本地系統(tǒng)目錄文件
IOUtils.copyBytes(fisin, fsoutstream, 4000, false);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally{
IOUtils.closeStream(fsoutstream);
IOUtils.closeStream(fisin);
}
}
}
寫文件流程圖
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。