分享

Hadoop培训笔记之HDFS编程

nettman 2014-2-13 21:35:01 发表于 代码分析 [显示全部楼层] 回帖奖励 阅读模式 关闭右栏 4 20430
本帖最后由 pig2 于 2014-2-27 22:18 编辑

上一篇:

hadoop培训笔记之HDFS介绍--HDFS优点与缺点


可以带着下面问题来阅读本文:
1.面对shell命令,你是否能够想出对应的Java代码?如创建一个文件该怎么写,需要几步,都用到什么类?

2.能否写出Client端与Namenode之间的RPC通信协议?

3.什么功能使得Hadoop集群能够感知机架-物理主机-Hadoop节点三层架构?

4.DistributedFileSystem调用create方法后的返回类型是什么?

5.Java中对文件读取速度最快的数据结构是什么?

6.默认的Namenode web管理端口是多少?(很简单,可是不一定能说对)



HDFS API使用http://hadoop.apache.org/docs/current1/api/ (Hadoop 1.2.1)

主要用代码讲解了其中一些API的使用。以下是JUnit测试代。

效果可以用如下(类似)命令查看:

./hadoop fs -lsr /
./hadoop fs -cat /test/a.txt

  1. import java.io.BufferedInputStream;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.net.URI;
  7. import org.apache.hadoop.conf.Configuration;
  8. import org.apache.hadoop.fs.BlockLocation;
  9. import org.apache.hadoop.fs.FSDataOutputStream;
  10. import org.apache.hadoop.fs.FileStatus;
  11. import org.apache.hadoop.fs.FileSystem;
  12. import org.apache.hadoop.fs.Path;
  13. import org.apache.hadoop.io.IOUtils;
  14. import org.apache.hadoop.util.Progressable;
  15. import org.junit.Test;
  16. import junit.framework.TestCase;
  17. public class TestHDFS extends TestCase {
  18.         
  19.         public static String hdfsUrl = "hdfs://192.168.56.101:9100";
  20.         //create HDFS folder
  21.         @Test
  22.         public void testHDFSMkdir() throws IOException{
  23.                 Configuration conf = new Configuration();
  24.                 FileSystem fs = FileSystem.get(URI.create(hdfsUrl), conf);
  25.                 Path path = new Path("/test");
  26.                 fs.mkdirs(path);
  27.         }
  28.         
  29.         //create a file
  30.         @Test
  31.         public void testCreateFile() throws IOException{
  32.                 Configuration conf = new Configuration();
  33.                 FileSystem fs = FileSystem.get(URI.create(hdfsUrl), conf);
  34.                 Path path = new Path("/test/a.txt");
  35.                 FSDataOutputStream out = fs.create(path);
  36.                 out.write("hello hadoop".getBytes());
  37.         }
  38.         
  39.         //rename a file
  40.         @Test
  41.         public void testRenameFile() throws IOException{
  42.                 Configuration conf = new Configuration();
  43.                 FileSystem fs = FileSystem.get(URI.create(hdfsUrl), conf);
  44.                 Path path = new Path("/test/a.txt");
  45.                 Path newpath = new Path("/test/b.txt");
  46.                 System.out.println(fs.rename(path, newpath));
  47.         }
  48.         
  49.         //upload a local file to HDFS
  50.         @Test
  51.         public void testUploadFile1() throws IOException{
  52.                 Configuration conf = new Configuration();
  53.                 FileSystem fs = FileSystem.get(URI.create(hdfsUrl), conf);
  54.                 Path src = new Path("/home/xwchen/hadoop/hadoop-1.2.1/bin/rcc");
  55.                 Path dst = new Path("/test");
  56.                 fs.copyFromLocalFile(src, dst);
  57.         }
  58.         
  59.         @Test
  60.         public void testUploadFile2() throws IOException{
  61.                 Configuration conf = new Configuration();
  62.                 FileSystem fs = FileSystem.get(URI.create(hdfsUrl), conf);
  63.                 InputStream in = new BufferedInputStream(new FileInputStream(new File("/home/xwchen/hadoop/hadoop-1.2.1/bin/rcc")));
  64.                 FSDataOutputStream out = fs.create(new Path("/test/rcc1"));
  65.                 IOUtils.copyBytes(in, out, 4096);
  66.         }
  67.         
  68.         @Test
  69.         public void testUploadFile3() throws IOException{
  70.                 Configuration conf = new Configuration();
  71.                 FileSystem fs = FileSystem.get(URI.create(hdfsUrl), conf);
  72.                 InputStream in = new BufferedInputStream(new FileInputStream(new File("/home/xwchen/hadoop/hadoop-1.2.1/bin/rcc")));
  73.                 FSDataOutputStream out = fs.create(new Path("/test/rcc2"), new Progressable(){
  74.                         @Override
  75.                         public void progress() {
  76.                                 System.out.println(".");
  77.                                 
  78.                         }});
  79.                 IOUtils.copyBytes(in, out, 4096);
  80.         }
  81.         
  82.         
  83.         @Test //dd if=/dev/zero of=data bs=1024 count=1024
  84.         public void testUploadFile4() throws IOException{
  85.                 Configuration conf = new Configuration();
  86.                 FileSystem fs = FileSystem.get(URI.create(hdfsUrl), conf);
  87.                 InputStream in = new BufferedInputStream(new FileInputStream(new File("/home/xwchen/hadoop/hadoop-1.2.1/bin/data")));
  88.                 FSDataOutputStream out = fs.create(new Path("/test/data"), new Progressable(){
  89.                         @Override
  90.                         public void progress() {
  91.                                 System.out.println(".");
  92.                                 
  93.                         }});
  94.                 IOUtils.copyBytes(in, out, 4096);
  95.         }
  96.         
  97.         //list files under folder
  98.         @Test
  99.         public void testListFiles() throws IOException{
  100.                 Configuration conf = new Configuration();
  101.                 FileSystem fs = FileSystem.get(URI.create(hdfsUrl), conf);
  102.                 Path dst = new Path("/test");
  103.                 FileStatus[] files = fs.listStatus(dst);
  104.                 for(FileStatus file: files){
  105.                         System.out.println(file.getPath().toString());
  106.                 }
  107.         }
  108.         
  109.         //list block info of file
  110.         @Test
  111.         public void testGetBlockInfo() throws IOException{
  112.                 Configuration conf = new Configuration();
  113.                 FileSystem fs = FileSystem.get(URI.create(hdfsUrl), conf);
  114.                 Path dst = new Path("/test/data");
  115.                 FileStatus fileStatus = fs.getFileStatus(dst);
  116.                 BlockLocation[] blkLoc = fs.getFileBlockLocations(fileStatus, 0, fileStatus.getLen());
  117.                 for(BlockLocation loc: blkLoc){
  118.                         //System.out.println(loc.getHosts());
  119.                         for (int i=0; i<loc.getHosts().length; i++)
  120.                                 System.out.println(loc.getHosts()[i]);
  121.                 }
  122.         }
  123. }
  124. </font></font>
