public static final HDFS_PATH="hdfs url";
FileSystem fileSystem = null;
Configuration configuration =null;
@Before
public void setUp() throws Exception{
System.out.println("----------setUp---------");
configuration = new Configuration();
/**
*构造一个访问指定HDFS系统的客户端对象
*第一个参数: HDFS的URI
*第二个参数: 客户端指定的配置参数
*第三个参数: 客户端的身份(用户名)
*/
fileSystem = FileSystem.get(new URI(HDFS_PATH),configuration,"root");
}
/**
* 创建HDFS目录
*/
@Test
public void mkdir() throws Exception{
fileSystem.mkdir(new Path("/hdfs/test"));
}
/**
* 查看HDFS文件
*/
@Test
public void readHDFSFile() throws Exception{
FSDataInputStream in = fileSystem.open(new Path("hdfs 文件路径"));
IOUtils.copyBytes(in, System.out, 1024);
}
/**
* 创建HDFS文件 获取副本数 取hdfs-default.xml里的默认值
*/
@Test
public void readHDFSFile() throws Exception{
FSDataOutStream out = fileSystem.create(new Path("hdfs 文件路径"));
out.writeUTF("Hello HDFS");
out.flush();
out.close();//查看文件副本数 是3
System.out.println(configuration.get("dfs.replication"));
configruation.setReplication("1");
out = fileSystem.create(new Path("hdfs 文件路径"));
out.writeUTF("Hello HDFS replication");
out.flush();
out.close(); //查看文件副本数 是1
System.out.println(configuration.get("dfs.replication"));
}
/**
* 重命名HDFS文件
*/
@Test
public void renameHDFSFile() throws Exception{
Path oldPath = new Path("");
Path newPath = new Path("");
boolean result = fileSystem.rename(oldPath,newPath);
System.out.println(result);
}
/**
* 拷贝本地文件到HDFS文件系统
*/
@Test
public void copyFromLocalFile() throws Exception{
Path src = new Path("");
Path dst = new Path("");
boolean result = fileSystem.copyFromLocalFile(src,dst);
System.out.println(result);
}
/**
* 拷贝本地大文件到HDFS文件系统:进度条展示
*/
@Test
public void copyBigFile() throws Exception{
InputStream in = new BufferedInputStream(new FileInputStream(new File("本地大文件")));
FSDataOutputStream out = fileSystem.create(new Path("HDFS 目标文件"),new Processable(){
public void progress(){
System.out.println(".");
}
});
IOUtils.copyBytes(in, out, 4096);
}
/**
* 拷贝HDFS文件系统到本地
*/
@Test
public void copyToLocalFile() throws Exception{
Path src = new Path("");
Path dst = new Path("");
boolean result = fileSystem.copyToLocalFile(src,dst);
System.out.println(result);
}
/**
* 查看目标文件夹的文件
*/
@Test
public void getFileStatus() throws Exception{
FileStatus[] statues = fileSystem.listStatus(new Path("HDFS 文件夹路径"));
for(FileStatus file : statues){
String isDir = file.isDirectory? "文件夹":"文件";
String permission = file.getPermission().toString();
short replication = file.getReplication();
long length = file.getLen();
String path = file.getPath().toString();
System.out.println(isDir +"\t" + permission
+"\t"+replication + "\t"+length
+"\t"+path);
}
}
/**
* 列出HDFS文件夹下所有文件, true 递归读取
*/
@Test
public void listFiles() throws Exception{
RemoteIterator<LocatedFileStatus> files = fileSystem.listFiles(new Path("HDFS 文件夹路径"),true);
while(files.hasNext()){
LocatedFileStatus file = files.next();
String isDir = file.isDirectory? "文件夹":"文件";
String permission = file.getPermission().toString();
short replication = file.getReplication();
long length = file.getLen();
String path = file.getPath().toString();
System.out.println(isDir +"\t" + permission
+"\t"+replication + "\t"+length
+"\t"+path);
}
}
/**
* 查看HDFS文件的block信息
*/
@Test
public void getFileBlockLocations() throws Exception{
FileStatus[] fileStatus = fileSystem.listStatus(new Path("HDFS 文件夹路径"));
BlockLocation[] blocks = fileSystem.getFileBlockLocations(fileStatus, 0, fileStatus.getLen() )
for(BlockLocation block : blocks){
for(String name :block.getNames()){
System.out.println(name + " : "+ block.getOffset()+" : "+ block.getLength());
}
}
}
/**
* 删除HDFS文件
*/
@Test
public void getFileBlockLocations() throws Exception{
boolean result = fileSystem.delete(new Path("HDFS 文件夹路径"),true);
System.out.println(result);
}
@After
public void tearDown(){
FileSystem fileSystem = null;
Configuration configuration =null;
System.out.println("----------tearDown---------");
}