-通过hdfs putMerge命令合并上传的文件
通过命令 bin/hdfs dfs 查看dfs的一些操作命令可以找到-getmerge命令
[hao@bigdata00 hadoop-2.5.0]$ bin/hdfs dfs
Usage: hadoop fs [generic options]
[-appendToFile <localsrc> ... <dst>]
[-cat [-ignoreCrc] <src> ...]
[-checksum <src> ...]
[-chgrp [-R] GROUP PATH...]
[-chmod [-R] <MODE[,MODE]... | OCTALMODE> PATH...]
[-chown [-R] [OWNER][:[GROUP]] PATH...]
[-copyFromLocal [-f] [-p] <localsrc> ... <dst>]
[-copyToLocal [-p] [-ignoreCrc] [-crc] <src> ... <localdst>]
[-count [-q] <path> ...]
[-cp [-f] [-p | -p[topax]] <src> ... <dst>]
[-createSnapshot <snapshotDir> [<snapshotName>]]
[-deleteSnapshot <snapshotDir> <snapshotName>]
[-df [-h] [<path> ...]]
[-du [-s] [-h] <path> ...]
[-expunge]
[-get [-p] [-ignoreCrc] [-crc] <src> ... <localdst>]
[-getfacl [-R] <path>]
[-getfattr [-R] {-n name | -d} [-e en] <path>]
[-getmerge [-nl] <src> <localdst>]
[-help [cmd ...]]
[-ls [-d] [-h] [-R] [<path> ...]]
[-mkdir [-p] <path> ...]
[-moveFromLocal <localsrc> ... <dst>]
[-moveToLocal <src> <localdst>]
[-mv <src> ... <dst>]
[-put [-f] [-p] <localsrc> ... <dst>]
[-renameSnapshot <snapshotDir> <oldName> <newName>]
[-rm [-f] [-r|-R] [-skipTrash] <src> ...]
[-rmdir [--ignore-fail-on-non-empty] <dir> ...]
[-setfacl [-R] [{-b|-k} {-m|-x <acl_spec>} <path>]|[--set <acl_spec> <path>]]
[-setfattr {-n name [-v value] | -x name} <path>]
[-setrep [-R] [-w] <rep> <path> ...]
[-stat [format] <path> ...]
[-tail [-f] <file>]
[-test -[defsz] <path>]
[-text [-ignoreCrc] <src> ...]
[-touchz <path> ...]
[-usage [cmd ...]]
合并本机上传的三个文件
通过以上操作查看[-getmerge [-nl] ]可知,命令后前面是hdfs需要合并风文件路径,后面是合并后本机地址文件
[hao@bigdata00 hadoop-2.5.0]$ bin/hdfs dfs -getmerge /data/HebingData/core.xml /data/HebingData/hdfs.xml /data/HebingData/yarn.xml /opt/datafile/hebing/2.xml
![]()
操作后可以看到本机目录下有了 2.xml这个合并后的文件
[hao@bigdata00 hadoop-2.5.0]$ ll /opt/datafile
total 12
-rw-r--r-- 1 hao hao 1034 Feb 14 00:26 core.xml
drwxrwxr-x 2 hao hao 4096 Feb 14 02:19 hebing
-rw-rw-r-- 1 hao hao 128 Feb 10 05:50 wc.input
[hao@bigdata00 hadoop-2.5.0]$ ll /opt/datafile/hebing/
total 4
-rw-r--r-- 1 hao hao 2714 Feb 14 02:19 2.xml
下面通过API实现文件合并后上传
步骤:
1.获取文件系统
2.列举合并文件
3.打开文件,打开输入流
4.打开输出流
5.关闭流
public static void main(String[] args) throws IOException {
// step 1: 获取文件系统 get FileSystem
Configuration conf = new Configuration();
LocalFileSystem localfs =FileSystem.getLocal(conf);
FileSystem dfs = FileSystem.get(conf);
// step 2:获取输入及输出文件地址 in/put path
Path inpath = new Path("/opt/modules/hadoop-2.5.0/etc/hadoop");
Path outpath =new Path("/data/HebingData/hebing.xml");
// step 3:打开数据流 open Stream
FSDataOutputStream outputStream =null;
// step 4:列举要合并的文件,并输出
FileStatus[] fileStatuse =null;
try{
//输出流获取输出地址
outputStream = dfs.create(outpath);
//输入流获取要合并的文件地址
fileStatuse =localfs.listStatus(inpath);
// 通过for循环获取要合并的文件
for(FileStatus FileName:fileStatuse){
FSDataInputStream inputStream = localfs.open(FileName.getPath());
IOUtils.copyBytes(inputStream,outputStream, 4096,false);
//关闭输入流
IOUtils.closeStream(inputStream);
//在控制台输出所有合并文件的文件名
System.out.println(FileName.getPath().getName());
}
}catch(Exception e){
e.printStackTrace();
}finally{
// step:5关闭输出流
IOUtils.closeStream(outputStream);
//合并完成后在控制台输出
System.out.print("success PutMerge");
}
通过控制台查看合并的所有文件
然后通过50070端口在页面查看合并后的文件