代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
hadoop hdfs 文件操作工具类
作者:
yamorn
/ 发布于
2014/11/13
/
530
/** * Created by superman on 14-11-9. */ public class DfsUtils { private Logger logger = Logger.getLogger(getClass().getName()); private Configuration conf; public DfsUtils(Configuration conf) { this.conf = conf; if (conf.get("fs.default.name").equals("file:///")) { conf.set("fs.default.name", "hdfs://localhost:9000"); } } public void traverse(String file, PathFilter pathFilter, DfsFileHandler handler) throws Exception { FileSystem fs = FileSystem.get(URI.create("/"), conf); Path path = new Path(file); _traverse(fs, path, pathFilter, handler); fs.close(); } private void _traverse(FileSystem fs, Path path, PathFilter pathFilter, DfsFileHandler handler) throws IOException { if (fs.isFile(path)) { handler.handler(path); } else { FileStatus[] list = fs.listStatus(path, pathFilter); for (FileStatus stat : list) { _traverse(fs, stat.getPath(), pathFilter, handler); } } } public void mkdirs(String dir) throws Exception { FileSystem fs = FileSystem.get(URI.create("/"), conf); Path path = new Path(dir); if (!fs.exists(path)) { if (!fs.mkdirs(path)) { logger.warning("Create dir " + path.toString() + " failed."); } } else { logger.info(dir + " already exists."); } fs.close(); } public void rm(String file, boolean recursive) throws Exception { FileSystem fs = FileSystem.get(URI.create("/"), conf); Path path = new Path(file); if (!fs.delete(path, recursive)) { logger.warning("Delete path " + file + " failed."); } fs.close(); } public void createFile(String file, String content, boolean overwrite) throws Exception { FileSystem fs = FileSystem.get(URI.create("/"), conf); byte[] buff = content.getBytes(); FSDataOutputStream os = null; try { os = fs.create(new Path(file), overwrite); os.write(buff, 0, buff.length); } finally { if (os != null) { os.close(); } } fs.close(); } public String cat(String file) throws Exception { FileSystem fs = FileSystem.get(URI.create("/"), conf); Path path = new Path(file); if (!fs.exists(path)) { logger.warning(file + " doesn't exist."); return null; } if (!fs.isFile(path)) { logger.warning(file + " isn't a file."); return null; } FSDataInputStream is = null; ByteArrayOutputStream os = new ByteArrayOutputStream(); try { is = fs.open(path); IOUtils.copyBytes(is, os, 4096); } finally { IOUtils.closeStream(is); IOUtils.closeStream(os); } fs.close(); return os.toString(); } public void copyFromLocal(String src, String dst, boolean delSrc, boolean overwrite) throws Exception { FileSystem fs = FileSystem.get(URI.create("/"), conf); File file = new File(src); if (!file.exists()) { logger.log(Level.SEVERE, src + " doesn't exists."); return; } Path dstPath = new Path(dst); fs.copyFromLocalFile(delSrc, overwrite, new Path(src), dstPath); fs.close(); } public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); // conf.set("fs.default.name","hdfs://localhost:9000"); DfsUtils dfsUtils = new DfsUtils(conf); //// dfsUtils.copyFromLocal("/home/louis/dic.txt","/home/dic.txt",false,false); // String str=dfsUtils.cat("/home/dic.txt"); // System.out.println(str); } } /** * Created by superman on 14-11-9. */ public interface DfsFileHandler { public void handler(Path path); }
试试其它关键字
文件操作
hadoop
hdfs
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
yamorn
贡献的其它代码
(
2
)
.
使用变步长的龙格库塔算法求解常微分方程
.
hadoop hdfs 文件操作工具类
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3