Hadoop FSDataInputStream 流定位的例子

2019-03-28 13:46|来源: 网络

Hadoop FSDataInputStream 流定位的例子,不单独说明了,代码中的注释很详细了 

  1. /** 
  2.  * 
  3.  * Description: 这个例子用于展示Hadoop的FSDataInputStream的流定位能力 
  4.  * 
  5.  * @author charles.wang 
  6.  * @created Mar 13, 2012 9:21:34 AM 
  7.  *  
  8.  */ 
  9. public class FileSystemCatSeekable { 
  10.      
  11.     public static void main(String [] args) throws Exception{ 
  12.          
  13.         //获取命令行参数 
  14.         String uri = args[0]; 
  15.          
  16.         Configuration conf = new Configuration(); 
  17.         conf.set("hadoop.job.ugi""root,root123"); 
  18.          
  19.          
  20.         //打开一个Hadoop FileSystem ,用FileSystem的静态方法获取之 
  21.         FileSystem fs = FileSystem.get(URI.create(uri) ,conf); 
  22.          
  23.         //打开一个InputStream 对象 
  24.         FSDataInputStream in = null
  25.          
  26.         try
  27.          
  28.         //让其指向FileSystem中由命令行提供的uri对应的路径 
  29.         in =fs.open(new Path (uri)); 
  30.          
  31.         //第一次读取 
  32.         IOUtils.copyBytes(in, System.out, 20false); 
  33.          
  34.         //让读头重新定位到文件起始地方 
  35.         in.seek(0); 
  36.          
  37.         //第二次读取 
  38.         IOUtils.copyBytes(in, System.out, 20false); 
  39.          
  40.         }catch (Exception ex){ 
  41.             ex.printStackTrace(); 
  42.         }finally
  43.             in.close(); 
  44.         } 
  45.     } 

更多Hadoop相关信息见Hadoop 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=13

相关问答

更多