通过使用API来操作HDFS

来源:互联网 发布:淘宝主图视频怎么制作 编辑:程序博客网 时间:2024/06/05 22:55

整体的流程就是:

public static final String HDFS_PATH = "hdfs://masterIP:9000/";

Configuration config=new Configuration();

FileSystem fs=FileSystem.get(new URI(HDFS_PATH ),config); //FIleSystem类是操作文件的主要的类

1、创建一个文件夹,同时并将一个本地的文件输入到这个新创建的文件夹下

//在hdfs下创建/test目录

fs.mkdir(new Path("/test"));

//将本地的文件上传到/test文件夹下,并且命名为yy.txt

FSDataOutputStream out=fs.create(new Path("/test/yy.txt"));

//介绍create几个方法的重载

FSDataOutputStream create(Path path)

FSDataOutputStream create(Path path,boolean overwrite)

FSDataOutputStream create(Path path,boolean overwrite ,int buffersize)

//得到本地文件的输入流

FileInputStream in = new FileInputStream("C:/Users/Administrator/Desktop/yy.txt");

//使用IOUtils这个工具,直接将输入流传给输出流

/**
* @param in
*            输入流
* @param out
*            输出流
* @param buffersize
*            输出缓冲区
* @param close
*            是否关闭
*/

IOUtils.copyBytes(in, out, 1024, true); 

2、将hdfs文件的数据,复制到本地指定的文件 中

FSDataInputStream in=fs.open(new Path("/test/hello"));

FileoutputStream out=new FIleOutputStream(new File("/usr/local/yy/hello.txt"));

IOUtils.copyBytes(in,out,1024,true);

3、删除指定的文件或者文件夹下所有的文件

boolean delete(Path f,boolean recursive) API

如果f是一个目录,且recursive=true 则删除该文件夹下所有的文件。

如果f就是一个文件 ,则无论recursive=true  or  false 则只删除该文件

boolean result=fs.delete(new Path("/test"),true);

4、获取指定文件的信息

FileStatus getFileStatus(Path path)

FileStatus stat=fs.getFileStatus("/test/hello");

System.out.println(stat.getPath().toUri().getPath());// 相对路径
System.out.println(stat.getPath());// 绝对路径
System.out.println(stat.getLen());
System.out.println(stat.isDir());
System.out.println(stat.getModificationTime());
System.out.println(stat.getReplication());
System.out.println(stat.getBlockSize());
System.out.println(stat.getOwner());
System.out.println(stat.getGroup());
System.out.println(stat.getPermission().toString());

5、获取文件夹下所有文件的信息(使用递归)

FileStatus[] listStatus(Path f)

public void showFileStatus(Path p){

FileStatus[] status=fs.listStatus(new Path("/test"));

for(int i=0;i<status.length;i++){

    if(status[i].isDir()){

       showFileStatus(status[i].getPath());

  }else{

    System.out.println(stat.getPath());// 绝对路径
System.out.println(status[i].getLen());
System.out.println(status[i].isDir());
System.out.println(status[i].getModificationTime());
System.out.println(status[i].getReplication());
System.out.println(status[i].getBlockSize());
System.out.println(status[i].getOwner());
System.out.println(status[i].getGroup());
System.out.println(status[i].getPermission().toString());

 }

}

}

//这个是重载的方法,主要就是通过PathFilter过滤到一些不想要的文件或者路径

FileStatus[] listStatus(Path f,PathFilter pathFilter)

package hdfs;
import org.apache.hadoop.fs.Path;
public class MyPathFilter implements PathFilter {
private final String regex;
public MyPathFilter(String regex) {
this.regex = regex;
}
@Override
public boolean accept(Path path) {
return path.toString().matches(regex);
}
}

FileStatus[] fstatus=fs.listStatus(new Path("/test"),new MyPathFilter("f*")); //主要f开头的文件


FileStatus[] globalStatus(Path pattern) ;//输入的路径可以是正则表达式,将多个路径的文件显示出来

FileStatus[]      fss=fs.globalStatus("/test*");

Path[] paths=FileUtils.stat2Path(fss); //这是一个工具类,将FileStatus转化为路径

for(Path p:paths){

System.out.println(p);

}/span>

6、得到所有的DataNode节点的具体信息

private static void getHDFSNodes() throws IOException, URISyntaxException {
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(new URI(HADOOP_PATH), conf);
DistributedFileSystem dfs = (DistributedFileSystem) fs;
DatanodeInfo[] datanodestats = dfs.getDataNodeStats();
for (int i = 0; i < datanodestats.length; i++) {
System.out.println("datanode_" + i + "--"
+ datanodestats[i].getHost() + "--"
+ datanodestats[i].getHostName() + "--"
+ datanodestats[i].getDatanodeReport() + "--"
+ datanodestats[i].getAdminState());
}
}

7、得到文件的块在哪些主机上

private static void getFileLocal() throws IOException, URISyntaxException {
FileSystem fs = FileSystem.get(new URI(HADOOP_PATH),
new Configuration());
Path fpath = new Path("/in/hello");
FileStatus filestatus = fs.getFileStatus(fpath);
System.out.println(filestatus.getReplication() + "--"
+ filestatus.getOwner() + "--" + filestatus.getGroup() + "--"
+ filestatus.getBlockSize() + "--" + filestatus.getLen() + "--"
+ filestatus.getPath());
BlockLocation[] blklocation = fs.getFileBlockLocations(filestatus, 0,
filestatus.getLen());
int blocklen = blklocation.length;
for (int i = 0; i < blocklen; i++) {
String[] hosts = blklocation[i].getHosts();
System.out.println("block_" + i + "--" + "-location:" + hosts[0]);
}
}



0 0
原创粉丝点击