HDFS API的簡單使用
需要配置的pom.XML文件
需要在<dependencies></dependencies>
中添加如下內容
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.8.4</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.8.4</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>2.8.4</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.8.4</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.10</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
在Test文件夾中創建Text文件,簡單練習
引入的包
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.junit.Test;
import java.net.URI;
import java.net.URISyntaxException;
使用API連接虛擬機,並上傳一個文件
/**
* @Test代表是個測試方法
* 上傳文件到HDFS
* @throws URISyntaxException
*/
@Test
public void putHdfs() throws Exception {
Configuration config = new Configuration();
/**連接文件,連接的是namenode所在的節點,root是用戶名*/
FileSystem fs = FileSystem.get(new URI("hdfs://bigdata2:9000"), config, "root");
/**複製文件,不刪除E盤的源文件,但是會覆蓋linux上的同名文件*/
fs.copyFromLocalFile(new Path("E:\\a.png"),new Path("hdfs://bigdata2:9000/a.png"));
fs.close();
}
創建文件夾,修改文件夾名稱,查看文件詳情,判斷是否是文件夾,刪除文件夾
代碼如下:
@Test
/**創建文件夾,修改文件夾名稱,查看文件詳情,判斷是否是文件夾,刪除文件夾*/
public void dirHdfs() throws Exception {
Configuration config = new Configuration();
/**連接文件*/
FileSystem fs = FileSystem.get(new URI("hdfs://bigdata2:9000"), config, "root");
fs.mkdirs(new Path("hdfs://bigdata2:9000/user"));
System.out.println("創建文件夾");
/**文件夾改名*/
fs.rename(new Path("hdfs://bigdata2:9000/user"),new Path("hdfs://bigdata2:9000/aaa"));
System.out.println("user文件夾改名爲aaa");
/**查看文件詳情*/
RemoteIterator<LocatedFileStatus> listfile = fs.listFiles(new Path("/"),true);
while(listfile.hasNext()){
LocatedFileStatus fileStatus = listfile.next();
System.out.println("文件名:" + fileStatus.getPath().getName());
System.out.println("文件塊大小" + fileStatus.getBlockSize());
System.out.println("文件權限" + fileStatus.getPermission());
System.out.println("文件大小" + fileStatus.getLen());
}
/**判斷是文件還是文件夾,也可以使用status.isFile();方法來進行判斷*/
FileStatus[] listStatus = fs.listStatus(new Path("/"));
for(FileStatus status : listStatus){
if (status.isDirectory()){
System.out.println("文件夾:"+status.getPath().getName());
}else if(status.isFile()){
System.out.println("文件:"+status.getPath().getName());
}
}
/**刪除文件夾*/
fs.delete(new Path("hdfs://bigdata2:9000/aaa"),true);
System.out.println("文件夾aaa已經刪除");
fs.delete(new Path("hdfs://bigdata2:9000/a.png"),true);
System.out.println("文件a已經刪除");
fs.close();
}