這篇文章將為大家詳細(xì)講解有關(guān)HDFS文件操作有哪些,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

目前創(chuàng)新互聯(lián)已為千余家的企業(yè)提供了網(wǎng)站建設(shè)、域名、雅安服務(wù)器托管、綿陽(yáng)服務(wù)器托管、企業(yè)網(wǎng)站設(shè)計(jì)、新蔡網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶(hù)導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶(hù)和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。
HDFS的文件操作
格式化HDFS
命令:user@namenode :Hadoop$bin/hadoop namenode -format
啟動(dòng)HDFS
命令:user@namenode :hadoop$bin/start-dfs.sh
列出HDFS上的文件
命令:user@namenode :hadoop$bin/hadoop dfs -ls
使用hadoop API
public List<String[]>GetFileBolckHost(Configuration conf, String FileName) {
try {
List<String[]> list = new ArrayList<String[]>();
FileSystem hdfs = FileSystem.get(conf);
Path path = new Path(FileName);
FileStatus fileStatus = hdfs.getFileStatus(path);
BlockLocation[] blkLocations = hdfs.getFileBlockLocations(
fileStatus, 0,fileStatus.getLen());
int blkCount = blkLocations.length;
for (int i = 0; i < blkCount; i++) {
String[] hosts =blkLocations.getHosts();
list.add(hosts);
}
return list;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
在HDFS上創(chuàng)建目錄
命令:user@namenode :hadoop$bin/hadoop dfs -mkdir /文件名
使用hadoop API
// 在HDFS新建文件
public FSDataOutputStream CreateFile(Configuration conf, StringFileName) {
try {
FileSystem hdfs = FileSystem.get(conf);
Path path = new Path(FileName);
FSDataOutputStream outputStream = hdfs.create(path);
return outputStream;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
上傳一個(gè)文件到HDFS
命令:user@namenode :Hadoop$ bin/hadoopdfs -put 文件名/user/yourUserName/
使用hadoop API
// 上傳文件到HDFS
public voidPutFile(Configuration conf, String srcFile, String dstFile) {
try {
FileSystem hdfs =FileSystem.get(conf);
Path srcPath = newPath(srcFile);
Path dstPath = newPath(dstFile);
hdfs.copyFromLocalFile(srcPath, dstPath);
} catch (IOExceptione) {
e.printStackTrace();
}
}
從 HDFS 中導(dǎo)出數(shù)據(jù)
命令:user@namenode:hadoop$ bin/hadoopdfs -cat foo
使用hadoop API
// 從HDFS讀取文件
public voidReadFile(Configuration conf, String FileName) {
try {
FileSystem hdfs =FileSystem.get(conf);
FSDataInputStreamdis = hdfs.open(new Path(FileName));
IOUtils.copyBytes(dis, System.out, 4096, false);
dis.close();
} catch (IOExceptione) {
e.printStackTrace();
}
}
HDFS 的關(guān)閉
命令:user@namenode:hadoop$bin/stop-dfs.sh
HDFS全局狀態(tài)信息
命令:bin/Hadoop dfsadmin -report
我們可以得到一份全局狀態(tài)報(bào)告。這份報(bào)告包含了HDFS集群的基本信息,當(dāng)然也有每臺(tái)機(jī)器的一些情況。
以上講的都是本地操作HDFS,都是基于在Ubuntu下并配置有hadoop環(huán)境下對(duì)HDFS的操作,作為客戶(hù)端也可以在window系統(tǒng)下遠(yuǎn)程的對(duì) HDFS進(jìn)行操作,其實(shí)原理基本上差不多,只需要集群中namenode對(duì)外開(kāi)放的IP和端口,就可以訪問(wèn)到HDFS
/**
* 對(duì)HDFS操作
* @author yujing
*
*/
public class Write {
public static voidmain(String[] args) {
try {
uploadTohdfs();
readHdfs();
getDirectoryFromHdfs();
} catch(FileNotFoundException e) {
e.printStackTrace();
} catch (IOExceptione) {
e.printStackTrace();
}
}
public static voiduploadTohdfs() throws FileNotFoundException, IOException {
String localSrc ="D://qq.txt";
String dst ="hdfs://192.168.1.11:9000/usr/yujing/test.txt";
InputStream in = newBufferedInputStream(new FileInputStream(localSrc));
Configuration conf =new Configuration();
FileSystem fs =FileSystem.get(URI.create(dst), conf);
OutputStream out =fs.create(new Path(dst), new Progressable() {
public voidprogress() {
System.out.println(".");
}
});
System.out.println("上傳文件成功");
IOUtils.copyBytes(in,out, 4096, true);
}
/** 從HDFS上讀取文件 */
private static voidreadHdfs() throws FileNotFoundException, IOException {
String dst ="hdfs://192.168.1.11:9000/usr/yujing/test.txt";
Configuration conf =new Configuration();
FileSystem fs =FileSystem.get(URI.create(dst), conf);
FSDataInputStreamhdfsInStream = fs.open(new Path(dst));
OutputStream out = newFileOutputStream("d:/qq-hdfs.txt");
byte[] ioBuffer = newbyte[1024];
int readLen =hdfsInStream.read(ioBuffer);
while (-1 != readLen){
out.write(ioBuffer, 0, readLen);
readLen =hdfsInStream.read(ioBuffer);
}
System.out.println("讀文件成功");
out.close();
hdfsInStream.close();
fs.close();
}
/**
* 以append方式將內(nèi)容添加到HDFS上文件的末尾;注意:文件更新,需要在hdfs-site.xml中添<property><name>dfs.
*append.support</name><value>true</value></property>
*/
private static voidappendToHdfs() throws FileNotFoundException,
IOException {
String dst ="hdfs://192.168.1.11:9000/usr/yujing/test.txt";
Configuration conf =new Configuration();
FileSystem fs =FileSystem.get(URI.create(dst), conf);
FSDataOutputStream out= fs.append(new Path(dst));
int readLen ="zhangzk add by hdfs java api".getBytes().length;
while (-1 != readLen){
out.write("zhangzk add by hdfs java api".getBytes(), 0,readLen);
}
out.close();
fs.close();
}
/** 從HDFS上刪除文件 */
private static voiddeleteFromHdfs() throws FileNotFoundException,
IOException {
String dst ="hdfs://192.168.1.11:9000/usr/yujing";
Configuration conf =new Configuration();
FileSystem fs =FileSystem.get(URI.create(dst), conf);
fs.deleteOnExit(newPath(dst));
fs.close();
}
/** 遍歷HDFS上的文件和目錄 */
private static voidgetDirectoryFromHdfs() throws FileNotFoundException,
IOException {
String dst ="hdfs://192.168.1.11:9000/usr/yujing";
Configuration conf =new Configuration();
FileSystem fs =FileSystem.get(URI.create(dst), conf);
FileStatus fileList[]= fs.listStatus(new Path(dst));
int size =fileList.length;
for (int i = 0; i <size; i++) {
System.out.println("文件名name:" + fileList.getPath().getName()
+ "文件大小/t/tsize:" +fileList.getLen());
}
fs.close();
}
}
我們可以通過(guò)http://主機(jī)IP:50030就可以查看集群的所有信息,也可以查看到自己上傳到HDFS上的文件。
關(guān)于“HDFS文件操作有哪些”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
                分享名稱(chēng):HDFS文件操作有哪些
                
                文章源于:http://chinadenli.net/article34/goijpe.html
            
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、App開(kāi)發(fā)、營(yíng)銷(xiāo)型網(wǎng)站建設(shè)、網(wǎng)站策劃、微信小程序、用戶(hù)體驗(yàn)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
