当前位置: 移动技术网 > IT编程>开发语言>Java > HDFS的Java API的访问方式实例代码

HDFS的Java API的访问方式实例代码

2019年07月19日  | 移动技术网IT编程  | 我要评论

本文研究的主要是hdfs的java api的访问方式,具体代码如下所示,有详细注释。

最近的节奏有点儿快,等有空的时候把这个封装一下

实现代码

要导入的包:

import java.io.ioexception;
import java.net.uri;
import java.net.urisyntaxexception;

import org.apache.hadoop.conf.configuration;
import org.apache.hadoop.fs.blocklocation;
import org.apache.hadoop.fs.filestatus;
import org.apache.hadoop.fs.filesystem;
import org.apache.hadoop.fs.fileutil;
import org.apache.hadoop.fs.path;
import org.apache.hadoop.hdfs.distributedfilesystem;
import org.apache.hadoop.hdfs.protocol.datanodeinfo;

实体方法:

/**
   * 获取hdfs文件系统
   * @return
   * @throws ioexception 
   * @throws urisyntaxexception 
   */
public static filesystem getfilesystem() throws ioexception, urisyntaxexception{
	//read config file
	configuration conf = new configuration();
	//返回默认文件系统
	//如果在hadoop集群下运行,使用此种方法可以直接获取默认文件系统
	//filesystem fs = filesystem.get(conf);
	//指定的文件系统地址
	uri uri = new uri("hdfs://hy:9000");
	//返回指定的文件系统
	//如果在本地测试,需要使用此种方法获取文件系统
	filesystem fs = filesystem.get(uri, conf);
	return fs;
}
/**
   * 创建文件目录
   * @throws exception
   */
public static void mkdir() throws exception{
	//获取文件系统
	filesystem fs = getfilesystem();
	//创建文件目录
	fs.mkdirs(new path("hdfs://hy:9000/hy/weibo"));
	//释放资源
	fs.close();
}
/**
   * 删除文件或者文件目录
   * @throws exception
   */
public static void rmdir() throws exception{
	//获取文件系统
	filesystem fs = getfilesystem();
	//删除文件或者文件目录
	fs.delete(new path("hdfs://hy:9000/hy/weibo"), true);
	//释放资源
	fs.close();
}
/**
   * 获取目录下所有文件
   * @throws exception
   */
public static void listallfile() throws exception{
	//获取文件系统
	filesystem fs = getfilesystem();
	//列出目录内容
	filestatus[] status = fs.liststatus(new path("hdfs://hy:9000/hy/"));
	//获取目录下所有文件路径
	path[] listedpaths = fileutil.stat2paths(status);
	//循环读取每个文件
	for (path path : listedpaths) {
		system.out.println(path);
	}
	//释放资源
	fs.close();
}
/**
   * 将文件上传至hdfs
   * @throws exception
   */
public static void copytohdfs() throws exception{
	//获取文件对象
	filesystem fs = getfilesystem();
	//源文件路径是linux下的路径 path srcpath = new path("/home/hadoop/temp.jar");
	//如果需要在windows下测试,需要改为windows下的路径,比如 e://temp.jar
	path srcpath = new path("e://temp.jar");
	//目的路径
	path dstpath = new path("hdfs://hy:9000/hy/weibo");
	//实现文件上传
	fs.copyfromlocalfile(srcpath, dstpath);
	//释放资源
	fs.close();
}
/**
   * 从hdfs上下载文件
   * @throws exception
   */
public static void getfile() throws exception{
	//获得文件系统
	filesystem fs = getfilesystem();
	//源文件路径
	path srcpath = new path("hdfs://hy:9000/hy/weibo/temp.jar");
	//目的路径,默认是linux下的
	//如果在windows下测试,需要改为windows下的路径,如c://user/andy/desktop/
	path dstpath = new path("d://");
	//下载hdfs上的文件
	fs.copytolocalfile(srcpath, dstpath);
	//释放资源
	fs.close();
}
/**
   * 获取hdfs集群点的信息
   * @throws exception
   */
public static void gethdfsnodes() throws exception{
	//获取文件系统
	filesystem fs = getfilesystem();
	//获取分布式文件系统
	distributedfilesystem hdfs = (distributedfilesystem)fs;
	//获取所有节点
	datanodeinfo[] datanodestats = hdfs.getdatanodestats();
	//循环比遍历
	for (int i = 0; i < datanodestats.length; i++) {
		system.out.println("datanote_" + i + "_name:" + datanodestats[i].gethostname());
	}
	//释放资源
	fs.close();
}
/**
   * 查找某个文件在hdfs集群的位置
   * @throws exception
   */
public static void getfilelocal() throws exception{
	//获取文件系统
	filesystem fs = getfilesystem();
	//文件路径
	path path = new path("hdfs://hy:9000/hy/weibo/temp.jar");
	//获取文件目录
	filestatus filestatus = fs.getfilestatus(path);
	//获取文件块位置列表
	blocklocation[] blocklocations = fs.getfileblocklocations(filestatus, 0, filestatus.getlen());
	//循环输出块信息
	for (int i = 0; i < blocklocations.length; i++) {
		string[] hosts = blocklocations[i].gethosts();
		system.out.println("block_" + i + "_location:" + hosts[0]);
	}
	//释放资源
	fs.close();
}

总结

以上就是本文关于hdfs的java api的访问方式实例代码的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网