?
?
?新建一個Java項目,導入jar,新建一個測試類,編寫代碼實現文件操作功能:
?
package com.bw.test;
?
import java.io.FileInputStream;
import java.io.FileOutputStream;
?
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
?
import org.apache.commons.compress.utils.IOUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.junit.Before;
import org.junit.Test;
/**
?*
?* @類描述 :
?* @作者 :
?* @日期 : 2016-7-8 下午5:10:22
?*
?*/
public class test3 {
?
??? //hdfs操作入口
??? FileSystem fileSystem = null;
???
??? @Before
??? public void setUp() throws Exception {
??????? URI uri = new URI("hdfs://192.192.192.3:9000");
??????? Configuration configuration = new Configuration();
??????? fileSystem = FileSystem.get(uri, configuration,"root");
??? }
??? @Test
??? public void list() throws Exception {
??????? //遞歸方式取得指定目錄下的文件信息
??????? FileStatus[] liFileStatus = fileSystem.listStatus(new Path("/"));
??????? //輸出取得的文件權限和文件名稱
??????? for (FileStatus fileStatus : liFileStatus) {
??????????? System.out.println(fileStatus.getPermission()+" "+fileStatus.getPath());
??????? }
??? }
??? //上傳
??? @Test
??? public void put() throws Exception {
??????? //是否覆蓋同名文件(前提是也在同一目錄下)標志位
??????? boolean overwrite = true;
??????? //DFS文件輸出流
??????? FSDataOutputStream outputStream = fileSystem.create(new Path("/hbase-1.1.2-bin.tar.gz"),overwrite);
??????? //本地輸入流
??????? InputStream inputStream = new FileInputStream("C:\Users\YAN\Desktop\開發(fā)指南\Hadoop\05. HBase使用指南\安裝程序\hbase-1.1.2-bin.tar.gz");
??????? //使用工具類上傳
??????? IOUtils.copy(inputStream, outputStream,4096);
??? }
??? //下載
??? @Test
??? public void get() throws Exception {
??????? //DFS文件輸入流
??????? FSDataInputStream inputStreamOption = fileSystem.open(new Path("/hbase-1.1.2-bin.tar.gz"));
??????? //本地文件輸出流
??????? OutputStream outputStream = new FileOutputStream("E:\hbase-1.1.2-bin.tar.gz");
??????? //使用工具類下載
??????? IOUtils.copy(inputStreamOption, outputStream,4096);
??? }
??? //刪除
??? @Test
??? public void del() throws Exception {
??????? //是否刪除
??????? boolean b = true;
??????? //DFS文輸入流
??????? fileSystem.delete(new Path("/input"),b);
??? }
?
}
?
本文摘自 :https://blog.51cto.com/u