总结了一下三个方法:hdfs自带 按字节复制 按行复制 (在java io里还有字符复制,暂且不提)

因为hdfs自带的,不知道为什么有些场合不能用,每次能下载的个数还不一定,所以就考虑自己按照java的方式来复制,就出现第2、3种方法。

有时间好好研究一下IO,比如针对特殊文件,文件复制会出现大小不一样的情况。这里

	// void downloadFromHdfs(String hdfsSrc , String localDst)
// String hdfsDst = "hdfs://54.0.88.53:8020/user/flume/SyslogNetwork/";
// String localDir = "D://flume//";
//下载单个文件
public static boolean downloadFromHdfs(String hdfsSrc, String localDst) {
Configuration conf = new Configuration();
Path dst = new Path(hdfsSrc);
try {
Path Src = new Path(hdfsSrc);
String Filename = Src.getName().toString();
String local = localDst + Filename;
Path Dst = new Path(local);
FileSystem fs = FileSystem.get(URI.create(hdfsSrc), conf);
FSDataInputStream in = fs.open(Src);
OutputStream output = new FileOutputStream(new File(local));
IOUtils.copyBytes(in, output, 4096, true);
System.out.print(" download successed.");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.print(" download failed.");
return false;
}
return true; }
//下载目录下所有文件,方法1: IOUtils.copyBytes或者copyToLocal
public static boolean downFromHdfsDir(String hdfsSrc, String localDst)
throws IOException {
Configuration conf = new Configuration();
Path dstpath = new Path(hdfsSrc);
int i = 1;
FileSystem fs = FileSystem.get(URI.create(hdfsSrc), conf);
try {
String subPath = "";
FileStatus[] fList = fs.listStatus(dstpath);
for (FileStatus f : fList) {
if (null != f) {
subPath = new StringBuffer()
.append(f.getPath().getParent()).append("/")
.append(f.getPath().getName()).toString();
if (f.isDir()) {
downFromHdfsDir(subPath, localDst);
} else {
System.out.println("/t/t" + subPath);// hdfs://54.0.88.53:8020/
Path dst = new Path(subPath);
i++;
FSDataInputStream in = null;
OutputStream output = null;
try {
Path Src = new Path(subPath);
String Filename = Src.getName().toString();
String local = localDst + Filename;
Path Dst = new Path(local);
FileSystem hdfs = FileSystem.get(URI
.create(subPath), conf);
in = hdfs.open(Src);
output = new FileOutputStream(new File(local));
// true-是否关闭数据流,如果是false则在finally里关闭
// IOUtils.copyBytes(in, output, 4096, false);
IOUtils.copyBytes(in, output, conf);
output.flush();
System.out.print(" download successed.");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.print(" download failed.");
} finally {
IOUtils.closeStream(in);
IOUtils.closeStream(output);
}
}
}
}
} catch (Exception e) {
} finally {
System.out.println("the number of files is :" + i);
}
return true;
} //下载目录下所有文件,方法2: 按字节复制
public static boolean downFromHdfsDir2(String hdfsSrc, String localDst)
throws IOException {
Configuration conf = new Configuration();
Path dstpath = new Path(hdfsSrc);
int i = 1;
FileSystem fs = FileSystem.get(URI.create(hdfsSrc), conf);
try {
String subPath = "";
FileStatus[] fList = fs.listStatus(dstpath);
for (FileStatus f : fList) {
if (null != f) {
subPath = new StringBuffer()
.append(f.getPath().getParent()).append("/")
.append(f.getPath().getName()).toString();
if (f.isDir()) {
downFromHdfsDir(subPath, localDst);
} else {
System.out.println("/t/t" + subPath);// hdfs://54.0.88.53:8020/
Path dst = new Path(subPath);
i++;
try {
Path Src = new Path(subPath);
String Filename = Src.getName().toString();
String local = localDst + Filename;
Path Dst = new Path(local);
FileSystem localFS = FileSystem.getLocal(conf);
FileSystem hdfs = FileSystem.get(URI
.create(subPath), conf);
FSDataInputStream in = hdfs.open(Src);
FSDataOutputStream output = localFS.create(Dst);
byte[] buf = new byte[1024];
int readbytes = 0;
while ((readbytes = in.read(buf)) > 0) {
output.write(buf, 0, readbytes);
}
in.close();
output.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.print(" download failed.");
} finally {
}
}
}
}
} catch (Exception e) {
} finally {
System.out.println("the number of files is :" + i);
}
return true;
} //下载目录下所有文件,方法2: 按行复制
public static boolean downFromHdfsDir3(String hdfsSrc, String localDst)
throws IOException {
Configuration conf = new Configuration();
Path dstpath = new Path(hdfsSrc);
int i = 1;
FileSystem fs = FileSystem.get(URI.create(hdfsSrc), conf);
try {
String subPath = "";
FileStatus[] fList = fs.listStatus(dstpath);
for (FileStatus f : fList) {
if (null != f) {
subPath = new StringBuffer()
.append(f.getPath().getParent()).append("/")
.append(f.getPath().getName()).toString();
if (f.isDir()) {
downFromHdfsDir(subPath, localDst);
} else {
System.out.println("/t/t" + subPath);// hdfs://54.0.88.53:8020/
Path dst = new Path(subPath);
i++;
try {
Path Src = new Path(subPath);
String Filename = Src.getName().toString();
String local = localDst + Filename;
Path Dst = new Path(local);
FileSystem localFS = FileSystem.getLocal(conf);
FileSystem hdfs = FileSystem.get(URI
.create(subPath), conf);
FSDataInputStream in = hdfs.open(Src);
BufferedReader read = new BufferedReader(new InputStreamReader(in));
BufferedWriter output=new BufferedWriter(new FileWriter(local));
String line = null;
while ((line = read.readLine()) != null) {
output.append(line);
output.newLine();
output.flush();
}
in.close();
read.close();
output.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.print(" download failed.");
} finally {
}
}
}
}
} catch (Exception e) {
} finally {
System.out.println("the number of files is :" + i);
}
return true;
}

  一次读取整个文件

