hadoop的dfs工具类一个

  开始没搞定插件问题,就弄了个dsf操作类,后面搞定了插件问题,这玩意也就聊胜于无了,还是丢这里算了。

  首先是一个配置,ztool.hadoop.properties

hadoop.home.dir=G:/hadoop/hadoop-2.4.1
hadoop.user.name=hadoop

hadoop.server.ip=192.168.117.128
hadoop.server.hdfs.port=9000

  前面两个属性后面代码会有说明的。

  属性文件的读取,方法多了,一般用commons-configuration包,我是自己把这个再整了一次,加了些自动处理,这个代码中可以无视,直接把代码中的那部分改成普通引用就好了。

  logger部分,用了logback,也是处理了一下,处理了其在linux下会莫名其妙找不到配置文件的问题。这里就不放出代码了,直接把代码中的那部分改成普通引用就好了,我就不改了。

  工具类代码如下

package com.cnblogs.zxub.hadoop.dfs;

import java.io.IOException;
import java.net.URI;

import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.mapred.JobConf;
import org.slf4j.Logger;

import com.cnblogs.zxub.util.logger.ZLoggerFactory;
import com.cnblogs.zxub.util.properties.PropertiesLoader;

public class DfsUtil {

    private static final Logger logger = ZLoggerFactory
            .getLogger(DfsUtil.class);

    private final PropertiesConfiguration props = PropertiesLoader
            .getConfiguration("ztool.hadoop");
    private Configuration config = null;
    private String hdfsPath = null;

    private String baseDir = null;

    public DfsUtil(String hdfs, Configuration config) {
        // windows下设置HADOOP_HOME后,还可能找不到winutils.exe,直接自己写进程序算了
        System.setProperty("hadoop.home.dir",
                this.props.getString("hadoop.home.dir"));
        // 设置与dfs服务通信的用户名,省得换当前用户名,也不改配置关闭权限控制了
        System.setProperty("HADOOP_USER_NAME",
                this.props.getString("hadoop.user.name"));
        this.hdfsPath = (hdfs == null) ? "hdfs://"
                + this.props.getString("hadoop.server.ip") + ":"
                + this.props.getString("hadoop.server.hdfs.port") + "/" : hdfs;
        if (config == null) {
            JobConf conf = new JobConf(DfsUtil.class);
            conf.setJobName("HdfsDAO");
            config = conf;
        }
        this.config = config;
    }

    public DfsUtil(Configuration conf) {
        this(null, conf);
    }

    public DfsUtil() {
        this(null, null);
    }

    public String getBaseDir() {
        return this.baseDir;
    }

    public void setBaseDir(String baseDir) {
        this.baseDir = baseDir;
    }

    public String getHdfsPath() {
        return this.hdfsPath;
    }

    public Configuration getConfig() {
        return this.config;
    }

    private String standardPath(String path) {
        if (this.baseDir == null) {
            this.baseDir = "/";
        }
        if (this.baseDir.indexOf("/") != 0) {
            this.baseDir = "/" + this.baseDir;
        }
        if (this.baseDir.lastIndexOf("/") == this.baseDir.length() - 1) {
            this.baseDir = this.baseDir.replaceFirst("/$", "");
        }
        if (path.indexOf("/") != 0) {
            path = "/" + path;
        }
        path = this.baseDir + path;
        if (path.lastIndexOf("/") == path.length() - 1) {
            path = path.replaceFirst("/$", "");
        }
        return path;
    }

    public void ll(String folder) throws IOException {
        folder = this.standardPath(folder);
        Path path = new Path(folder);
        FileSystem fs = FileSystem.get(URI.create(this.getHdfsPath()),
                this.getConfig());
        FileStatus[] list = fs.listStatus(path);
        System.out.println("ll: " + folder);
        for (FileStatus f : list) {
            System.out.printf("name: %s, folder: %s, size: %d\n", f.getPath(),
                    f.isDirectory(), f.getLen());
        }
        fs.close();
    }

    public void mkdirs(String folder) throws IOException {
        folder = this.standardPath(folder);
        Path path = new Path(folder);
        FileSystem fs = FileSystem.get(URI.create(this.getHdfsPath()),
                this.getConfig());
        if (!fs.exists(path)) {
            fs.mkdirs(path);
            logger.info("create: {}.", folder);
        } else {
            logger.warn("folder [{}] already exists, mkdirs failed.", folder);
        }
        fs.close();
    }

    public void rm(String file) throws IOException {
        file = this.standardPath(file);
        Path path = new Path(file);
        FileSystem fs = FileSystem.get(URI.create(this.getHdfsPath()),
                this.getConfig());
        fs.deleteOnExit(path);
        logger.info("delete: {}.", file);
        fs.close();
    }

    public void newFile(String file, String content) throws IOException {
        file = this.standardPath(file);
        FileSystem fs = FileSystem.get(URI.create(this.getHdfsPath()),
                this.getConfig());
        byte[] buff = content.getBytes();
        FSDataOutputStream os = null;
        try {
            os = fs.create(new Path(file));
            os.write(buff, 0, buff.length);
            logger.info("create: {}.", file);
        } finally {
            if (os != null) {
                os.close();
            }
        }
        fs.close();
    }

    public void scp(String local, String remote) throws IOException {
        remote = this.standardPath(remote);
        FileSystem fs = FileSystem.get(URI.create(this.getHdfsPath()),
                this.getConfig());
        fs.copyFromLocalFile(new Path(local), new Path(remote));
        logger.info("copy: from [{}] to [{}]", local, remote);
        fs.close();
    }

    public void download(String remote, String local) throws IOException {
        remote = this.standardPath(remote);
        Path path = new Path(remote);
        FileSystem fs = FileSystem.get(URI.create(this.getHdfsPath()),
                this.getConfig());
        fs.copyToLocalFile(path, new Path(local));
        logger.info("download: from [{}] to [{}]", remote, local);
        fs.close();
    }

