• mapreduce 读写Parquet格式数据 Demo


    import java.io.IOException;
    
    import org.apache.hadoop.conf.Configuration;
    import org.apache.hadoop.fs.Path;
    import org.apache.hadoop.io.LongWritable;
    import org.apache.hadoop.io.Text;
    import org.apache.hadoop.mapreduce.Job;
    import org.apache.hadoop.mapreduce.Mapper;
    import org.apache.hadoop.mapreduce.Reducer;
    import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
    import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
    import org.apache.hadoop.util.GenericOptionsParser;
    import org.apache.parquet.example.data.Group;
    import org.apache.parquet.example.data.simple.SimpleGroupFactory;
    import org.apache.parquet.hadoop.ParquetInputFormat;
    import org.apache.parquet.hadoop.ParquetOutputFormat;
    import org.apache.parquet.hadoop.example.GroupReadSupport;
    import org.apache.parquet.hadoop.example.GroupWriteSupport;
    import org.apache.parquet.schema.MessageType;
    import org.apache.parquet.schema.OriginalType;
    import org.apache.parquet.schema.PrimitiveType.PrimitiveTypeName;
    import org.apache.parquet.schema.Types;
    
    /**
     * MR Parquet格式数据读写Demo
     */
    public class ParquetReaderAndWriteMRDemo {
    
        public static void main(String[] args) throws Exception {
            Configuration conf = new Configuration();
            String[] otherargs=new GenericOptionsParser(conf, args).getRemainingArgs();
            if(otherargs.length!=3){
                System.out.println("<in> <out> 1");
                System.out.println("<parquet-in> <out> 2");
                System.out.println("<in> <parquet-out> 3");
                System.out.println("<parquet-in> <parquet-out> 4");
                System.exit(2);
            }
            //此demo 输入数据为2列     city  ip
            
            MessageType schema = Types.buildMessage() 
                       .required(PrimitiveTypeName.BINARY).as(OriginalType.UTF8).named("city") 
                       .required(PrimitiveTypeName.BINARY).as(OriginalType.UTF8).named("ip") 
                       .named("pair");
            System.out.println("[schema]=="+schema.toString());
            GroupWriteSupport.setSchema(schema, conf);
            
            Job job = Job.getInstance(conf, "ParquetReadMR");
            job.setJarByClass(ParquetReaderAndWriteMRDemo.class);
            
            if(otherargs[2].equals("1")){
                job.setMapperClass(NormalMapper.class);
                job.setReducerClass(NormalReducer.class);
                job.setOutputKeyClass(Text.class);
                job.setOutputValueClass(Text.class);
                FileInputFormat.setInputPaths(job,otherargs[0] );
                FileOutputFormat.setOutputPath(job, new Path(otherargs[1]));
                if (!job.waitForCompletion(true))
                    return;
            }
            if(otherargs[2].equals("3")){
                job.setMapperClass(ParquetWriteMapper.class);
                job.setNumReduceTasks(0);
                FileInputFormat.setInputPaths(job,otherargs[0] );
                
                //parquet输出
                job.setOutputFormatClass(ParquetOutputFormat.class);
                ParquetOutputFormat.setWriteSupportClass(job, GroupWriteSupport.class);
    //            ParquetOutputFormat.setOutputPath(job, new Path(otherargs[1]));
                FileOutputFormat.setOutputPath(job, new Path(otherargs[1]));
                if (!job.waitForCompletion(true))
                    return;
            }
            
            if(otherargs[2].equals("2")){
                //parquet输入
                job.setMapperClass(ParquetReadMapper.class);
                job.setNumReduceTasks(0);
                job.setInputFormatClass(ParquetInputFormat.class);
                ParquetInputFormat.setReadSupportClass(job, GroupReadSupport.class);
                
                job.setOutputKeyClass(Text.class);
                job.setOutputValueClass(Text.class);
                FileInputFormat.setInputPaths(job,otherargs[0] );
                FileOutputFormat.setOutputPath(job, new Path(otherargs[1]));
                if (!job.waitForCompletion(true))
                    return;
            }
            if(otherargs[2].equals("4")){
                //TODO 不想写了
            }
        }
        
        public static class ParquetWriteMapper extends Mapper<LongWritable, Text, Void, Group> {
            SimpleGroupFactory factory=null;
            protected void setup(Context context) throws IOException ,InterruptedException {
                factory = new SimpleGroupFactory(GroupWriteSupport.getSchema(context.getConfiguration()));
            };
            
            public void map(LongWritable _key, Text ivalue, Context context) throws IOException, InterruptedException {
                Group pair=factory.newGroup();
                String[] strs=ivalue.toString().split("\s+");
                pair.append("city", strs[0]);
                pair.append("ip", strs[1]);
                context.write(null,pair);
            }
        }
        
        public static class ParquetReadMapper extends Mapper<Void, Group, Text, Text> {
            public void map(Void _key, Group group, Context context) throws IOException, InterruptedException {
                String city=group.getString(0, 0);
                String ip=group.getString(1, 0);
                context.write(new Text(city),new Text(ip));
            }
        }
        
        public static class NormalMapper extends Mapper<LongWritable, Text, Text, Text> {
    
            public void map(LongWritable ikey, Text ivalue, Context context) throws IOException, InterruptedException {
                String[] strs=ivalue.toString().split("\s+");
                context.write(new Text(strs[0]), new Text(strs[1]));
            }
        }
            public static class NormalReducer extends Reducer<Text, Text, Text, Text> {
    
                public void reduce(Text _key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
                    for (Text text : values) {
                        context.write(_key,text);
                    }
                    
                }
            }
    
    }
  • 相关阅读:
    gradle
    1-NIO使用
    处理非正常终止的错误
    一个取消多生产者单消费者的日志线程池服务
    executes()源码
    死锁
    CyclicBarrier使用
    Semaphore
    Spring学习(4)IOC容器配置bean:定义与实例化
    在Maven上Web项目添加Spring框架
  • 原文地址:https://www.cnblogs.com/yanghaolie/p/7389543.html
Copyright © 2020-2023  润新知