Chinaunix首页 | 论坛 | 博客
  • 博客访问: 274991
  • 博文数量: 68
  • 博客积分: 125
  • 博客等级: 入伍新兵
  • 技术积分: 606
  • 用 户 组: 普通用户
  • 注册时间: 2012-12-12 15:35
文章分类

全部博文(68)

文章存档

2014年(5)

2013年(59)

2012年(4)

分类: 云计算

2013-01-08 00:00:00

Java接口

读——hdfs 中读取文件

(该程序文件在hadoop 0.20.0版本下使用)
给准备读的文件指定一个path对象,用FileSystem类中的open()方法返回一个FSDataInputStream对象,然后从该数据流中读取数据。
import java.net.URI;
import java.io.*;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.conf.Configuration;

public class FileSystemCat {
    public static void main(String[] args) throws Exception {
        String uri = args[0];
        //Configuration对象封装了客户端或服务器的配置
        Configuration conf = new Configuration();
        //通过给定的URI方案和权限获得我们需要使用的文件系统实例fs
        FileSystem fs = FileSystem.get(URI.create(uri), conf);
        InputStream in = null;
        try {
            //hadoop文件系统中通过Path对象代表文件,调用open()函数来获取文件的输入流
            in = fs.open(new Path(uri));
            IOUtils.copyBytes(in, System.out, 4096, false);
        } finally {
            IOUtils.closeStream(in);
        }
    }
}


写——hdfs 写文件

FileSystem类中有一系列创建文件的方法,最简单的方法是给准备创建的文件指定一个Path对象,然后返回一个用于写入数据的输出流
1.新建文件
import org.apache.hadoop.io.*;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.util.*;
import java.io.*;
import java.net.URI;
public class FileCopyWithProgress {
    public static void main(String[] args) throws Exception {
        String loc = args[0];
        String dst = args[1];
        InputStream in = new BufferedInputStream(new FileInputStream(loc));
        Configuration conf = new Configuration();
        try {
            FileSystem fs = FileSystem.get(URI.create(dst), conf);
            //使用create()方法在指定Path路径中创建文件,并使用Progressable()接口返回写入数据进度
            OutputStream out = fs.create(new Path(dst), new Progressable() {
                public void progress() {
                    System.out.print(".");
                }
            });
            IOUtils.copyBytes(in, out, 4096, false);
        } finally {
            IOUtils.closeStream(in);
            //IOUtils.closeStream(out);
        }
    }
}



2.追加文件

public FSDataOutputStream append(Path f) throws IOException

该追加操作允许一个writer打开文件后在访问该文件的最后偏移量处追加数据。

创建目录

public boolean mkdirs(Path f) throws IOException

查询文件系统

FileStatus 类封装了文件系统中哦给你文件和目录的元数据,包括文件长度,块大小,备份,修改时间,所有者以及权限信息

import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileUtil;
import java.net.URI;
public class ListStatus {
    public static void main(String[] args) throws Exception
    {
        String uri = args[0];
        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(URI.create(uri), conf);
        Path path = new Path(uri);
        new ListStatus().pathPrint(path ,fs, 0);
    }
    public void pathPrint(Path path, FileSystem fs, int level) throws Exception {
        //FileSystem类中listStatus方法返回FileStatus[]对象表示该路径下的文件或目录的状态
        FileStatus[] status = fs.listStatus(path);
        int le = level;
        for(int i=0;i        {
            if(status[i].isDir()) {
                Path path1 = status[i].getPath();
                System.out.println(le + " " + path1);
                pathPrint(path1, fs, le + 1);
            } else {
                Path[] listedPaths = FileUtil.stat2Paths(status);
                for(Path p : listedPaths) {
                System.out.println(le + " " + p);
                }
            }
        }
    }
}
//未能实现文件分目录层次打印(即前面有多少个标点符号)


删除数据

使用FileSystem.delete()方法可以永久性删除文件或目录

public boolean delete(Path f, boolean recursive) throws IOException

如果f是一个文件或空目录,那么recursive的值就会被忽略,只有在recursive值为true时,一个非空目录及其内容才会被删除(否则抛出IOException异常)

阅读(1615) | 评论(0) | 转发(1) |
0

上一篇:Shell script

下一篇:查看linux系统版本命令

给主人留下些什么吧!~~