12 hdfs常用文件、目录拷贝操作、删除操作
package com.da.hbase.tool.utils; import com.da.hbase.tool.common.Const;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.RemoteIterator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Map; /**
* hdfs操作常用方法类
*/
public class HdfsUtils {
public static final Logger LOG= LoggerFactory.getLogger(HdfsUtils.class);
/**
* 通过ip直接连接hdfs
* @param ip
* @return
*/
public static FileSystem getFsFromIp(String ip){
FileSystem fs = null;
try {
fs=FileSystem.get(URI.create("hdfs://"+ip),new Configuration());
} catch (IOException e) {
LOG.error("此ip:{} 连接出现异常", ip);
}
return fs;
} /**
* 检查该fs是否可用
* @param fs
* @return
*/
public static Boolean checkFs(FileSystem fs){
Boolean success=true;
if(null==fs){
return false;
}
Path path=new Path("/");
try {
RemoteIterator<FileStatus> remoteIterator= fs.listStatusIterator(path);
success=true;
} catch (IOException e) {
success=false;
}
return success;
} /**
* 从ips中获取一个可用的fs
* @param ips
* @return
*/
public static FileSystem getAndCheckFs(String ips){
return getAndCheckFs(ips,",");
}
/**
* 从ips中获取一个可用的fs
* @param ips
* @param separator
* @return
*/
public static FileSystem getAndCheckFs(String ips,String separator){
String [] ipArr=ips.split(separator);
FileSystem fs=null;
for (String ip : ipArr) {
fs=getFsFromIp(ip);
if(checkFs(fs)){
LOG.info("此Ip:{}可连接hdfs",ip);
break;
}else{
fs=null;
}
}
if(null==fs){
LOG.error("无法连接hdfs环境,请检查网络是否可用或者ip配置是否正确,配置ips:{}",ips);
}
return fs;
} /**
* 测试getAndCheckFs方法
*/
private static void testConnectFs(){
String ips="10.17.139.126,10.17.139.127,10.17.139.125";
FileSystem fs=getAndCheckFs(ips);
String path1="/hbase/data/default/";
Path path=new Path(path1);
try {
RemoteIterator<FileStatus> remoteIterator= fs.listStatusIterator(path);
while(remoteIterator.hasNext()){
System.out.println(remoteIterator.next().getPath());
}
} catch (IOException e) {
}
} /**
* 查看当前路径是否存在
* @param fs
* @param path
* @return
*/
public static Boolean checkPathExist(FileSystem fs,String path){
Boolean isExist=true;
try {
isExist=fs.exists(new Path(path));
} catch (IOException e) {
isExist=false;
e.printStackTrace();
}
return isExist;
} /**
* 递归遍历找到所有目录和文件存储在map中,文件,key:路径,value:FILE ;目录,key:路径,value:DIR
* @param fs
* @param src
*/
public static void recureScanDir(FileSystem fs,Path src, Map<Path,String> map){
try{
if(fs.isFile(src)) {
map.put(src, Const.FILE_STATUS);
}else{
map.remove(src);
RemoteIterator<FileStatus> remoteIterator= fs.listStatusIterator(src);
if(!remoteIterator.hasNext()){
map.put(src, Const.DIR_STATUS);
}else {
while (remoteIterator.hasNext()){
recureScanDir(fs,remoteIterator.next().getPath(),map);
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
} /**
* 目录从本地拷贝到hdfs上
* @param fs
* @param src
* @param dst
* @return
*/
public static Boolean copyFromLocal(FileSystem fs,Path src,Path dst){
Boolean success=true;
try {
if(fs.exists(dst)){
fs.delete(dst,true);
}
fs.copyFromLocalFile(false,true,src,dst);
success=true;
} catch (IOException e) {
success=false;
LOG.error("文件从本地拷贝到hdfs上,出现Io异常,导致拷贝文件失败,src:{},dst:{}",src,dst);
e.printStackTrace();
}
return success;
} /**
*目录从hdfs上拷贝到本地
* @param fs
* @param src
* @param dst
* @return
*/
public static Boolean copyToLocal(FileSystem fs,Path src,Path dst){
Boolean success=true;
try {
if(new File(dst.toString()).exists()){
Utils.deletNotEmptyDir(new File(dst.toString()));
}
fs.copyToLocalFile(false, src, dst, true);
success=true;
} catch (IOException e) {
success=false;
LOG.error("文件从hdfs拷贝到本地,出现Io异常,导致拷贝文件失败");
e.printStackTrace();
}
return success;
} private static void testCopyFileToLocal(){
String ips="10.17.139.126,10.17.139.127,10.17.139.125";
FileSystem fs=getAndCheckFs(ips);
String path1="/hbase/data/default/";
Path path=new Path(path1);
try {
RemoteIterator<FileStatus> remoteIterator= fs.listStatusIterator(path);
while(remoteIterator.hasNext()){
System.out.println(remoteIterator.next().getPath());
}
} catch (IOException e) {
LOG.error(e.getMessage());
}
} /**
* 获取目录path下所有的文件名
* @param fs
* @param path
* @return
*/
public static List<String> scanDir(FileSystem fs,Path path){
List<String> list=new ArrayList<>();
try {
RemoteIterator<FileStatus> remoteIterator= fs.listStatusIterator(path);
while(remoteIterator.hasNext()){
list.add(remoteIterator.next().getPath().getName());
}
} catch (IOException e) {
LOG.error(e.getMessage());
}
return list;
} public static void main(String[] args) {
//testConnectFs();
testCopyFileToLocal(); }
}
12 hdfs常用文件、目录拷贝操作、删除操作的更多相关文章
- C#路径,文件,目录,I/O常见操作
C#路径,文件,目录,I/O常见操作 文件操作是程序中非常基础和重要的内容,而路径.文件.目录以及I/O都是在进行文件操作时的常见主题,这里想把这些常见的问题作个总结,对于每个问题,尽量提供 ...
- HDFS常用文件操作
put 上传文件 hadoop fs -put wordcount.txt /data/wordcount/ text 查看文件内容 hadoop fs -text /output/wo ...
- C#路径/文件/目录/I/O常见操作汇总
文件操作是程序中非常基础和重要的内容,而路径.文件.目录以及I/O都是在进行文件操作时的常见主题,这里想把这些常见的问题作个总结,对于每个问题,尽量提供一些解决方案,即使没有你想要的答案,也希望能提供 ...
- 使用scp将文件/目录拷贝到另一台Linux主机上
如何将一台Linux主机上的文件或目录拷贝到另一台Linux主机上,scp命令可以实现该需求 前提条件:两台Linux主机处于同一网段,可以互相ping通 操作如下: 文件拷贝 ①将本地文件拷贝到远端 ...
- C#路径/文件/目录/I/O常见操作汇总<转载>
文件操作是程序中非常基础和重要的内容,而路径.文件.目录以及I/O都是在进行文件操作时的常见主题,这里想把这些常见的问题作个总结,对于每个问题,尽量提供一些解决方案,即使没有你想要的答案,也希望能提供 ...
- 【转】C#路径/文件/目录/I/O常见操作汇总
文件操作是程序中非常基础和重要的内容,而路径.文件.目录以及I/O都是在进行文件操作时的常见主题,这里想把这些常见的问题作个总结,对于每个问题,尽量提供一些解决方案,即使没有你想要的答案,也希望能提供 ...
- 提供一个HDFS内的文件的路径,对该文件进行创建和删除操作。如果文件所在目录不存在,则自动创建目录。
1 import java.text.SimpleDateFormat; 2 3 import org.apache.hadoop.fs.FSDataOutputStream; 4 import or ...
- hadoop HDFS常用文件操作命令
命令基本格式: hadoop fs -cmd < args > 1. ls 列出hdfs文件系统根目录下的目录和文件 hadoop fs -ls /dir hadoop fs -ls -R ...
- Hadoop之HDFS常用文件操作命令
命令基本格式:hadoop fs -cmd < args > 1. ls 列出hdfs文件系统根目录下的目录和文件hadoop fs -ls /dirhadoop fs -ls -R /d ...
随机推荐
- smarty 自定义函数
自定义函数:<{方法名称}> 在lib/plugins中新建文件,命名方式是固定的:function.方法名称.php 或者 block.方法名称.php 1.<{literal}& ...
- jquery1.9学习笔记 之选择器(基本元素一)
所有选择器("*") 描述:选择所有元素 注意:大多数情况下,这个选择器极其的慢,尤其是在作用于自身时. 例子: 查找文档中的每个元素.然后追加一个<script>或& ...
- Nginx源码研究一:NGINX模块启动
Nginx 是一个轻量级,但是高性能的 HTTP 和 代理 服务器,也是一个 IMAP/POP3/SMTP代理服务器. 它的第一个版本0.1.0是由俄罗斯的工程师Igor Sysoev与2004年10 ...
- ubuntu下wine打开自由们找不到MFC42.DLL重新安装的解决方法
一直在找ubuntu下的X墙工具,看到大部分的都是ssh和tor的,但是tor下载不到,找了很多方法,没有办法,只能用FG了.但是Fg是运行在windows系统下的程序. 只好再安装一遍wine,用终 ...
- 一句话输出网站404页面,REFER及相关排序
cat www.log|awk '$9~/404/ {print $7"," $11}'|sort|uniq -c|sort -nr > ./www404.csv
- P0口上拉电阻选择
如果是驱动led,那么用1K左右的就行了.如果希望亮度大一些,电阻可减小,最小不要小于200欧姆,否则电流太大:如果希望亮度小一些,电阻可增大,增加到多少呢,主要看亮度情况,以亮度合适为准,一般来说超 ...
- TCP 的那些事儿(转载)
无论是PC客户端开发还是移动开发,或是Web开发, 网络编程都是很重要的一块, 深入理解TCP/IP和HTTP协议是一个优秀程序员的必备技能.看到酷壳老大正好写了篇相关文章, 正好拿来学习, 转自 h ...
- 玩转Google开源C++单元测试框架Google Test系列(转载)
越来越多公司采用敏捷开发,单元和回归测试越来越重要,GTest作为最佳C++单元测试工具越来越多的被使用.转自 http://www.cnblogs.com/coderzh/archive/2009/ ...
- html编码转换
http://webdesign.about.com/od/localization/l/blhtmlcodes-math.htm http://www.cnblogs.com/terryglp/ar ...
- office web apps server 问题和解决办法
New-OfficeWebAppsFarm –InternalURL "http://owa.zjkhlib.com" –AllowHttp –EditingEnabled 错误1 ...