复制代码
测验相关:


FileSystem类是一个抽象类。

Client端与Namenode之间的RPC通信协议是ClientProtocol。

传统的Hadoop集群节点拓扑结构包括机架和主机层,但虚拟化在此基础上还需要知道hypervisor这一层。HVE(Hadoop Virtualization Extensions)功能使得Hadoop集群能够感知机架-物理主机-Hadoop节点三层架构,并且根据相应算法使运行于同一台物理主机上的存储节点和计算节点之间的通信方式满足数据本地化的要求。

DistributedFileSystem调用create方法后的返回类型是FSDataOutputStream
Java中对文件读取速度最快的数据结构是FileChannel,其次是BufferInputStream,FileInputStrea,andomAccessFile(http://bbs.itheima.com/thread-48379-1-1.html

FSDataOutputStream实现的接口:Closeable, DataOutput, Flushable, CanSetDropBehind, Syncable

文件系统如ZFS,Moose、Glusterfs和Lustre使用FUSE实现,FastDFS没有提供FUSE功能

用户空间文件系统(Filesystem in Userspace,简称FUSE)是操作系统中的概念,指完全在用户态实现的文件系统。(http://zh.wikipedia.org/wiki/FUSE

默认的Namenode web管理端口是50070

DirectByteBuffer和ByteBuffer:ByteBuffer需要通过wrap方法来封装字节数组,ByteBuffer在heap上分配内存,DirectByteBuffer的字节访问速度比ByteBuffer快,ByteBuffer由JVM负责垃圾回收(Direct不是)



加微信w3aboutyun,可拉入技术爱好者群

已有(4)人评论

跳转到指定楼层
ssbpls 发表于 2014-2-14 08:43:59
学习了,谢谢楼主。
回复

使用道具 举报

tang 发表于 2015-3-7 20:23:01
回复

使用道具 举报

hello1988 发表于 2015-11-25 13:57:20
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条