HDFS常用API
阿新 • • 發佈:2021-09-22
在pom.xml中匯入依賴包
<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-hdfs -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>2.7.6</version>
</dependency>
<!-- https:// mvnrepository.com/artifact/org.apache.hadoop/hadoop-common -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.7.6</version>
</dependency>
<!-- https:// mvnrepository.com/artifact/org.apache.hadoop/hadoop-client -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.7.6</version>
</dependency>
<!-- https:// mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.3</version>
</dependency>
</dependencies>
hdfs連線Java
1.先獲取配置檔案(hdfs-site.xml)
Configuration cg = new Configuration(); 匯入的是import org.apache.hadoop.conf.Configuration;
cg.set("dfs.replication","1"); 1是備份數量
2.獲取連線地址(core-site.xml)
URI uri = new URI("hdfs://master:9000");
3.建立(獲取)hdfs檔案管理系統的物件,同過物件操作hdfs
FileSystem fs = FileSystem.get(uri, cg);
常用HDFSAPI:
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.junit.Before;
import org.junit.Test;
import java.io.*;
import java.net.URI;
public class HdfsApi {
FileSystem fs;
@Before
public void main() throws Exception {
Configuration cg = new Configuration();
cg.set("dfs.replication", "1");
URI uri = new URI("hdfs://master:9000");
fs = FileSystem.get(uri, cg);
}
@Test
public void mk() throws IOException {
boolean mk = fs.mkdirs(new Path("/test"));
}
@Test
public void del() throws IOException {
//false表示不迭代刪除也可以不加,true可以進行多目錄迭代刪除
boolean del = fs.delete(new Path("/test"),false);
System.out.println(del);
}
@Test
public void listStatus() throws IOException { //對比圖在下
//檢視目錄下檔案列表
FileStatus[] fileStatuses = fs.listStatus(new Path("/data/data"));
System.out.println(fileStatuses); //[Lorg.apache.hadoop.fs.FileStatus;@106cc338
System.out.println("-------------------------------");
for (FileStatus fileStatus : fileStatuses) {
System.out.println(fileStatus.getLen()); //檔案大小 以B位元組為單位
System.out.println(fileStatus.getReplication()); //副本個數
System.out.println(fileStatus.getPermission()); //讀寫狀態
System.out.println(fileStatus.getBlockSize()); //固定的一個block大小128MB
System.out.println(fileStatus.getAccessTime()); //建立檔案時的時間戳
System.out.println(fileStatus.getPath()); //檔案路徑
System.out.println("-----------------");
}
@Test
public void listBlockLocation() throws IOException { //對比圖在下
BlockLocation[] fbl =
fs.getFileBlockLocations(
new Path("/data/data/students.txt"),0,1000000000);
for (BlockLocation bl : fbl) {
String[] hosts = bl.getHosts();
for (String host : hosts) {
System.out.println(host);
} //node1 表示檔案存在node1,因為檔案小於一個block,所以這裡只存在一個節點
System.out.println(bl.getLength()); //41998 size大小
String[] names = bl.getNames();
for (String name : names) {
System.out.println(name);
} //192.168.163.120:50010 node1的地址
System.out.println(bl.getOffset()); //0 偏移量
String[] topologyPaths = bl.getTopologyPaths();
for (String topologyPath : topologyPaths) {
System.out.println(topologyPath);
} // /default-rack/192.168.163.120:50010
}
}
@Test
public void open() throws IOException {
FSDataInputStream open = fs.open(new Path("/data/data/students.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(open)); //因為檔案中有中文,所以將位元組流轉為字元流來讀取
String len;
while ((len=br.readLine())!=null){
System.out.println(len);
}
br.close();
}
@Test
public void create() throws IOException {
FSDataOutputStream fos = fs.create(new Path("/data/data/test.txt"));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
bw.write("你好");
bw.write("世界");
bw.newLine();
bw.write("我和我的祖國");
bw.flush();
bw.close();
}
}