总结了一下三个方法: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. python字符串问题

    相关知识点: 字符串在Python内部的表示是unicode编码,因此,在做编码转换时,通常需要以unicode作为中间编码,即先将其他编码的字符串解码(decode)成unicode,再从unico ...

  2. awk 里的substr()

    awk 里的substr函数用法举例: 要截取要截取的内容1: F115!16201!1174113017250745 10.86.96.41 211.140.16.1 200703180718F12 ...

  3. 【转】CentOS 6.3(x86_32)下安装Oracle 10g R2

    一.硬件要求 1.内存 & swap Minimum: 1 GB of RAMRecommended: 2 GB of RAM or more 检查内存情况 # grep MemTotal / ...

  4. Java基础知识(一)

    类与对象 1.对象:客观存在的一切事物称之为对象 类:具有相同属性和方法的对象的集合 2.类:属性,方法 3.修饰符: public protected  默认(不写)  private 任何地方   ...

  5. easyUI前后台分页代码实现

    一.后台分页 (1)客户端代码: var dg = $('#table'); var opts = dg.datagrid('options'); var pager = dg.datagrid('g ...

  6. JAVA设计模式---命令模式

    1.定义: 将“请求”封装成对象,以便使用不同的请求.队列或者日志来参数化其他对象,命令模式也支持可撤销的操作.命令可以用来实现日志和事务系统. 2.实例: 1)需求:设计一个家电遥控器的API,遥控 ...

  7. 简易发号SQL,可用于生成指定前缀自增序列,如订单号,生成优惠券码等

    需求1:订单号要求唯一.长度不太长.自增.但不能通过早上订单号和晚上订单号相减推算出平台大概一天的单量 需求2:要求生成10w张优惠券,要求券码唯一.不能太长,不能轻易猜测出其他券码 根据这些需求提供 ...

  8. 将常用的Android adb shell 命令行封装为C#静态函数

    更多原创测试技术文章同步更新到微信公众号 :三国测,敬请扫码关注个人的微信号,感谢! 简介:adb命令是常用的Android命令行,自动化.代码调试.手工排查问题都会用的到,这里将常用的一些命令行封装 ...

  9. c# excel print 打印 将所有列调整为一页

    excel有时候列数比较多,行数也比较多,转换成xps文档的时候,一般是通过打印来实现. 由于打印的范围限制,所以会出现本来在一行的数据,由于列数比较多,溢出范围,被打印到两页了. 为解决这个问题,需 ...

  10. python学习--Linux下dlib安装(主要是cmake和boost的安装)

    昨天我们使用了dlib和opencv进行了人脸检测标注(http://www.cnblogs.com/take-fetter/p/8310298.html) 但是运行环境是基于windows的而且可能 ...