目录
获取文件系统
/**
* 获取文件系统
*/
public class getFileSystemTest {
/**
* 第一种方式:获取文件系统
* configuration:该类的对象封装了客户端或者服务器的配置
* filesystem
*/
@Test
public void getSystem() throws IOException {
// 获取configuration对象
Configuration configuration = new Configuration();
// 设置configuration对象,设置的目的是来指定我们要操作的文件系统
// hdfs://node01:8020 操作分布式文件系统, 操作元数据节点
configuration.set("fs.defaultFS" , "hdfs://node01:8020");
// 获取指定的文件系统, 获取FileSystem就相当于获取了主节点中所有的元数据信息
FileSystem fileSystem = FileSystem.get(configuration);
System.out.println("fileSystem:"+fileSystem.toString());
}
/**
* 第二种方式:获取文件系统
* configuration:该类的对象封装了客户端或者服务器的配置
* filesystem
*/
@Test
public void getSystem2() throws URISyntaxException, IOException {
FileSystem fileSystem = FileSystem.get(new URI("hdfs://node01:8020"), new Configuration());
System.out.println("fileSystem2:"+fileSystem.toString());
}
/**
* 第三种方式:获取文件系统
* configuration:该类的对象封装了客户端或者服务器的配置
* filesystem
*/
@Test
public void getSystem3() throws IOException {
Configuration configuration = new Configuration();
configuration.set("fs.defaultFS" , "hdfs://node01:8020");
FileSystem fileSystem = FileSystem.newInstance(configuration);
System.out.println("fileSystem3:"+fileSystem.toString());
}
/**
* 第四种方式:获取文件系统
* configuration:该类的对象封装了客户端或者服务器的配置
* filesystem
*/
@Test
public void getSystem4() throws Exception {
FileSystem fileSystem = FileSystem.newInstance(new URI("hdfs://node01:8020"), new Configuration());
System.out.println("fileSystem4:"+fileSystem.toString());
}
}
文件操作
public class getHDFSFileTest {
/**
* 文件上传
*/
@Test
public void uploadFile() throws URISyntaxException, IOException, InterruptedException {
// 1.获取FileSystem对象
FileSystem fileSystem = FileSystem.get(new URI("hdfs://node01:8020"), new Configuration(), "root");
// 本地拷贝到hdfs上
fileSystem.copyFromLocalFile(
new Path("C:\\Users\\baidu\\Desktop\\123\\1111.jpg"),
new Path("/study/hadoop/1111.jpg"));
fileSystem.close();
}
/**
* 方式二:下载文件
*/
@Test
public void downloadFile2() throws IOException, URISyntaxException, InterruptedException {
// 1.获取FileSystem对象 伪造用户root
FileSystem fileSystem = FileSystem.get(new URI("hdfs://node01:8020"), new Configuration(), "root");
// 2.调用方法直接实现文件的下载
fileSystem.copyToLocalFile(new Path("/newFile.txt"),
new Path("C:\\Users\\baidu\\Desktop\\newFile.txt"));
fileSystem.close();
}
/**
* 方式一:下载文件
*/
@Test
public void downloadFile() throws URISyntaxException, IOException {
// 1.获取FileSystem对象
FileSystem fileSystem = FileSystem.get(new URI("hdfs://node01:8020"), new Configuration());
// 2.获取hdfs文件的输入流
FSDataInputStream inputStream = fileSystem.open(new Path("/seats.sql"));
// 3.获取本地文件的输出流
FileOutputStream outputStream = new FileOutputStream(new File("C:\\Users\\baidu\\Desktop\\video\\hello.txt"));
// 4.实现文件下载
IOUtils.copy(inputStream, outputStream);
// 关闭流
IOUtils.closeQuietly(inputStream);
IOUtils.closeQuietly(outputStream);
fileSystem.close();
}
/**
* 在hdfs上创建文件(夹)
*/
@Test
public void mkdirHdfs() throws Exception {
// 1.获取FileSystem对象
FileSystem fileSystem = FileSystem.get(new URI("hdfs://node01:8020"), new Configuration());
// 2.创建文件夹
//fileSystem.mkdirs(new Path("/javaApi/test"));
// 3.创建文件
fileSystem.create(new Path("/javaApi/test/a.txt"));
}
/**
* 获取目录下所有文件信息
*/
@Test
public void getFileList() throws Exception {
// 1.获取FileSystem对象
FileSystem fileSystem = FileSystem.get(new URI("hdfs://node01:8020"), new Configuration());
// 2.获取remoteIterator得到所有的文件或文件夹,第一个参数指定遍历的路径,第二个参数表示是否要递归遍历
// 获取目录下所有文件信息
RemoteIterator<LocatedFileStatus> files = fileSystem.listFiles(new Path("/"), true);
while (files.hasNext()) {
LocatedFileStatus next = files.next();
// hdfs://node01:8020/study/hadoop/alibaba/阿里巴巴Java开发手册2.0.pdf
System.out.println(next.getPath().toString());
System.out.println(next.getPath().getName().toString());
// 获取文件的block存储信息
BlockLocation[] blockLocations = next.getBlockLocations();
System.out.println("每个文件的block数量:"+blockLocations.length);
for (BlockLocation blockLocation : blockLocations) {
// 每个block副本的存储位置
// 每个副本存储主机的位置
String[] hosts = blockLocation.getHosts();
for (String host : hosts) {
System.out.println(host+"===");
}
}
}
fileSystem.close();
}
}