OutputStream:(一次读入整个文件) 字节

private static String readHdfsFile2(FileSystem fs, Path path, String charset)
throws IOException {
FSDataInputStream hdfsInStream = fs.open(path);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] ioBuffer = new byte[1024];
int readLen = hdfsInStream.read(ioBuffer);
while (-1 != readLen) {
bos.write(ioBuffer, 0, readLen);
readLen = hdfsInStream.read(ioBuffer);
}
hdfsInStream.close();
return new String(bos.toByteArray(), charset);
}
或者
FileStatus status = fs.getFileStatus(Src);
byte[] buffer = new byte[Integer.parseInt(String.valueOf(status.getLen()))];
in.readFully(0, buffer);
is.close();
fs.close();
System.out.println(buffer.toString());

  

Hdfs读取文件到本地总结的更多相关文章

  1. 【ABAP系列】SAP ABAP 从FTP服务器读取文件到本地

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP 从FTP服务器 ...

  2. Spark1.4从HDFS读取文件运行Java语言WordCounts

    Hadoop:2.4.0 Spark:1.4.0 Ubuntu 14.0 1.首先启动Hadoop的HDFS系统.     HADOOP_HOME/sbin/start-dfs.sh 2.在Linux ...

  3. Spark1.4从HDFS读取文件运行Java语言WordCounts并将结果保存至HDFS

    本次实验相关信息如下: 操作系统:Ubuntu 14 Hadoop版本:2.4.0 Spark版本:1.4.0 运行前提是Hadoop与Spark均已正确安装配置 2.在Linux中生成一个文件tes ...

  4. IE8上传文件时javascript读取文件的本地路径的问题("C:\fakepath\")的解决方案

    <script type="text/javascript"> function getPath(obj) { if (obj) { ) { obj.select(); ...

  5. Spark中加载本地(或者hdfs)文件以及SparkContext实例的textFile使用

    默认是从hdfs读取文件,也可以指定sc.textFile("路径").在路径前面加上hdfs://表示从hdfs文件系统上读 本地文件读取 sc.textFile("路 ...

  6. HDFS读文件过程分析:读取文件的Block数据

    转自http://shiyanjun.cn/archives/962.html 我们可以从java.io.InputStream类中看到,抽象出一个read方法,用来读取已经打开的InputStrea ...

  7. 使用JAVA API读取HDFS的文件数据出现乱码的解决方案

    使用JAVA api读取HDFS文件乱码踩坑 想写一个读取HFDS上的部分文件数据做预览的接口,根据网上的博客实现后,发现有时读取信息会出现乱码,例如读取一个csv时,字符串之间被逗号分割 英文字符串 ...

  8. 【HDFS API编程】从本地拷贝文件,从本地拷贝大文件,拷贝HDFS文件到本地

    接着之前继续API操作的学习 CopyFromLocalFile: 顾名思义,从本地文件拷贝 /** * 使用Java API操作HDFS文件系统 * 关键点: * 1)create Configur ...

  9. JAVA本地读取文件,解决中文乱码问题

    JAVA本地读取文件出现中文乱码,查阅一个大神的博客做一下记录 import java.io.BufferedInputStream;import java.io.BufferedReader;imp ...

随机推荐

  1. java中的 private Logger log=Logger.getLogger(this.getClass());

    this.getClass()得到什么? this 表示当前对象的引用: getClass() 是 java.lang.Object 中的方法,它返回一个对象的运行时类: this.getClass( ...

  2. 无法远程连接SQLSERVER2000的解决方法

    有时候客户端和服务器不在同一个局域网里面,这时候很可能无法直接使用服务器名称来标识该服务器,这时候我们可以使用HOSTS文件来进行名字解析,具体的方法是: 1.使用记事本打开HOSTS文件(一般情况下 ...

  3. FastDFS分布式存储实战

    <FastDFS分布式存储实战> 技术选型 FastDFS相关组件及原理 FastDFS介绍 FastDFS架构 FastDFS工作流程 上传 同步机制 下载 文件合并原理 实验环境说明 ...

  4. 排序算法之NB三人组

    快速排序 思路: 例如:一个列表[5,7,4,6,3,1,2,9,8], 1.首先取第一个元素5,以某种方式使元素5归位,此时列表被分为两个部分,左边的部分都比5小,右边的部分都比5大,这时列表变成了 ...

  5. python中的函数对象与闭包函数

    函数对象 在python中,一切皆对象,函数也是对象 在python语言中,声明或定义一个函数时,使用语句: def func_name(arg1,arg2,...): func_suite 当执行流 ...

  6. Linux中的Buffer 与 Cache

    A buffer is something that has yet to be "written" to disk.       A cache is something tha ...

  7. POJ1743 Musical Theme [后缀自动机]

    题意:不重叠最长重复子串 后缀数组做法:http://www.cnblogs.com/candy99/p/6227659.html 后缀自动机的话,首先|Right|>=2 然后min(t[u] ...

  8. 51Nod 欢乐手速场1 C 开心的小Q[莫比乌斯函数]

    开心的小Q tangjz (命题人) quailty (测试)   基准时间限制:1 秒 空间限制:131072 KB 分值: 80 如果一个数字存在一个约数是完全平方数,那么小Q就认为这个数是有趣的 ...

  9. 走进JavaScript——重拾函数

    创建函数 通过构造器的方式来创建函数,最后一个参数为函数体其他为形参 new Function('a','b','alert(a)') /* function anonymous(a,b) { ale ...

  10. Python学习-使用opencv-python提取手掌和手心及部分掌纹

    上次我们成功训练了手掌识别器http://www.cnblogs.com/take-fetter/p/8438747.html,可以成功得到识别的结果如图 接下来需要使用opencv来获取手掌,去除背 ...