    public void cat(String remote) throws IOException {
        remote = this.standardPath(remote);
        Path path = new Path(remote);
        FileSystem fs = FileSystem.get(URI.create(this.getHdfsPath()),
                this.getConfig());
        FSDataInputStream fsdis = null;
        System.out.println("cat: " + remote);
        try {
            fsdis = fs.open(path);
            IOUtils.copyBytes(fsdis, System.out, 4096, false);
        } finally {
            IOUtils.closeStream(fsdis);
            fs.close();
        }
    }

    public static void main(String[] args) throws IOException {
        DfsUtil hdfs = new DfsUtil();
        // hdfs.setBaseDir("/test");
        // hdfs.mkdirs("/debug_in");
        // hdfs.newFile("/test.txt", "测试");
        // hdfs.rm("/test.txt");
        // hdfs.rm("/test");
        // hdfs.scp("c:/q.txt", "/");
        // hdfs.ll("/");
        // hdfs.download("/test.txt", "c:/t.txt");
        // hdfs.cat("q.txt");
        hdfs.scp("c:/din/f1.txt", "debug_in");
        hdfs.scp("c:/din/f2.txt", "debug_in");
    }
}

hadoop的dfs工具类一个

时间: 2024-08-03 20:39:07

hadoop的dfs工具类一个的相关文章

hadoop 写入文件工具类

package com.wecash.bi.util; import java.io.File; import java.io.FileOutputStream; import java.net.URI; import java.util.List; import org.apache.commons.io.IOUtils; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.FSDataOutputS

ImageUtils-图片工具类

ImageUtils-图片工具类 一个日常项目中经常要用到的工具类,直接复制到项目中使用. 更新于:2015-08-10 import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Matrix; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; public clas

UIViewAdditions(一个很方便使用的工具类吧)

我们在工程中,或多或少的要修改控件的坐标-宽度-高度,于是,经常性的见到大家self.view.frame.origin.x,self.view.frame.size.width.........相当的麻烦,在这里向大家推荐一个比较好的工具类,是UIView的类目,它里面对于求坐标,求高度什么的做了封装,很方便大家调用. @下载链接:点击这里 @.h #import <Foundation/Foundation.h> #import <UIKit/UIKit.h> @interfa

自己用反射写的一个request.getParameter工具类

适用范围:当我们在jsp页面需要接收很多值的时候,如果用request.getParameter(属性名)一个一个写的话那就太麻烦了,于是我想是 否能用反射写个工具类来简化这样的代码,经过1个小时的代码修改调试,终于雏形出来了,很高兴调试成功,呵呵,代码贴出来. package com.letv.uts2.utcServer.util; import org.slf4j.Logger;import org.slf4j.LoggerFactory; import java.lang.reflect

翻翻git之---一个丰富的通知工具类 NotifyUtil

转载请注明出处王亟亟的大牛之路 P1(废话板块,今天还加了个小广告) 昨天出去浪,到家把麦麦当当放出来玩一会就整到了12点多..早上睡过头了..简直心酸.... 最近手头上有一些职位可以操作,然后这里Share给大家 公司:暴走大事件 职位:Android/iOS开发 地点:上海 公司:Pactera 职位:Android/Java/PHP/.Net/Web前端/测试/UI设计 地点:上海(这批收的服务于 平安) 公司: 阿里巴巴 职位:Android/iOS 地点:北京/杭州/上海 有意向 可

一个使用命令行编译Android项目的工具类

一个使用命令行编译Android项目的工具类 简介 编译apk项目需要使用的几个工具,基本都在sdk中,它们分别是(Windows系统): 1.aapt.exe 资源打包工具 2.android.jar Android编译工具 3.dx.bat dex文件生成工具 4.sdklib.jar 生成apk 5.jarsigner 签名工具 准备 在打包前,需要的环境如下: 1.JDK1.6+ 2.Android SDK 3.上述5个工具的路径 打包过程 1.生成R.java文件 比如: aapt p

为什么工具类App,都要做一个社区?

非著名程序猿涩郎 非著名程序员,字耿左直右,号涩郎.爱搞机,爱编程,是爬行在移动互联网中的一名码匠! 个人微信号:loonggg,微博:涩郎.专注于移动互联网的开发和研究.本号致力于分享IT技术和程序员工作心得体会. 欢迎大家关注与转载. 为什么工具类App.都要做一个社区? 非著名程序猿 移动互联网的蓬勃发展,以至于应用市场上App数以亿计.工具类App甚多,那天我在知乎上看到了一个问题,那就是:为什么工具类App.无论实用没用,都喜欢加上一个社区呢?当然以下的回答也是五花八门. 并且他们答的

我的Android进阶之旅------&gt;Android关于HttpsURLConnection一个忽略Https证书是否正确的Https请求工具类

下面是一个Android HttpsURLConnection忽略Https证书是否正确的Https请求工具类,不需要验证服务器端证书是否正确 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.UnsupportedEn

一个基于POI的通用excel导入导出工具类的简单实现及使用方法

前言: 最近PM来了一个需求,简单来说就是在录入数据时一条一条插入到系统显得非常麻烦,让我实现一个直接通过excel导入的方法一次性录入所有数据.网上关于excel导入导出的例子很多,但大多相互借鉴.经过思考,认为一百个客户在录入excel的时候,就会有一百个格式版本,所以在实现这个功能之前,所以要统一excel的格式.于是提供了一个通用excel模版的下载功能.当所有客户用模版录入好数据再上传到系统,后端对excel进行解析,然后再持久化到数据库. 概述: 此工具类的几大特点 1.基本导入导出