1、導包
<!-- 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>
2、連接配接hadoop
public class hadoopconn {
@Test
public void getConn()throws Exception{
Configuration configuration = new Configuration();
//可以設定一些屬性,設定副本數
configuration.set("dfs.replication","1");
//uri擷取連接配接的位址
URI uri = new URI("hdfs://master:9000");
//使用靜态方法連接配接
FileSystem fs = FileSystem.get(uri, configuration);
//進行操作,建立一個目錄
boolean b = fs.mkdirs(new Path("/test"));
System.out.println(b);
}
@Test
public void getDelete()throws Exception{
//後面的boolean表示疊代删除,true:可以,false:不可以
boolean b = fs.delete(new Path("/test"), true);
System.out.println(b);
}
}
3、檢視目錄下檔案詳細資訊
//檢視data檔案下目錄清單
@Test
public void getStatus()throws Exception{
FileStatus[] fileStatuses = fs.listStatus(new Path("/data"));
//擷取一個一個單個檔案
for (FileStatus f : fileStatuses) {
System.out.println(f.getBlockSize()); //block塊128
System.out.println(f.getLen()); //檔案實際大小
System.out.println(f.getGroup()); //組
System.out.println(f.getOwner()); //所屬者
System.out.println(f.getPath()); //路徑
。。。。。。
}
}
4、檢視檔案裡面的詳細資訊(存儲塊多少,哪個節點等資訊)
//檢視data檔案下目錄清單
@Test
public void getStatus()throws Exception{
FileStatus[] fileStatuses = fs.listStatus(new Path("/data"));
//擷取一個一個單個檔案
for (FileStatus f : fileStatuses) {
System.out.println(f.getBlockSize()); //塊128
System.out.println(f.getLen()); //檔案大小
System.out.println(f.getGroup()); //組
System.out.println(f.getOwner()); //所屬者
System.out.println(f.getPath()); //路徑
}
}
//擷取檔案存儲詳細資訊
@Test
public void getBlock() throws Exception {
BlockLocation[] bl = fs.getFileBlockLocations(new Path("/data/student.txt"), 0, 100000000);
for (BlockLocation b : bl) {
//檔案存儲的節點,由于每個塊隻能存128,所有可能有多個塊存多個節點上
String[] hosts = b.getHosts();
for (String host : hosts) {
System.out.println(host);
}
//檔案資料的大小
System.out.println(b.getLength());
//還是每個節點資料的主機機器
String[] names = b.getNames();
for (String name : names) {
System.out.println(name);
}
//存完128之後剩餘的量為偏移量
System.out.println(b.getOffset());
//節點
String[] topologyPaths = b.getTopologyPaths();
for (String s : topologyPaths) {
System.out.println(s);
}
}
}
}
5、讀取檔案,使用open打開檔案擷取流,讀取
@Test
public void getopen() throws Exception{
//使用open方法打開檔案檔案
FSDataInputStream inputStream = fs.open(new Path("/data/student.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line=br.readLine())!=null){
System.out.println(line);
}
br.close();
inputStream.close();
}
6、上傳寫入檔案,hdfs上檔案不能被修改,隻能覆寫,不能追加
使用create建立檔案儲存資料
@Test
public void getPut()throws Exception{
FSDataOutputStream os = fs.create(new Path("/data/test.txt"));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
bw.write("你好");
bw.flush();
bw.close();
}
}