评论管家之HDFS

package com.movie.arith;

import java.io.IOException;
import java.text.SimpleDateFormat;

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.hdfs.DistributedFileSystem;
import org.apache.hadoop.io.IOUtils;

/**
 * HDFS文件操作
 *
 */
public class HDFSFile {
    Configuration conf = new Configuration();
    private FileSystem hdfs;
    /**
     * 构建hdfs文件系统对象
     * 
     * @param hdfsPath
     * @throws IOException
     */
    public HDFSFile(Path hdfsPath) throws IOException{
        hdfs = hdfsPath.getFileSystem(conf);
    }
    /**
     * 创建目录
     * @param path
     * @throws IOException
     */
    public void mkDir(Path path) throws IOException{        
        hdfs.mkdirs(path);
    }
    /**
     * 上传文件
     * @param src
     * @param dst
     * @throws IOException
     */
    public void copyLocalToHdfs(Path src,Path dst) throws IOException{
        hdfs.copyFromLocalFile(src, dst);
    }
    /**
     * 删除文件
     * @param path
     * @throws IOException
     */
    @SuppressWarnings("deprecation")
    public void delFile(Path path) throws IOException{
        hdfs.delete(path);
    }
    /**
     * 读取文件内容
     * @param path
     * @throws IOException
     */
    public void readFile(Path path) throws IOException{
        //获取文件信息
        FileStatus filestatus = hdfs.getFileStatus(path);
        //FS的输入流
        FSDataInputStream in = hdfs.open(path);
        //用Hadoop的IOUtils工具方法来让这个文件的指定字节复制到标准输出流上
        IOUtils.copyBytes(in,System.out,(int)filestatus.getLen(),false);
        System.out.println();
    }
    /**
     * 得到文件的修改时间
     * @param path
     * @throws IOException
     */
    public void getModifyTime(Path path) throws IOException{
        FileStatus files[] = hdfs.listStatus(path);
        for(FileStatus file: files){
            //time = file.getModificationTime().
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            String date = sdf.format(file.getModificationTime());
            System.out.println(file.getPath()+"\t"+date);
        }
    }
    /**
     * 在hdfs上创建文件并写入内容
     * @param path
     * @param content
     * @throws IOException
     */
    public void writeFile(Path path,String content) throws IOException{
        FSDataOutputStream os = hdfs.create(path);
        //以utf-8的格式写入文件
        os.write(content.getBytes("UTF-8"));
        os.close();
    }
    /**
     * 列出某一路径下所有的文件
     * @param path
     * @throws IOException
     */
    public void listFiles(Path path) throws IOException{
        hdfs = path.getFileSystem(conf);
        FileStatus files[] = hdfs.listStatus(path);
        int listlength=files.length;  
        for (int i=0 ;i<listlength ;i++){  
            if (files[i].isDir() == false) {  
                System.out.println("filename:"  
                        + files[i].getPath()  + "\tsize:"  
                        + files[i].getLen());  
            } else {  
                Path newpath = new Path(files[i].getPath().toString());  
                listFiles(newpath);  
            }  
        }  
    }
}
 

转载于:https://my.oschina.net/u/3581306/blog/1143059

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值