参考书目:hadoop real world solutions cookbook
源码的下载地址:http://www.packtpub.com/code_download/11101 ,可以下载到所有章节的代码。这里演示的是第二章 chapter2。
Captuer2 提供了两种方式来操作HDFS的API,分别是Java和C语言。因为Hadoop本身就是用java写的,因此java操作更容易些。
这里先介绍下java调用Hadoop的方式。
首先我们编译和运行chapter2中的例子代码,看到里面有build.xml可以知道是用ant进行构建的,因此先准备好ant环境。
1)首先来编译和运行下java代码试试
进入源码目录
[gaotong@localhost HDFS_Java_API]$ pwd /home/gaotong/7287OS_Code/chapter2/HDFS_Java_API
Configuration conf = new Configuration(); conf.addResource(new Path( "/u/hadoop-1.0.2/conf/core-site.xml")); conf.addResource(new Path( "/u/hadoop-1.0.2/conf/hdfs-site.xml")); FileSystem fileSystem = FileSystem.get(conf); System.out.println(fileSystem.getUri()); Path file = new Path("demo.txt"); if (fileSystem.exists(file)) { System.out.println("File exists."); } else { // Writing to file FSDataOutputStream outStream = fileSystem.create(file); outStream.writeUTF("Welcome to HDFS Java API!!!"); outStream.close(); } // Reading from file FSDataInputStream inStream = fileSystem.open(file); String data = inStream.readUTF(); System.out.println(data); inStream.close(); // deleting the file. Non-recursively. // fileSystem.delete(file, false); fileSystem.close();
直接运行ant 命令即可
[gaotong@localhost HDFS_Java_API]$ ~/Downloads/apache-ant-1.9.4/bin/ant Buildfile: /home/gaotong/7287OS_Code/chapter2/HDFS_Java_API/build.xml compile: [mkdir] Created dir: /home/gaotong/7287OS_Code/chapter2/HDFS_Java_API/build [javac] Compiling 1 source file to /home/gaotong/7287OS_Code/chapter2/HDFS_Java_API/build [jar] Building jar: /home/gaotong/7287OS_Code/chapter2/HDFS_Java_API/HDFSJavaAPI.jar BUILD SUCCESSFUL Total time: 3 seconds
构建成功后,可以看到打好的jar包, 直接运行即可。
[gaotong@localhost HDFS_Java_API]$ ~/hadoop-1.2.1/bin/hadoop jar HDFSJavaAPI.jar HDFSJavaAPIDemo hdfs://192.168.101.128:9000 Welcome to HDFS Java API!!!
[gaotong@localhost HDFS_Java_API]$ ~/hadoop-1.2.1/bin/hadoop fs -ls /user/gaotong/ Found 1 items -rw-r--r-- 2 gaotong supergroup 29 2014-07-03 22:19 /user/gaotong/demo.txt