pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.hadoop.experiment</groupId>
<artifactId>firsthadoop</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<hadoop.version>3.0.2</hadoop.version>
</properties>
<repositories>
<repository>
<id>apache</id>
<url>https://mvnrepository.com/artifact</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>${hadoop.version}</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>${hadoop.version}</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>${hadoop.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>
</dependencies>
</project>
hadoopUtils类
package com.hadoop;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.io.IOUtils;
import java.io.FileInputStream;
import java.net.URI;
public class HadoopUtils {
private FileSystemfileSystem =null;
private Configurationconf =null;
public void setConf()throws Exception{
conf =new Configuration();
conf.set("fs.defaultFS","hdfs://192.168.0.10:9000/");
fileSystem = FileSystem.get(new URI("hdfs://192.168.0.10:9000/"),conf,
"root");
}
public void upLoad()throws Exception{
setConf();
Path path =new Path("/test");
FSDataOutputStream fo =fileSystem.create(path);
FileInputStream fi =new FileInputStream("c:/Users/Administrator/Desktop/centos.txt");
IOUtils.copyBytes(fi, fo,conf);
}
//上传文件
public void upLoad2()throws Exception{
setConf();
fileSystem.copyFromLocalFile(new Path("c:/Users/Administrator/Desktop/centos.txt"),
new Path("hdfs://192.168.0.10:9000/cc"));
}
//下载文件
public void downLoad()throws Exception{
setConf();
fileSystem.copyToLocalFile(new Path(""),new Path(""));
}
//列出文件
public void listFiles()throws Exception{
setConf();
/**
RemoteIterator<LocatedFileStatus>fileList = fileSystem.listFiles(new Path("/"), true);
while (fileList.hasNext()){
LocatedFileStatus files = fileList.next();
Path path = files.getPath();
String fileName = path.getName();
System.out.println(fileName);
}
*
*/
System.out.println("====================");
FileStatus [] fileList =fileSystem.listStatus(new Path("/"));
for (FileStatus fileStatus: fileList){
String fileName = fileStatus.getPath().getName();
System.out.println(fileName);
}
}
//创建目录
public void makeDir()throws Exception{
setConf();
fileSystem.mkdirs(new Path("/aaa/bbb"));
}
//删除文件
public void rm()throws Exception{
setConf();
fileSystem.delete(new Path("/cc"),true);
}
}
test
import com.hadoop.HadoopUtils;
import org.junit.Test;
public class HadoopTest {
@Test
public void test1()throws Exception{
HadoopUtils hadoopUtils =new HadoopUtils();
hadoopUtils.listFiles();
}
}