Package com.mongodb.hadoop.bookstore

Source Code of com.mongodb.hadoop.bookstore.TagsReducer

package com.mongodb.hadoop.bookstore;

import com.mongodb.BasicDBObject;
import com.mongodb.hadoop.io.BSONWritable;
import com.mongodb.hadoop.io.MongoUpdateWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reporter;
import org.apache.hadoop.mapreduce.Reducer;
import org.bson.BSONObject;
import org.bson.BasicBSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

public class TagsReducer extends Reducer<Text, BSONWritable, NullWritable, MongoUpdateWritable>
    implements org.apache.hadoop.mapred.Reducer<Text, BSONWritable, NullWritable, MongoUpdateWritable> {
    @Override
    protected void reduce(final Text key, final Iterable<BSONWritable> values, final Context context)
        throws IOException, InterruptedException {

        BasicDBObject query = new BasicDBObject("_id", key.toString());
        ArrayList<BSONObject> books = new ArrayList<BSONObject>();
        for (BSONWritable val : values) {
            books.add(val.getDoc());
        }

        BasicBSONObject update = new BasicBSONObject("$set", new BasicBSONObject("books", books));
        context.write(null, new MongoUpdateWritable(query, update, true, false));
    }

    @Override
    public void reduce(final Text key, final Iterator<BSONWritable> values, final OutputCollector<NullWritable, MongoUpdateWritable> output,
                       final Reporter reporter) throws IOException {
        BasicDBObject query = new BasicDBObject("_id", key.toString());
        ArrayList<BSONObject> books = new ArrayList<BSONObject>();
        while (values.hasNext()) {
            books.add(values.next().getDoc());
        }

        BasicBSONObject update = new BasicBSONObject("$set", new BasicBSONObject("books", books));
        output.collect(null, new MongoUpdateWritable(query, update, true, false));
    }

    @Override
    public void close() throws IOException {
    }

    @Override
    public void configure(final JobConf job) {
    }
}
TOP

Related Classes of com.mongodb.hadoop.bookstore.TagsReducer

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.