HDFS简单编程实例:文件合并

下图显示了HDFS文件系统中路径为“localhost:50070/explorer.html#/user/hadoop”的目录中所有的文件信息:

对于该目录下的所有文件,我们将执行以下操作:

首先,从该目录中过滤出所有后缀名不为".abc"的文件。

然后,对过滤之后的文件进行读取。

最后,将这些文件的内容合并到文件“hdfs://localhost:9000/user/hadoop/merge.txt”中。

代码如下:

 1 package mergeFile;
 2
 3 import java.io.IOException;
 4 import java.io.PrintStream;
 5 import java.net.URI;
 6
 7 import org.apache.hadoop.conf.Configuration;
 8 import org.apache.hadoop.fs.FSDataInputStream;
 9 import org.apache.hadoop.fs.FSDataOutputStream;
10 import org.apache.hadoop.fs.FileStatus;
11 import org.apache.hadoop.fs.FileSystem;
12 import org.apache.hadoop.fs.Path;
13 import org.apache.hadoop.fs.PathFilter;
14
15
16 class myPathFilter implements PathFilter{  //过滤掉文件名满足特定条件的文件
17     String reg = null;
18     myPathFilter(String reg){
19         this.reg = reg;
20     }
21     public boolean accept(Path path) {
22         if(!(path.toString().matches(reg)))
23             return true;
24         return false;
25     }
26
27 }
28
29 public class merge {
30     Path inputPath = null;  //待合并的文件所在的目录的路径
31     Path outputPath = null; //输出文件的路径
32     public merge(String input, String output){
33         this.inputPath = new Path(input);
34         this.outputPath = new Path(output);
35     }
36     public void doMerge() throws IOException{
37         Configuration conf = new Configuration();
38         conf.set("fs.defaultFS","hdfs://localhost:9000" );
39         conf.set("fs.hdfs.impl", "org.apache.hadoop.hdfs.DistributedFileSystem");
40
41         FileSystem fsSource = FileSystem.get(URI.create(inputPath.toString()),conf);
42         FileSystem fsDst = FileSystem.get(URI.create(outputPath.toString()),conf);
43
44         FileStatus[] sourceStatus = fsSource.listStatus(inputPath, new myPathFilter(".*\\.abc"));   //过滤掉目录中后缀为.abc的文件
45         FSDataOutputStream fsdos = fsDst.create(outputPath);
46
47         //下面分别读取过滤之后的每个文件的内容,并输出到同一个文件中
48         for(FileStatus sta:sourceStatus){
49             System.out.println("路径: " + sta.getPath() + "   文件大小: " + sta.getLen() + "   权限: " + sta.getPermission() + "  内容: ");
50             FSDataInputStream fsdis = fsSource.open(sta.getPath());
51             byte[] data = new byte[1024];
52             int read = -1;
53             PrintStream ps = new PrintStream(System.out);
54             while((read = fsdis.read(data)) > 0){
55                 ps.write(data, 0, read);
56                 fsdos.write(data, 0, read);
57             }
58         }
59         fsdos.close();
60     }
61     public static void main(String args[]) throws IOException{
62         merge merge = new merge("hdfs://localhost:9000/user/hadoop/", "hdfs://localhost:9000/user/hadoop/merge.txt");
63         merge.doMerge();
64     }
65 }

执行结果:

原文地址:https://www.cnblogs.com/zyb993963526/p/10222130.html

时间: 2024-08-03 06:03:35

HDFS简单编程实例:文件合并的相关文章

HDFS操作及小文件合并

小文件合并是针对文件上传到HDFS之前 这些文件夹里面都是小文件 参考代码 package com.gong.hadoop2; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FSDataInputStream; import or

Linux简单程序实例(GNU工具链,进程,线程,无名管道pipe,基于fd的文件操作,信号,scoket)

一, GNU工具链简介: (1)编译代码步骤: 预处理 -> 编译 -> 汇编 -> 链接: 预处理:去掉注释,进行宏替换,头文件包含等工作: gcc -E test.c -o test.i 编译:   不同平台使用汇编语言不同,汇编将高级语言编译成汇编语言: gcc -S test.c -o test.s 汇编:   将汇编语言翻译成二进制代码: gcc -c test.c -o test.o 链接:   包含各函数库的入口,得到可执行文件: gcc -o test test.c (2

windows下使用fftw进行傅里叶变换及其编程实例

傅里叶变换应该是上大二的时候<信号与系统>课上学过,上研后在<数字信号处理>课上又学了一遍.当初一直在想傅里叶变换到底有什么用呢?什么时候能用上呢?时间如梭,没想到毕业四年后,一个小项目要用到傅里叶变换,喜大普奔啊,当初晦涩的概念.眼晕的公式,终于没白学.是的,其实任何知识都不是白学的,即使工作中一直用不到傅里叶变换,至少思维得到了锻炼,都是有益的.在应用傅里叶变换过程中,可以按照公式自己编程实现,也可以使用已有的开源傅里叶变换函数库.经过查阅资料,本人选用fftw函数库来进行傅里

ffmpeg流文件合并concat

使用ffmpeg的concat可以实现简单的流文件合并功能. 例如: ./ffmpeg -i concat:"out002.ts|out003.ts|out004.ts" -acodec copy -vcodec copy -f mp4 cat.mp4 注意: 输入的各个流需要编码参数一致,否则输出的文件会跟预期不同. 资料引用: Physical concatenation protocol. Allow to read and seek from many resource in

Linux文件编程实例

//捕获fopen调用中的错误 #include <stdio.h> #include <errno.h> #include <string.h> #define MYFILE "missing.txt" int main(  ) { FILE* fin; fin=fopen( MYFILE,"r" ); if( fin==(FILE*)NULL ) { printf( "%s: %s\n",MYFILE,st

HDFS小文件合并问题的优化:copyMerge的改进

1.问题分析 用fsck命令统计 查看HDFS上在某一天日志的大小,分块情况以及平均的块大小,即 [[email protected] jar]$ hadoop fsck /wcc/da/kafka/report/2015-01-11 DEPRECATED: Use of this script to execute hdfs command is deprecated. Instead use the hdfs command for it. 15/01/13 18:57:23 WARN ut

一个简单的Matlab面向对象编程实例

新建Dog.m 内容: classdef Dog properties % these are the variables name; age msg; end methods % these are the functions function obj = Dog() % constructor end function obj = setInfo(obj, name, age) obj.name = name; obj.age = age; end function rst = bark(o

linux编程实例--简单多进程服务器

主要利用fork事先创建若干个进程,并发处理多个客户端的连接,返回当前系统时间.具体代码如下: server.c # include <sys/types.h> # include <sys/socket.h> # include <netinet/in.h> # include <time.h> # include <string.h> # include <stdio.h> # include <signal.h> #

锁定窗口编程实例就是这么简单只需三行中文即可

锁定窗口编程实例就是这么简单只需三行中文即可,看后都会编写. 进入 按钮的程序代码如下图 这就是国内最出名的 中文编程,在学些复杂编程语言之前,可以先学学这个,主要学习编程思路. [编辑推荐视频教程点击下方链接] 中文编程进入学习 原文地址:http://blog.51cto.com/13172026/2287695