Hadoop文件系统 
基本的文件系统命令操作, 通过hadoop fs -help可以获取所有的命令的详细帮助文件。

Java抽象类org.apache.hadoop.fs.FileSystem定义了hadoop的一个文件系统接口。该类是一个抽象类,通过以下两种静态工厂方法可以过去FileSystem实例: 
public static FileSystem.get(Configuration conf) throws IOException 
public static FileSystem.get(URI uri, Configuration conf) throws IOException

具体方法实现: 
1、public boolean mkdirs(Path f) throws IOException
一次性新建所有目录(包括父目录), f是完整的目录路径。

2、public FSOutputStream create(Path f) throws IOException
创建指定path对象的一个文件,返回一个用于写入数据的输出流 
create()有多个重载版本,允许我们指定是否强制覆盖已有的文件、文件备份数量、写入文件缓冲区大小、文件块大小以及文件权限。

3、public boolean copyFromLocal(Path src, Path dst) throws IOException
将本地文件拷贝到文件系统

4、public boolean exists(Path f) throws IOException
检查文件或目录是否存在

5、public boolean delete(Path f, Boolean recursive)
永久性删除指定的文件或目录,如果f是一个空目录或者文件,那么recursive的值就会被忽略。只有recursive=true时,一个非空目录及其内容才会被删除。

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

通过"FileStatus.getPath()"可查看指定HDFS中某个目录下所有文件。

package hdfsTest;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

public class OperatingFiles {
 //initialization
 static Configuration conf = new Configuration();
 static FileSystem hdfs;
 static {
  String path = "/usr/java/hadoop-1.0.3/conf/";
  conf.addResource(new Path(path + "core-site.xml"));
  conf.addResource(new Path(path + "hdfs-site.xml"));
  conf.addResource(new Path(path + "mapred-site.xml"));
  path = "/usr/java/hbase-0.90.3/conf/";
  conf.addResource(new Path(path + "hbase-site.xml"));
  try {
   hdfs = FileSystem.get(conf);
  } catch (IOException e) {
   e.printStackTrace();
  }
 }
 
 //create a direction
 public void createDir(String dir) throws IOException {
  Path path = new Path(dir);
  hdfs.mkdirs(path);
  System.out.println("new dir \t" + conf.get("fs.default.name") + dir);
 } 
 
 //copy from local file to HDFS file
 public void copyFile(String localSrc, String hdfsDst) throws IOException{
  Path src = new Path(localSrc);  
  Path dst = new Path(hdfsDst);
  hdfs.copyFromLocalFile(src, dst);
  
  //list all the files in the current direction
  FileStatus files[] = hdfs.listStatus(dst);
  System.out.println("Upload to \t" + conf.get("fs.default.name") + hdfsDst);
  for (FileStatus file : files) {
   System.out.println(file.getPath());
  }
 }
 
 //create a new file
 public void createFile(String fileName, String fileContent) throws IOException {
  Path dst = new Path(fileName);
  byte[] bytes = fileContent.getBytes();
  FSDataOutputStream output = hdfs.create(dst);
  output.write(bytes);
  System.out.println("new file \t" + conf.get("fs.default.name") + fileName);
 }
 
 //list all files
 public void listFiles(String dirName) throws IOException {
  Path f = new Path(dirName);
  FileStatus[] status = hdfs.listStatus(f);
  System.out.println(dirName + " has all files:");
  for (int i = 0; i< status.length; i++) {
   System.out.println(status[i].getPath().toString());
  }
 }

//judge a file existed? and delete it!
 public void deleteFile(String fileName) throws IOException {
  Path f = new Path(fileName);
  boolean isExists = hdfs.exists(f);
  if (isExists) { //if exists, delete
   boolean isDel = hdfs.delete(f,true);
   System.out.println(fileName + "  delete? \t" + isDel);
  } else {
   System.out.println(fileName + "  exist? \t" + isExists);
  }
 }

public static void main(String[] args) throws IOException {
  OperatingFiles ofs = new OperatingFiles();
  System.out.println("\n=======create dir=======");
  String dir = "/test";
  ofs.createDir(dir);
  System.out.println("\n=======copy file=======");
  String src = "/home/ictclas/Configure.xml";
  ofs.copyFile(src, dir);
  System.out.println("\n=======create a file=======");
  String fileContent = "Hello, world! Just a test.";
  ofs.createFile(dir+"/word.txt", fileContent);
 }
}

