HDFS常用API
package com.ithhs;
import junit.framework.TestCase;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import java.io.*;
import java.util.Arrays;
public class HDFSJavaApiDemoTest {
FileSystem fs = null;
@Before
public void init() throws IOException {
System.setProperty("HADOOP_USER_NAME", "root");
Configuration configuration = new Configuration();
configuration.set("fs.defaultFS", "hdfs://mini1:9000");
fs = FileSystem.get(configuration);
}
@Test
public void testMkDirs() throws IOException {
Path path = new Path("/test2");
boolean isSuccess = fs.mkdirs(path);
Assert.assertEquals(true, isSuccess);
}
@Test
public void testDeleteDir() throws IOException {
boolean isDelete = fs.delete(new Path("/test2"), true);
Assert.assertEquals(true, isDelete);
}
@Test
public void testCopyFromLocal() throws IOException {
fs.copyFromLocalFile(new Path("e:/user.txt"), new Path("/user.txt"));
}
@Test
public void testCopyToLocal() throws IOException {
fs.copyToLocalFile(new Path("/user.txt"), new Path("e:/u.txt"));
}
@Test
public void testListFiles() throws IOException {
RemoteIterator<LocatedFileStatus> remoteIterator = fs.listFiles(new Path("/"), true);
while (remoteIterator.hasNext()) {
LocatedFileStatus fileStatus = remoteIterator.next();
System.out.println("文件路径:" + fileStatus.getPath());
System.out.println("文件名称:" + fileStatus.getPath().getName());
System.out.println("文件的长度:" + fileStatus.getLen());
System.out.println("文件块大小:" + fileStatus.getBlockSize());
System.out.println("文件副本:" + fileStatus.getReplication());
System.out.println("文件块信息:" + Arrays.toString(fileStatus.getBlockLocations()));
System.out.println("=====================================");
}
}
@Test
public void testListStatus() throws IOException {
FileStatus[] fileStatuses = fs.listStatus(new Path("/"));
for (FileStatus fileStatus : fileStatuses) {
if (fileStatus.isDirectory()) {
System.out.println("是一个目录");
} else {
System.out.println("是一个文件");
}
System.out.println("文件路径:" + fileStatus.getPath());
System.out.println("文件名称:" + fileStatus.getPath().getName());
System.out.println("文件的长度:" + fileStatus.getLen());
System.out.println("文件块大小:" + fileStatus.getBlockSize());
System.out.println("文件副本:" + fileStatus.getReplication());
System.out.println("=====================================");
}
}
@Test
public void testCreate() throws IOException {
FSDataOutputStream fsDataOutputStream = fs.create(new
Path("/out.txt"), true);
for (int i = 0; i < 10; i++) {
fsDataOutputStream.write("hello world".getBytes());
fsDataOutputStream.write("\r\n".getBytes());
}
fsDataOutputStream.close();
}
@Test
public void testRead() throws IOException {
FSDataInputStream fsDataInputStream = fs.open(new
Path("/out2.txt"));
int readNum = 0;
byte[] bytes = new byte[1024];
while ((readNum = fsDataInputStream.read(bytes)) != -1) {
System.out.println(new String(bytes, 0, readNum));
}
fsDataInputStream.close();
}
@After
public void destroy() throws IOException {
fs.close();
}
}