package com.future.hdfs;
import org.apache.commons.io.IOUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.junit.Before;
import org.junit.Test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
public class HDFSTest {
private static final String NAME = "fs.defaultFS";
private static final String VALUE = "hdfs://node1:8020";
private Configuration conf;
private FileSystem fs;
@Test
public void test1() throws Exception {
Configuration conf = new Configuration();
conf.set(HDFSTest.NAME, HDFSTest.VALUE);
FileSystem fileSystem = FileSystem.get(conf);
RemoteIterator<LocatedFileStatus> listFiles = fileSystem.listFiles(new Path("/"), false);
while (listFiles.hasNext()) {
LocatedFileStatus fileStatus = listFiles.next();
Path path = fileStatus.getPath();
System.out.println(path.getName());
}
}
@Test
public void test2() throws Exception {
Configuration conf = new Configuration();
conf.set(HDFSTest.NAME, HDFSTest.VALUE);
FileSystem fileSystem = FileSystem.newInstance(conf);
fileSystem.create(new Path("/FUTURE/mydir/test"));
fileSystem.mkdirs(new Path("/FUTURE1/mydir/test"));
fileSystem.close();
}
@Test
public void test3() throws Exception {
Configuration conf = new Configuration();
conf.set(HDFSTest.NAME, HDFSTest.VALUE);
FileSystem fileSystem = FileSystem.get(conf);
fileSystem.copyToLocalFile(new Path("/FUTURE/mydir/test"), new Path("file:///D:\\temp\\JD_Phone"));
fileSystem.close();
}
@Test
public void test4() throws Exception {
FileSystem fileSystem = FileSystem.get(new URI(HDFSTest.VALUE), new Configuration());
FSDataInputStream inputStream = fileSystem.open(new Path("/FUTURE/mydir/test"));
FileOutputStream outputStream = new FileOutputStream(new File("D:\\temp\\JD_Phone\\test.txt"));
IOUtils.copy(inputStream, outputStream);
IOUtils.closeQuietly(inputStream);
IOUtils.closeQuietly(outputStream);
FileSystem.closeAll();
}
@Before
public void init() {
conf = new Configuration();
try {
fs = FileSystem.get(new URI(HDFSTest.VALUE), conf);
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void test5() throws IOException {
FileStatus[] fileStatus = fs.listStatus(new Path("/"));
for (FileStatus file : fileStatus) {
System.out.println("Name:" + file.getPath().getName());
System.out.println(file.isFile() ? "file" : "directory");
}
}
@Test
public void testLs() throws Exception {
RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/FUTURE"), true);
while (listFiles.hasNext()) {
LocatedFileStatus file = listFiles.next();
System.out.println("Name:" + file.getPath().getName());
System.out.println("Permission:" + file.getPermission());
System.out.println("BlockSize:" + file.getBlockSize());
System.out.println("Owner:" + file.getOwner());
System.out.println("Replication:" + file.getReplication());
BlockLocation[] blockLocations = file.getBlockLocations();
for (BlockLocation blockLocation : blockLocations) {
System.out.println("Block:" + blockLocation.getOffset());
System.out.println("Block lenth:" + blockLocation.getLength());
for (String host : blockLocation.getHosts()) {
System.out.println("Block host:" + host);
}
}
}
}
@Test
public void test6() throws Exception {
try {
fs.copyFromLocalFile(new Path("file:///D:\\temp\\2020-09-10\\5331d040-c753-4ba3-ac8e-2e771afce6f2.jpg"), new Path("/"));
} finally {
fs.close();
}
}
@Test
public void testDelete() throws Exception {
try {
fs.delete(new Path("/5331d040-c753-4ba3-ac8e-2e771afce6f2.jpg"), true);
} finally {
fs.close();
}
}
public void testUpload() throws Exception {
FSDataOutputStream fsDataOutputStream = fs.create(new Path("/sean2019/hellok.sh"), true);
FileInputStream fileInputStream = new FileInputStream(new File("file:///C:\\Users\\beidouxing\\Pictures\\Camera Roll\\picture\\login_bg3.jpg"));
IOUtils.copy(fileInputStream, fsDataOutputStream);
IOUtils.closeQuietly(fsDataOutputStream);
IOUtils.closeQuietly(fileInputStream);
}
@Test
public void testWrite() throws Exception {
FSDataInputStream fsDataInputStream = fs.open(new Path("/FUTURE/mydir/test"));
FileOutputStream outputStream = new FileOutputStream(new File("D:\\temp\\JD_Phone\\bb.txt"));
IOUtils.copy(fsDataInputStream, outputStream);
IOUtils.closeQuietly(outputStream);
IOUtils.closeQuietly(fsDataInputStream);
}
@Test
public void testRandomAccess() throws Exception {
FSDataInputStream fsDataInputStream = fs.open(new Path("/FUTURE/mydir/test"));
fsDataInputStream.seek(12);
FileOutputStream outputStream = new FileOutputStream(new File("D:\\temp\\JD_Phone\\bb.txt"));
IOUtils.copy(fsDataInputStream, outputStream);
IOUtils.closeQuietly(fsDataInputStream);
IOUtils.closeQuietly(outputStream);
}
@Test
public void testConsole() throws IllegalArgumentException, IOException{
FSDataInputStream inputStream = fs.open(new Path("/FUTURE/mydir/test"));
IOUtils.copy(inputStream, System.out);
IOUtils.closeQuietly(inputStream);
}
}