Below is a code sample of how to read from and write to HDFS in java.

1. Creating a configuration object:To be able to read from or write to HDFS, you need to create a Configuration object and pass configuration parameter to it using hadoop configuration files. 

// Conf object will read the HDFS configuration parameters from theseXML
// files. You may specify the parameters for your own if you want.

Configuration conf = new Configuration();
conf.addResource(new Path("/opt/hadoop-0.20.0/conf/core-site.xml"));
conf.addResource(new Path("/opt/hadoop-0.20.0/conf/hdfs-site.xml"));

If you do not assign the configurations to conf object (using hadoop xml file) your HDFS operation will be performed on the local file system and not on the HDFS.  

2. Adding file to HDFS:
Create a FileSystem object and use a file stream to add a file.

FileSystem fileSystem = FileSystem.get(conf);

// Check if the file already exists
Path path = new Path("/path/to/file.ext");
if (fileSystem.exists(path)) {
System.out.println("File " + dest + " already exists");
return;
}

// Create a new file and write data to it.
FSDataOutputStream out = fileSystem.create(path);
InputStream in = new BufferedInputStream(new FileInputStream(
new File(source)));

byte[] b = new byte[1024];
int numBytes = 0;
while ((numBytes = in.read(b)) > 0) {
out.write(b, 0, numBytes);
}

// Close all the file descripters
in.close();
out.close();
fileSystem.close();

3. Reading file from HDFS:Create a file stream object to a file in HDFS and read it.

FileSystem fileSystem = FileSystem.get(conf);

Path path = new Path("/path/to/file.ext");
if (!fileSystem.exists(path)) {
System.out.println("File does not exists");
return;
}

FSDataInputStream in = fileSystem.open(path);

String filename = file.substring(file.lastIndexOf('/') + 1,
file.length());

OutputStream out = new BufferedOutputStream(new FileOutputStream(
new File(filename)));

byte[] b = new byte[1024];
int numBytes = 0;
while ((numBytes = in.read(b)) > 0) {
out.write(b, 0, numBytes);
}

in.close();
out.close();
fileSystem.close();

3. Deleting file from HDFS:Create a file stream object to a file in HDFS and delete it.

FileSystem fileSystem = FileSystem.get(conf);

Path path = new Path("/path/to/file.ext");
if (!fileSystem.exists(path)) {
System.out.println("File does not exists");
return;
}

// Delete file
fileSystem.delete(new Path(file), true);

fileSystem.close();

3. Create dir in HDFS:Create a file stream object to a file in HDFS and read it.

FileSystem fileSystem = FileSystem.get(conf);

Path path = new Path(dir);
if (fileSystem.exists(path)) {
System.out.println("Dir " + dir + " already not exists");
return;
}

// Create directories
fileSystem.mkdirs(path);

fileSystem.close();

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

