1. 创建文件夹
工程的test包中java->com.imooc.bigdata->hadoop.hdfs.HDFSApp
注意包:
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import java.net.URI;
/**
* 使用JAVA API操作HDFS文件系统
* 1)创建Configuration
* 2)获取FileSystem
* 3)HDFS API操作
*/
public class HDFSApp {
public static void main(String[] args) throws Exception {
Configuration configuration = new Configuration();
FileSystem fileSystem = FileSystem.get(new URI("hdfs://hadoop000:8020"),configuration,"root");
Path path = new Path("/hdfsapi/test");
boolean result = fileSystem.mkdirs(path);
System.out.println(result);
}
}
提示错误:
解决:
C:\WINDOWS\system32\drivers\etc\hosts中
改为:
测试:
——————————————————————————————
使用Junit改写:
public class HDFSApp {
public static final String HDFS_PATH = "hdfs://hadoop000:8020";
FileSystem fileSystem = null;
Configuration configuration = null;
@Before
public void setUp() throws Exception {
System.out.println("---------setUp----------");
configuration = new Configuration();
fileSystem = FileSystem.get(new URI(HDFS_PATH),configuration,"root");
}
@After
public void tearDown(){
configuration = null;
fileSystem = null;
System.out.println("-------tearDown----------");
}
@Test
//创建HDFS文件夹
public void mkdir() throws Exception {
fileSystem.mkdirs(new Path("/hdfsapi/test2"));
}
}
测试:
2.查看文件内容
添加方法:
@Test
public void text() throws IOException {
FSDataInputStream in = fileSystem.open(new Path("/README.txt"));
IOUtils.copyBytes(in,System.out,1024);
}
包选:
import org.apache.hadoop.io.IOUtils;
测试:
3.创建文件并写入内容
@Test
public void create() throws IOException {
FSDataOutputStream out = fileSystem.create(new Path("/hdfsapi/test/a.txt"));
out.writeUTF("hello pk");
out.flush();
out.close();
}
执行后:
问题:
副本系数问题
http://192.168.61.129:50070/explorer.html#/
现在的副本系数为3
因为设置了一个空的Configuration对象,所以会自动加载依赖包中hdfs-default.xml中的配置
设置:
configuration.set(“dfs.replication”,“1”);
此时:
编码问题
查看idea编码:
修改为:
暂定:
应当是Windows系统和Centos系统方面的问题
4.文件重命名
@Test
public void rename() throws IOException {
Path oldPath = new Path("/hdfsapi/test/a.txt");
Path newPath = new Path("/hdfsapi/test/aaaaa.txt");
boolean result = fileSystem.rename(oldPath, newPath);
System.out.println(result);
}
测试结果:
5.拷贝本地文件到HDFS文件系统
@Test
public void copyFromLocalFile() throws IOException {
Path src = new Path("C:\\Users\\DELL\\Desktop\\新建文本文档 (2).txt");
Path dst = new Path("/hdfsapi/test/");
fileSystem.copyFromLocalFile(src,dst);
}
测试:
虽然出现了转义的问题,但是加上’'单引号就没问题了呢~
6.带进度的上传大文件(拷贝本地大文件到HDFS文件系统)
使用函数:
import java.io.*;
@Test
public void copyFromBigLocalFile() throws IOException {
InputStream in = new BufferedInputStream(new FileInputStream(new File("G:\\jdk-8u231-linux-x64.tar.gz")));
FSDataOutputStream out = fileSystem.create(new Path("/hdfsapi/test/jdk.tgz"), new Progressable() {
@Override
public void progress() {
System.out.print(".");
}
});
IOUtils.copyBytes(in,out,4096);
}
测试结果:
7.从HDFS拷贝文件到本地系统(下载)
@Test
public void copyToLocalFile() throws IOException {
Path src = new Path("/README.txt");
Path dst = new Path("C:\\Users\\DELL\\Desktop\\");
fileSystem.copyToLocalFile(false,src,dst,true);
}
出现空指针错误:
解决:
使用
fileSystem.copyToLocalFile(false,src,dst,true);
8.列出文件夹下的所有内容
@Test
public void listFiles() throws IOException {
FileStatus[] statuses = fileSystem.listStatus(new Path("/hdfsapi/test"));
for(FileStatus file : statuses){
String isDir = file.isDirectory() ? "文件夹" : "文件";
String permission = file.getPermission().toString();
short replication = file.getReplication();
long len = file.getLen();
String path = file.getPath().toString();
System.out.println(isDir + "\t" + permission + "\t"
+ replication + "\t" + len + "\t" + path
);
}
}
测试:
9.递归查看目标文件夹下的所有文件
对应的HDFS命令为:
代码:
public void listFilesRecursive() throws IOException {
RemoteIterator<LocatedFileStatus> files = fileSystem.listFiles(new Path("/"), true);
while(files.hasNext()){
LocatedFileStatus file = files.next();
String isDir = file.isDirectory() ? "文件夹" : "文件";
String permission = file.getPermission().toString();
short replication = file.getReplication();
long len = file.getLen();
String path = file.getPath().toString();
System.out.println(isDir + "\t" + permission + "\t"
+ replication + "\t" + len + "\t" + path
);
}
}
测试:
10.查看文件块信息
@Test
public void getFileBlockLocations() throws IOException {
FileStatus fileStatus = fileSystem.getFileStatus(new Path("/hdfsapi/test/jdk.tgz"));
BlockLocation[] blocks = fileSystem.getFileBlockLocations(fileStatus, 0, fileStatus.getLen());
for(BlockLocation block : blocks){
for (String name : block.getNames()){
System.out.println(name + " : " + block.getOffset() + " : " + block.getLength() + " : " + block.getHosts());
}
}
}
测试:
11.递归删除文件
@Test
public void delete() throws IOException {
boolean result = fileSystem.delete(new Path("/hdfsapi/test/jdk.tgz"));
System.out.println(result);
}
测试: