总结了一下三个方法: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. JSP内置对象值out对象及其它的一些常见方法

    out对象: out对象是jspWriter类的实例,是向客户端输出内容常用的对象. 常用方法如下: void println() 向客户端打印字符串 void clear() 清除缓冲区的内容,如果 ...

  2. win10预览版无开始菜单解决方案

    1.按下Win+R键打开“运行”程序,键入gpedit.msc 回车以打开本地组策略编辑器 2.调到图示位置将windows设置->安全设置->本地策略->安全选项->“用户账 ...

  3. 【转】一些常用的Vi命令,可帮助脱离鼠标

    使用Vi编写代码时,如果想脱离鼠标,需要使用一些命令快捷键,下面罗列了一些常用的并且容易记住的: 1. 命令模式下,移动光标或跳转 0到行首 ^到行首第一个非空字符 $到行尾非空字符 fx向后移动光标 ...

  4. 关于OMAPL138烧写程序的说明

    相信很多朋友在用CCS调试OMAPL138开发板的时候,肯定遇到了许许多多的问题: 例如: 1.CCS安装不完整,导致有些功能无法使用 2.ARM端没有加载gel文件,使得程序无法被唤醒 3.ccxm ...

  5. Java锁概念基础

    Java中的锁不管是Lock还是synchronized都可以分为互斥锁和非互斥锁. 互斥锁只能被一个线程持有,其他线程只能等待锁的释放.synchronized,ReentrantLock,Read ...

  6. CentOS上安装Git服务器

    1.安装Git 打开控制台,执行以下命令进行安装 $ yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel ...

  7. xcode7中使用cocos2d-x3.8的webview控件

    在XCode7中使用cocos2d-x 3.3以上版本的WebView控件时,碰到了编译错误 App Transport Security has blocked a cleartext HTTP ( ...

  8. WPF: WPF 中的 Triggers 和 VisualStateManager

    在之前写的这篇文章 WPF: 只读依赖属性的介绍与实践 中,我们介绍了在 WPF 自定义控件中如何添加只读依赖属性,并且使其结合属性触发器 (Trigger) 来实现对控件样式的改变.事实上,关于触发 ...

  9. SQL Server 文件操作

    在master数据库中,SQL Server提供系统扩展的存储过程,其中有一些存储过程的命名以xp_开头,用于处理操作系统的文件. 一,判断文件是否存在 存储过程sys.xp_fileexist 用于 ...

  10. OpenCV角点检测源代码分析(Harris和ShiTomasi角点)

    OpenCV中常用的角点检测为Harris角点和ShiTomasi角点. 以OpenCV源代码文件 .\opencv\sources\samples\cpp\tutorial_code\Trackin ...