JAVA操作Hadoop
生活随笔
收集整理的这篇文章主要介绍了
JAVA操作Hadoop
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
写在前面:
我的博客已迁移至自建服务器:博客传送门,CSDN博客暂时停止,如有机器学习方面的兴趣,欢迎来看一看。
此外目前我在gitHub上准备一些李航的《统计学习方法》的实现算法,目标将书内算法全部手打实现,欢迎参观并打星。GitHib传送门
一、客户端环境准备
1.jar 包及Eclipse 准备
该部分可网上查阅相关资料
2.相关Demo
1.获取文件系统
//1.获取文件系统 @Test public void initHDFS() throws IOException {// 1获取文件系统Configuration configuration = new Configuration();FileSystem fs = FileSystem.get(configuration);//2.打印文件系统到控制台System.out.println(fs.toString());fs.close(); }1.首先需要获取到Hadoop的文件系统,以便对Hadoop进行操作。
2.打印获取到的文件系统
3.控制台有信息输出,说明获取成功
2.文件上传
//2.文件上传 @Test public void CopyFromLocalFile() throws IOException, InterruptedException, URISyntaxException {//获取文件系统Configuration configuration = new Configuration();FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "atguigu");//上传fs.copyFromLocalFile(false, new Path("e:/hello.txt"), new Path("/hello1.txt"));//释放资源fs.close();}3.文件下载
//3.文件下载 @Test public void CopyToLoaclFile() throws IOException, InterruptedException, URISyntaxException{//获取文件系统Configuration configuration = new Configuration();FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "atguigu");//下载文件fs.copyToLocalFile(false, new Path("/hello1.txt"), new Path("e:\\hello1.txt"), true);fs.close(); }4.创建目录
//4.创建目录 @Test public void Mkdirs() throws IOException, InterruptedException, URISyntaxException {Configuration configuration = new Configuration();FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "atguigu");//创建目录fs.mkdirs(new Path("/test/haha/heihei"));fs.close(); }5.删除文件夹
//5.删除文件夹 @Test public void Delete() throws IOException, InterruptedException, URISyntaxException {//获取文件系统Configuration configuration = new Configuration();FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "atguigu");//删除文件夹fs.delete(new Path("/test"), true);//释放资源fs.close();}6.更改文件名
//6.更改文件名 @Test public void ReName() throws IOException, InterruptedException, URISyntaxException {//获取文件系统Configuration configuration = new Configuration();FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "atguigu");//更改文件名fs.rename(new Path("/hello.txt"), new Path("/hello2.txt"));//释放资源fs.close(); }7.查看文件详情
//7.查看文件详情 @Test public void ListFiles() throws IOException, InterruptedException, URISyntaxException {//获取文件系统Configuration configuration = new Configuration();FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "atguigu");RemoteIterator<LocatedFileStatus> remoteIterator = fs.listFiles(new Path("/"), true);while(remoteIterator.hasNext()) {LocatedFileStatus status = remoteIterator.next();//文件名System.out.println(status.getPath().getName());//文件长度System.out.println(status.getLen());//组System.out.println(status.getGroup());//副本数System.out.println(status.getReplication());//获取块位置信息BlockLocation[] blockLocations = status.getBlockLocations();for(BlockLocation bl: blockLocations) {//偏移量System.out.println(bl.getOffset());//副本所在主机System.out.println(bl.getHosts());System.out.println(bl.getNames());}System.out.println("-------------------------------------------");}fs.close(); }8.判断是否文件
//8.判断是否文件 @Test public void IsFile() throws IOException, InterruptedException, URISyntaxException {//获取文件系统Configuration configuration = new Configuration();FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "atguigu");FileStatus[] listStatus = fs.listStatus(new Path("/"));for(FileStatus status : listStatus) {if(status.isFile()) {System.out.println("f:" + status.getPath().getName());}else {System.out.println("d:" + status.getPath().getName());}}fs.close(); } } 《新程序员》:云原生和全面数字化实践50位技术专家共同创作,文字、视频、音频交互阅读总结
以上是生活随笔为你收集整理的JAVA操作Hadoop的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: Hadoop伪分布式运行案例
- 下一篇: IO流操作HDFS