public class HDFSClient {
    public HDFSClient() {

}

public void addFile(String source, String dest) throws IOException {
        Configuration conf = new Configuration();

// Conf object will read the HDFS configuration parameters from these
        // XML files.
        conf.addResource(new Path("/opt/hadoop-0.20.0/conf/core-site.xml"));
        conf.addResource(new Path("/opt/hadoop-0.20.0/conf/hdfs-site.xml"));

FileSystem fileSystem = FileSystem.get(conf);

// Get the filename out of the file path
        String filename = source.substring(source.lastIndexOf('/') + 1,
            source.length());

// Create the destination path including the filename.
        if (dest.charAt(dest.length() - 1) != '/') {
            dest = dest + "/" + filename;
        } else {
            dest = dest + filename;
        }

// System.out.println("Adding file to " + destination);

// Check if the file already exists
        Path path = new Path(dest);
        if (fileSystem.exists(path)) {
            System.out.println("File " + dest + " already exists");
            return;
        }

// Create a new file and write data to it.
        FSDataOutputStream out = fileSystem.create(path);
        InputStream in = new BufferedInputStream(new FileInputStream(
            new File(source)));

byte[] b = new byte[1024];
        int numBytes = 0;
        while ((numBytes = in.read(b)) > 0) {
            out.write(b, 0, numBytes);
        }

// Close all the file descripters
        in.close();
        out.close();
        fileSystem.close();
    }

public void readFile(String file) throws IOException {
        Configuration conf = new Configuration();
        conf.addResource(new Path("/opt/hadoop-0.20.0/conf/core-site.xml"));

FileSystem fileSystem = FileSystem.get(conf);

Path path = new Path(file);
        if (!fileSystem.exists(path)) {
            System.out.println("File " + file + " does not exists");
            return;
        }

FSDataInputStream in = fileSystem.open(path);

String filename = file.substring(file.lastIndexOf('/') + 1,
            file.length());

OutputStream out = new BufferedOutputStream(new FileOutputStream(
            new File(filename)));

byte[] b = new byte[1024];
        int numBytes = 0;
        while ((numBytes = in.read(b)) > 0) {
            out.write(b, 0, numBytes);
        }

in.close();
        out.close();
        fileSystem.close();
    }

public void deleteFile(String file) throws IOException {
        Configuration conf = new Configuration();
        conf.addResource(new Path("/opt/hadoop-0.20.0/conf/core-site.xml"));

FileSystem fileSystem = FileSystem.get(conf);

Path path = new Path(file);
        if (!fileSystem.exists(path)) {
            System.out.println("File " + file + " does not exists");
            return;
        }

fileSystem.delete(new Path(file), true);

fileSystem.close();
    }

public void mkdir(String dir) throws IOException {
        Configuration conf = new Configuration();
        conf.addResource(new Path("/opt/hadoop-0.20.0/conf/core-site.xml"));

FileSystem fileSystem = FileSystem.get(conf);

Path path = new Path(dir);
        if (fileSystem.exists(path)) {
            System.out.println("Dir " + dir + " already not exists");
            return;
        }

fileSystem.mkdirs(path);

fileSystem.close();
    }

public static void main(String[] args) throws IOException {

if (args.length < 1) {
            System.out.println("Usage: hdfsclient add/read/delete/mkdir" +
                " [<local_path> <hdfs_path>]");
            System.exit(1);
        }

HDFSClient client = new HDFSClient();
        if (args[0].equals("add")) {
            if (args.length < 3) {
                System.out.println("Usage: hdfsclient add <local_path> " + 
                "<hdfs_path>");
                System.exit(1);
            }

client.addFile(args[1], args[2]);
        } else if (args[0].equals("read")) {
            if (args.length < 2) {
                System.out.println("Usage: hdfsclient read <hdfs_path>");
                System.exit(1);
            }

client.readFile(args[1]);
        } else if (args[0].equals("delete")) {
            if (args.length < 2) {
                System.out.println("Usage: hdfsclient delete <hdfs_path>");
                System.exit(1);
            }

client.deleteFile(args[1]);
        } else if (args[0].equals("mkdir")) {
            if (args.length < 2) {
                System.out.println("Usage: hdfsclient mkdir <hdfs_path>");
                System.exit(1);
            }

client.mkdir(args[1]);
        } else {   
            System.out.println("Usage: hdfsclient add/read/delete/mkdir" +
                " [<local_path> <hdfs_path>]");
            System.exit(1);
        }

System.out.println("Done!");
    }
}

