您好,登錄后才能下訂單哦!
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.URL;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.BlockLocation;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.FsUrlStreamHandlerFactory;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.junit.Test;
/**
* 測試hadoop文件系統(tǒng)的API
* @author Administrator
*
*/
public class readfilestatus {
/**
* 通過filesystem對象API讀取HDFS文件數(shù)據(jù)
*
* @author Administrator
*
* 1.創(chuàng)建configuration對象,注意這里如果涉及到某些參數(shù),需要自己書寫指定在src目錄下
* 2.利用filesystem的get方法來獲取filesystem對象,get方法一共有三種(關鍵)
* 3.利用filesystem的open方法打開一個數(shù)據(jù)輸入流,方法過程中需要一個path對象的參數(shù)傳遞(關鍵)
* 4.利用ioutils工具類實現(xiàn)文件的輸出
*
*
*
*/
@Test
public void read() throws Exception {
//創(chuàng)建configuration對象,有個默認的加載順序,先從core-default.xml,再到src目錄中的文件,這里
//我們給定了
Configuration conf = new Configuration();
//可以手動添加指定的配置文件
// conf.addResource("my-core-site.xml");
//通過conf的configuration對象創(chuàng)建了該分布式文件系統(tǒng)fs,默認如果不指定文件的話為本地文件系統(tǒng)
FileSystem fs = FileSystem.get(conf);
//定義一個URL的字符串
String file = "hdfs://hadoop01/user/hadoop/data2/demo.txt";
//通過一個URL的字符串構建一個path對象,其實就是文件的地址
Path path = new Path(file);
//得到制定路徑下面的文件的狀態(tài)
FileStatus st = fs.getFileStatus(path);
System.out.println("st.getBlockSize() = " + st.getBlockSize());
System.out.println("st.getAccessTime() = " + st.getAccessTime());
System.out.println("st.getGroup() = " + st.getGroup());
System.out.println("st.getLen() = " + st.getLen());
System.out.println("st.getModificationTime() = " + st.getModificationTime());
System.out.println("st.getOwner() = " + st.getOwner());
System.out.println("st.getReplication() = " + st.getReplication());
System.out.println("st.getClass() = " + st.getClass());
System.out.println("st.getPath() = " + st.getPath());
System.out.println("st.getPermission() = " + st.getPermission());
System.out.println("-----------------------------------");
// System.out.println("st.getSymlink() = " + st.getSymlink());
//得到指定文件狀態(tài)的塊位置信息集合
//把整個文件從0字節(jié)開始到整個文件的全長的字節(jié)
BlockLocation[] locations = fs.getFileBlockLocations(st, 0, st.getLen());
for(BlockLocation block : locations){
System.out.println(block.getLength());
System.out.println(block.getOffset());
System.out.println(block.getCachedHosts());
System.out.println(block.getHosts());
System.out.println(block.getNames());
System.out.println(block.getTopologyPaths());
}
}
}
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。