Windows下不配环境运行可能会报错,Linux下肯定能运行。
package cn.itcast.centosdvd.hdfs;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.commons.io.IOUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.LocatedFileStatus;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.RemoteIterator;
import org.junit.Before;
import org.junit.Test;
public class HdfsUtil {
FileSystem fs=null;
@Before
public void init() throws IOException, Exception, URISyntaxException {
//读取classpath下的xxx-site.xml配置文件,并解析内容封装到conf对象
Configuration conf = new Configuration();
//也可在代码中对conf中的配置修改,进行手动设置,会覆盖配置文件中的值
conf.set("fs.defaultFS", "hdfs://weekend110:9000/");
//根据配置信息,获取一个具体的文件系统的客户端操作实例对象
fs= FileSystem.get(new URI("hdfs://weekend110:9000/"),conf,"centosdvd");
}
/**
* 上传文件 ,较底层的写法
* @throws Exception
*/
@Test
public void upload() throws Exception {
// TODO Auto-generated method stub
Path dst = new Path("hdfs://weekend110:9000/aa/666.txt");
FSDataOutputStream os = fs.create(dst);
FileInputStream is = new FileInputStream("d:/666.txt");
IOUtils.copy(is,os);
}
/**
* 封装好的写法
* @throws Exception
* @throws IOException
*/
@Test
public void upload2() throws Exception, IOException {
fs.copyFromLocalFile(new Path("d:/666.txt"),
new Path("hdfs://weekend110:9000/aa/6662.txt"));
fs.copyFromLocalFile(new Path("d:/666.txt"),
new Path("hdfs://weekend110:9000/aaa/bbb/ccc/6662.txt"));
}
/**
* 下载文件
* @throws Exception
* @throws IllegalArgumentException
*/
@Test
public void download() throws IllegalArgumentException, Exception {
// TODO Auto-generated method stub
// fs.copyToLocalFile(false,new Path("hdfs://weekend110:9000/aa/6662.txt"),
// new Path("d:/6662.txt"),true);
}
/**
* 查看文件信息
* @throws Exception
* @throws IllegalArgumentException
* @throws FileNotFoundException
*/
@Test
public void listFlies() throws FileNotFoundException, IllegalArgumentException, Exception{
// TODO Auto-generated method stub
// listFiles列出的是文件信息,而且提供递归遍历
RemoteIterator<LocatedFileStatus> files = fs.listFiles(new Path("/"), true);
while(files.hasNext()) {
LocatedFileStatus file = files.next();
Path filePath = file.getPath();
String filename = filePath.getName();
System.out.println(filename);
}
System.out.println("--------------------------------------");
//listStatus可以列出文件和文件夹的信息,不提供递归遍历
FileStatus[] listStatus = fs.listStatus(new Path("/"));
for(FileStatus status:listStatus) {
String name = status.getPath().getName();
System.out.println(name+(status.isDirectory()?" is Dir":" is file"));
}
}
/**
* 创建目录(文件夹)
* @throws Exception
* @throws IllegalArgumentException
*/
@Test
public void mkdir() throws IllegalArgumentException, Exception {
// TODO Auto-generated method stub
fs.mkdirs(new Path("/aaa/bbb/ccc"));
}
/**
* 删除文件或文件夹
* @throws Exception
* @throws IllegalArgumentException
*/
@Test
public void rm() throws IllegalArgumentException, Exception {
// TODO Auto-generated method stub
fs.delete(new Path("/aa"), true);
}
}