hadoop文件系统FileSystem详解 转自http://hi.baidu.com/270460591/item/0efacd8accb7a1d7ef083d05的更多相关文章

  1. GET、POST详解 --转自http://hi.baidu.com/richarwu/item/bd43633a6ba62986b611dbcd

    HTTP Get,Post请求详解 请求类型 三种最常见的请求类型是:GET,POST 和 HEAD GET:获取一个文档 大部分被传输到浏览器的html,images,js,css, … 都是通过G ...

  2. hadoop基础-SequenceFile详解

    hadoop基础-SequenceFile详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.SequenceFile简介 1>.什么是SequenceFile 序列文件 ...

  3. hdfs文件系统架构详解

    hdfs文件系统架构详解 官方hdfs分布式介绍 NameNode *Namenode负责文件系统的namespace以及客户端文件访问 *NameNode负责文件元数据操作,DataNode负责文件 ...

  4. Hadoop RPC机制详解

    网络通信模块是分布式系统中最底层的模块,他直接支撑了上层分布式环境下复杂的进程间通信逻辑,是所有分布式系统的基础.远程过程调用(RPC)是一种常用的分布式网络通信协议,他允许运行于一台计算机的程序调用 ...

  5. hadoop之yarn详解(框架进阶篇)

    前面在hadoop之yarn详解(基础架构篇)这篇文章提到了yarn的重要组件有ResourceManager,NodeManager,ApplicationMaster等,以及yarn调度作业的运行 ...

  6. 高可用,多路冗余GFS2集群文件系统搭建详解

    高可用,多路冗余GFS2集群文件系统搭建详解 2014.06 标签:GFS2 multipath 集群文件系统 cmirror 实验拓扑图: 实验原理: 实验目的:通过RHCS集群套件搭建GFS2集群 ...

  7. 【转载】Hadoop历史服务器详解

    免责声明:     本文转自网络文章,转载此文章仅为个人收藏,分享知识,如有侵权,请联系博主进行删除.     原文作者:过往记忆(http://www.iteblog.com/)     原文地址: ...

  8. hadoop hdfs uri详解

    body{ font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI& ...

  9. hadoop之mapreduce详解(进阶篇)

    上篇文章hadoop之mapreduce详解(基础篇)我们了解了mapreduce的执行过程和shuffle过程,本篇文章主要从mapreduce的组件和输入输出方面进行阐述. 一.mapreduce ...

随机推荐

  1. 浅谈Java回调机制

    像许多网上介绍回调机制的文章一样,我这里也以一个现实的例子开头:假设你公司的总经理出差前需要你帮他办件事情,这件事情你需要花些时间去做,这时候总经理肯定不能守着你做完再出差吧,于是就他告诉你他的手机号 ...

  2. iOS8 UIAlertController弹出框中添加视图(例如日期选择器等等)

    UIDatePicker *datePicker = [[UIDatePicker alloc] init]; datePicker.datePickerMode = UIDatePickerMode ...

  3. 读取XML文档结构并写入内容

    1.在项目中新建XML文档结构.xsd文件,在其中添加相应的节点. 2.读取文档结构并写入内容 string initFileName = @"D:\Config.xml"; Da ...

  4. 获取微信服务器IP地址

    关键字:微信公众平台 微信服务器 IP地址 如果公众号基于安全等考虑,需要获知微信服务器的IP地址列表,以便进行相关限制,可以通过该接口获得微信服务器IP地址列表. 接口调用请求说明 http请求方式 ...

  5. ChatForFun 公众号使用说明

    使用方法 2016-07-16 DennisMi ChatForFun 1,发送  #1 实现登陆,或者退出登陆 2,发送  #2 实现加入聊天,和退出聊天 3,聊天开始后,可以直接发送消息 4,如果 ...

  6. Vue.2.0.5-事件处理器

    监听事件 可以用 v-on 指令监听 DOM 事件来触发一些 JavaScript 代码. 示例: <div id="example-1"> <button v- ...

  7. C#线程系列讲座(4):同步与死锁

    虽然线程可以在一定程度上提高程序运行的效率,但也会产生一些副作用.让我们先看看如下的代码:     class Increment     {         private int n = 0;   ...

  8. ios-NSStringFromCGRect直接输出控件的frame

  9. C# 生成二维码,彩色二维码,带有Logo的二维码及普通条形码

    每次写博客,第一句话都是这样的:程序员很苦逼,除了会写程序,还得会写博客!当然,希望将来的一天,某位老板看到此博客,给你的程序员职工加点薪资吧!因为程序员的世界除了苦逼就是沉默.我眼中的程序员大多都不 ...

  10. hibernate反向生成映射文件报错

    报错原因:可能是你的数据库里的某个表没设置主键.