Java操作HDFS文件

15 篇文章 0 订阅

1.读取单个文件

 

 

[java] view plain copy

  1. Date date = DateUtil.getSpecifiedDayBefore();  
  2. String yesterday = DateUtil.dateToStr(date, "yyyy-MM-dd");  
  3. String path = "hdfs://ip:9000/output_log/output_log_click" + yesterday;  
  4.   
  5.  Configuration conf = new Configuration();  
  6. FileSystem fs = FileSystem.get(URI.create(path), conf);  
  7. FSDataInputStream hdfsInStream = fs.open(new Path(path));  
  8. InputStreamReader isr = new InputStreamReader(hdfsInStream, "utf-8");  
  9. BufferedReader br = new BufferedReader(isr);  
  10. String line;  
  11. // int k = 0;  
  12. while ((line = br.readLine()) != null) {  
  13.     System.out.println(line);  
  14. }  
  15.    


 

 

2.读取文件夹

 

[java] view plain copy

  1. Date date = DateUtil.getSpecifiedDayBefore();  
  2. String yesterday = DateUtil.dateToStr(date, "yyyy-MM-dd");  
  3. String path = "hdfs://ip:9000/output_log/output_log_click" + yesterday;  
  4.   
  5. Configuration conf = new Configuration();  
  6. FileSystem fs = FileSystem.get(URI.create(path), conf);  
  7. FileStatus[] status = fs.listStatus(new Path(path));  
  8. for (FileStatus file : status) {  
  9.     if (!file.getPath().getName().startsWith("newsMap")) {  
  10.               continue;  
  11.           }  
  12.     FSDataInputStream hdfsInStream = fs.open(file.getPath());  
  13.     InputStreamReader isr = new InputStreamReader(hdfsInStream, "utf-8");  
  14.     BufferedReader br = new BufferedReader(isr);  
  15.     String line;  
  16.     // int k = 0;  
  17.     while ((line = br.readLine()) != null) {  
  18.         System.out.println(line);  
  19.     }  
  20.       
  21. }  

 

package archy.com;

import java.io.InputStream;
import java.net.URI;
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.io.IOUtils;

public class MyHdfs {
    // hadoop fs的配置文件
    static Configuration conf = new Configuration(true);
    static {
        // 指定hadoop fs的地址
        conf.set("fs.default.name", "hdfs://bigdata01.com:8020");
    }

    /**
     * 将本地文件(filePath)上传到HDFS服务器的指定路径(dst)
     * 
     * @author 冯琪
     */
    public static void uploadFileToHDFS(String filePath, String dst)
            throws Exception {
        // 创建一个文件系统
        FileSystem fs = FileSystem.get(conf);
        Path srcPath = new Path(filePath);
        Path dstPath = new Path(dst);
        Long start = System.currentTimeMillis();
        fs.copyFromLocalFile(false, srcPath, dstPath);
        System.out.println("Time:" + (System.currentTimeMillis() - start));
        System.out.println("________准备上传文件" + conf.get("fs.default.name")
                + "____________");
        fs.close();
        getDirectoryFromHdfs(dst);
    }

    /**
     * 下载文件
     * 
     * @author 冯琪
     */
    public static void downLoadFileFromHDFS(String src) throws Exception {
        FileSystem fs = FileSystem.get(conf);
        Path srcPath = new Path(src);
        InputStream in = fs.open(srcPath);
        try {
            // 将文件COPY到标准输出(即控制台输出)
            IOUtils.copyBytes(in, System.out, 4096, false);
        } finally {
            IOUtils.closeStream(in);
            fs.close();
        }
    }

    /**
     * 遍历指定目录(direPath)下的所有文件
     * 
     * @author 冯琪
     */
    public static void getDirectoryFromHdfs(String direPath) throws Exception {

        FileSystem fs = FileSystem.get(URI.create(direPath), conf);
        FileStatus[] filelist = fs.listStatus(new Path(direPath));
        for (int i = 0; i < filelist.length; i++) {
            System.out.println("_________________第" + i + "个文件"
                    + "____________________");
            FileStatus fileStatus = filelist[i];
            System.out.println("Name:" + fileStatus.getPath().getName());
            System.out.println("size:" + fileStatus.getLen());
            System.out.println("_________________第" + i + "个文件"
                    + "____________________");
        }
        fs.close();
    }

    /**
     * 测试方法
     * 
     * @author 冯琪
     */
    public static void main(String[] args) {
        try {
            // 获取目录下的所有文件,hdfs服务器文件夹路径
            getDirectoryFromHdfs("/fengqi/1104/");
            // 上传文件,第一个参数为本地要上传的文件路径,第二个参数为HDFS的路径。
            // uploadFileToHDFS("D:/456.txt", "/fengqi/1104/");
            // 下载文件,第一个参数为HDFS上要下载的文件路径
            // downLoadFileFromHDFS("/fengqi/1104/456.txt");
        } catch (Exception e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
    }
}
--------------------- 
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值