引入依赖
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-yarn-common</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-yarn-client</artifactId>
<version>3.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-yarn-server-resourcemanager</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-mapreduce-client-core</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-mapreduce-client-jobclient</artifactId>
<version>3.1.2</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-mapreduce-client-common</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>org.anarres.lzo</groupId>
<artifactId>lzo-hadoop</artifactId>
<version>1.0.6</version>
</dependency>
测试方法
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.io.IOUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.*;
import java.net.URI;
public class HDFSTEST {
private final String HDFS_PATH = "hdfs://192.168.233.150:9000";
private FileSystem fs = null;
private Configuration cfg = null;
@Before
public void setUp() throws Exception {
cfg = new Configuration();
fs = FileSystem.get(new URI(HDFS_PATH), cfg);
System.out.println("HDFS 开启");
}
@Test
public void frameTest() throws IOException {
System.out.println(cfg);
System.out.println(fs);
}
@Test
public void uplodFile() throws IOException {
fs.copyFromLocalFile(new Path("F:/hadoop/hdfs/down/t1.txt"),new Path("/input/"));
System.out.println("本地文件已经上传文件到HDFS");
}
@Test
public void mkdir() throws IOException {
fs.mkdirs(new Path("/user/tt"));
System.out.println("成功创建HDFS 文件目录");
}
@Test
public void deleteDir() throws IllegalArgumentException, IOException {
fs.delete(new Path("/output"), true);
System.out.println("文件夹已删除");
}
@Test
public void downFile() throws Exception {
fs.copyToLocalFile(false, new Path("/output/part-r-00000"), new Path("F:/down/t1.txt"));
System.out.println("文件已经下载成功!");
}
@Test
public void listFiles() throws IOException {
System.out.println("查看文件详情:");
RemoteIterator<LocatedFileStatus> iterator = fs.listFiles(new Path("/myhah.txt"), true);
while (iterator.hasNext()){
LocatedFileStatus fileStatus = iterator.next();
System.out.println(fileStatus.getPath().getName());
System.out.println(fileStatus.getPermission());
System.out.println(fileStatus.getLen());
BlockLocation[] blockLocations = fileStatus.getBlockLocations();
for (BlockLocation blockLocation : blockLocations) {
String[] hosts = blockLocation.getHosts();
for (String host : hosts) {
System.out.println(host);
}
}
System.out.println("-----------------------------");
}
}
@Test
public void fileStatus() throws IOException {
FileStatus[] fileStatuses = fs.listStatus(new Path("/"));
for (FileStatus fileStatus : fileStatuses) {
if(fileStatus.isDirectory()){
System.out.println(fileStatus.getPath().getName()+" is folder");
}else if(fileStatus.isFile()){
System.out.println(fileStatus.getPath().getName()+" is file");
}
}
}
@Test
public void exists() throws IOException {
boolean exists = fs.exists(new Path("/12342.xml"));
if(exists){
System.out.println("文件已经存在");
}else {
System.out.println("文件不存在");
}
}
@Test
public void createNewFile() throws IOException {
boolean newFile = fs.createNewFile(new Path("/aa.txt"));
System.out.println("创建文件:"+newFile);
}
@Test
public void deleteFile() throws IOException {
boolean res = fs.delete(new Path("/aa.txt"),true);
System.out.println("删除文件:"+res);
}
@Test
public void rename() throws IOException {
Path src = new Path("/aa.txt");
Path aim = new Path("/aim.xml");
fs.rename(src, aim);
System.out.println("文件重命名完成");
}
@Test
public void writ() throws IOException {
FSDataOutputStream fos = fs.create(new Path("/aim.xml"));
fos.writeUTF("test");
fos.flush();
fos.close();
System.out.println("添加内容完成");
}
@Test
public void fileToIO() throws IllegalArgumentException, IOException {
FSDataInputStream inputStream = fs.open(new Path("/aim.xml"));
IOUtils.copyBytes(inputStream, System.out, 4096, false);
}
@Test
public void getStringByLine() throws IOException {
FSDataInputStream dis = fs.open(new Path("/aim.xml"));
BufferedReader br = new BufferedReader(new InputStreamReader(dis));
FileWriter fileWriter = new FileWriter(new File("F:/down/ss.xml"));
char[] temp = new char[16];
for (int i =0;i<16;i++){
br.read(temp);
fileWriter.write(new String(temp));
}
br.close();
fileWriter.close();
System.out.println("定位读取文件完成");
}
@Test
public void getStringByIOUtils() throws IOException {
FSDataInputStream dis = fs.open(new Path("/aim.xml"));
dis.seek(256);
BufferedReader br = new BufferedReader(new InputStreamReader(dis));
FileOutputStream fos = new FileOutputStream(new File("F:/down/ss2.xml"));
IOUtils.copyBytes(dis,fos,cfg);
IOUtils.closeStream(dis);
IOUtils.closeStream(fos);
}
@After
public void tearDown() throws Exception {
fs.close();
System.out.println("HDFS 关闭");
}
}