HDFS是一個分布式文件系統(tǒng),既然是文件系統(tǒng),就可以對其文件進行操作,比如說新建文件、刪除文件、讀取文件內容等操作。下面記錄一下使用JAVA API對HDFS中的文件進行操作的過程。
對分HDFS中的文件操作主要涉及一下幾個類:
Configuration類:該類的對象封轉了客戶端或者服務器的配置。
FileSystem類:該類的對象是一個文件系統(tǒng)對象,可以用該對象的一些方法來對文件進行操作。FileSystem fs = FileSystem.get(conf);通過FileSystem的靜態(tài)方法get獲得該對象。
FSDataInputStream和FSDataOutputStream:這兩個類是HDFS中的輸入輸出流。分別通過FileSystem的open方法和create方法獲得。
具體如何對文件操作清下下面例子:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
package com.hdfs; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import org.apache.hadoop.conf.Configuration; 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.apache.hadoop.io.IOUtils; public class HdfsTest { //創(chuàng)建新文件 public static void createFile(String dst , byte [] contents) throws IOException{ Configuration conf = new Configuration(); FileSystem fs = FileSystem.get(conf); Path dstPath = new Path(dst); //目標路徑 //打開一個輸出流 FSDataOutputStream outputStream = fs.create(dstPath); outputStream.write(contents); outputStream.close(); fs.close(); System.out.println( "文件創(chuàng)建成功!" ); } //上傳本地文件 public static void uploadFile(String src,String dst) throws IOException{ Configuration conf = new Configuration(); FileSystem fs = FileSystem.get(conf); Path srcPath = new Path(src); //原路徑 Path dstPath = new Path(dst); //目標路徑 //調用文件系統(tǒng)的文件復制函數(shù),前面參數(shù)是指是否刪除原文件,true為刪除,默認為false fs.copyFromLocalFile( false ,srcPath, dstPath); //打印文件路徑 System.out.println( "Upload to " +conf.get( "fs.default.name" )); System.out.println( "------------list files------------" + "\n" ); FileStatus [] fileStatus = fs.listStatus(dstPath); for (FileStatus file : fileStatus) { System.out.println(file.getPath()); } fs.close(); } //文件重命名 public static void rename(String oldName,String newName) throws IOException{ Configuration conf = new Configuration(); FileSystem fs = FileSystem.get(conf); Path oldPath = new Path(oldName); Path newPath = new Path(newName); boolean isok = fs.rename(oldPath, newPath); if (isok){ System.out.println( "rename ok!" ); } else { System.out.println( "rename failure" ); } fs.close(); } //刪除文件 public static void delete(String filePath) throws IOException{ Configuration conf = new Configuration(); FileSystem fs = FileSystem.get(conf); Path path = new Path(filePath); boolean isok = fs.deleteOnExit(path); if (isok){ System.out.println( "delete ok!" ); } else { System.out.println( "delete failure" ); } fs.close(); } //創(chuàng)建目錄 public static void mkdir(String path) throws IOException{ Configuration conf = new Configuration(); FileSystem fs = FileSystem.get(conf); Path srcPath = new Path(path); boolean isok = fs.mkdirs(srcPath); if (isok){ System.out.println( "create dir ok!" ); } else { System.out.println( "create dir failure" ); } fs.close(); } //讀取文件的內容 public static void readFile(String filePath) throws IOException{ Configuration conf = new Configuration(); FileSystem fs = FileSystem.get(conf); Path srcPath = new Path(filePath); InputStream in = null ; try { in = fs.open(srcPath); IOUtils.copyBytes(in, System.out, 4096 , false ); //復制到標準輸出流 } finally { IOUtils.closeStream(in); } } public static void main(String[] args) throws IOException { //測試上傳文件 //uploadFile("D:\\c.txt", "/user/hadoop/test/"); //測試創(chuàng)建文件 /*byte[] contents = "hello world 世界你好\n".getBytes(); createFile("/user/hadoop/test1/d.txt",contents);*/ //測試重命名 //rename("/user/hadoop/test/d.txt", "/user/hadoop/test/dd.txt"); //測試刪除文件 //delete("test/dd.txt"); //使用相對路徑 //delete("test1"); //刪除目錄 //測試新建目錄 //mkdir("test1"); //測試讀取文件 readFile( "test1/d.txt" ); } } |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://www.cnblogs.com/liuling/p/2013-6-17-01.html