zoukankan      html  css  js  c++  java
  • Java读写hdfs上的avro文件

    import java.io.IOException;
     import java.io.InputStream;
     
     import org.apache.avro.file.DataFileStream;
     import org.apache.avro.generic.GenericDatumReader;
     import org.apache.avro.generic.GenericRecord;
     import org.apache.hadoop.conf.Configuration;
     import org.apache.hadoop.fs.FileSystem;
     import org.apache.hadoop.fs.Path;
     import org.apache.hadoop.io.IOUtils;
     
     public class HdfsReadAvro {
     
         public static void readFromAvro(InputStream is) throws IOException {
             DataFileStream<Object> reader = new DataFileStream<Object>(is,
                     new GenericDatumReader<Object>());
             for (Object o : reader) {
                 GenericRecord r = (GenericRecord) o;
                 System.out.println(r.get("username")+ ":"+r.get("password"));
             }
             IOUtils.cleanup(null, is);
             IOUtils.cleanup(null, reader);
         }
     
         public static void main(String[] args) throws Exception {
             Configuration config = new Configuration();
             FileSystem hdfs = FileSystem.get(config);
             Path destFile = new Path(args[0]);
             InputStream is = hdfs.open(destFile);
             readFromAvro(is);
         }
     }
  • 相关阅读:
    visual sudio开发工具使用小技巧
    JS去除右边的逗号
    下拉标题
    sp_addextendedproperty
    触发器的使用
    缺失一个正数
    组合总和 去重
    拖动 Drag
    n皇后问题
    括号生成
  • 原文地址:https://www.cnblogs.com/caiwuzi/p/12842800.html
Copyright © 2011-2022 走看看