HDFS API操作
一、导入依赖包
在File->Project Structure->Modules->Dependencies下导入Hadoop->share文件下的相应jar包,包括:
common下的:hadoop-common-3.2.1.jar、
hadoop-nfs-3.2.1.jar以及所有lib下的jar包
hdfs下以及hdfs/lib下的所有jar包

二、api操作
package hdfs;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.fs.permission.FsPermission;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.util.Progressable;
import org.junit.Test;
import java.io.*;import java.net.URI;
public class hdfsapi {
/**
* ./hadoop fs -ls /
* @throws IOException
*/
@Test
public void createFolder() throws IOException {
Configuration conf=new Configuration();
FileSystem fs=FileSystem.get(URI.create("hdfs://192.168.253.128:9000"),conf);
Path path=new Path("/test2");
fs.mkdirs(path, FsPermission.getDirDefault());
System.out.println("finished");
}
/**
* ./hadoop fs -ls /test2/
* @throws IOException
*/
@Test
public void createFile() throws IOException {
Configuration conf=new Configuration();
FileSystem fs=FileSystem.get(URI.create("hdfs://192.168.253.128:9000"),conf);
Path path=new Path("/test2/a1.txt");
FSDataOutputStream out = fs.create(path);
out.write("11111".getBytes());
System.out.println("finished");
}
/**
* ./hadoop fs -ls /test2/
* @throws IOException
*/
@Test
public void renameFile() throws IOException {
Configuration conf=new Configuration();
FileSystem fs=FileSystem.get(URI.create("hdfs://192.168.253.128:9000"),conf);
Path path=new Path("/test2/a1.txt");
Path path2=new Path("/test2/a2.txt");
boolean result = fs.rename(path, path2);
System.out.println(result);
}
/**
* ./hadoop fs -ls /test2/
* @throws IOException
*/
@Test
public void uploadFile() throws IOException {
Configuration conf=new Configuration();
FileSystem fs=FileSystem.get(URI.create("hdfs://192.168.253.128:9000"),conf);
Path path=new Path("F:\\5、开发语言\\java\\it.cast\\knowledge\\src\\main\\resources\\1.txt");
Path path2=new Path("/test2/a3.txt");
fs.copyFromLocalFile(path,path2);
System.out.println("finished.");
}
/**
* 上传文件方式2 显示进度
* @throws IOException
*/
@Test
public void uploadFile2() throws IOException {
Configuration conf=new Configuration();
FileSystem fs=FileSystem.get(URI.create("hdfs://192.168.253.128:9000"),conf);
InputStream in =new BufferedInputStream(new FileInputStream(new File("D:\\software\\CodeSmithGeneratoTemplates.7z")));
FSDataOutputStream out=fs.create(new Path("/test2/a4.7z"),new Progressable(){
@Override
public void progress(){
System.out.println("...");
}
});
IOUtils.copyBytes(in,out,4096);
System.out.println("finished.");
}
/**
* ./hadoop fs -ls /test2/
* @throws IOException
*/
@Test
public void getFileList() throws IOException {
Configuration conf=new Configuration();
FileSystem fs=FileSystem.get(URI.create("hdfs://192.168.253.128:9000"),conf);
Path path=new Path("/test2");
FileStatus[] fileStatuses = fs.listStatus(path);
for (FileStatus fileStatus:fileStatuses) {
System.out.println(fileStatus.getPath());
}
}
/**
* ./hadoop fs -ls /test2/
* @throws IOException
*/
@Test
public void getFileBlock() throws IOException {
Configuration conf=new Configuration();
FileSystem fs=FileSystem.get(URI.create("hdfs://192.168.253.128:9000"),conf);
Path path=new Path("/test2/a4.7z");
FileStatus fileStatus=fs.getFileStatus(path);
BlockLocation[] fileBlockLocations = fs.getFileBlockLocations(fileStatus, 0, fileStatus.getLen());
for(BlockLocation loc:fileBlockLocations){
for(int i=0;i<loc.getHosts().length;i++){
System.out.println(loc.getHosts()[i]);
}
}
}
}
三、创建问题
1、提示无权限

vim hdfs-site.xml
<configuration>
<property>
<name>dfs.permissions.enabled</name>
<value>false</value>
</property>
</configuration>
分类:
【大数据技术】Hadoop
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix