com.avira.couchdoop.demo.ExportMapper.java Source code

Java tutorial

Introduction

Here is the source code for com.avira.couchdoop.demo.ExportMapper.java

Source

/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package com.avira.couchdoop.demo;

import com.avira.couchdoop.exp.CouchbaseAction;
import com.avira.couchdoop.exp.CouchbaseOperation;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

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

/**
 * Mapper which reads a delimited text file with recommendations computed by a
 * recommending system and reformats them to be published in Couchbase as JSON
 * documents.
 */
public class ExportMapper extends Mapper<LongWritable, Text, String, CouchbaseAction> {

    private static final String KEY_PREFIX = "rec::";
    private static final String DELIMITER = "\t";
    private static final String SECONDARY_DELIMITER = ";";

    private static final ObjectMapper JACKSON = new ObjectMapper();

    @Override
    /**
     * Recommended articles are in the format: session_id   article_name1;score1    article_name2;score2    ....
     */
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String[] tokens = value.toString().split(DELIMITER);

        String documentKey = tokens[0];

        Recommendation rec = new Recommendation();
        for (int i = 1; i < tokens.length; i++) {
            String[] recData = tokens[i].split(SECONDARY_DELIMITER);
            rec.addArticle(new RecommendedItem(recData[0], Float.parseFloat(recData[1])));
        }

        CouchbaseAction action = new CouchbaseAction(CouchbaseOperation.SET, JACKSON.writeValueAsString(rec));

        context.write(KEY_PREFIX + documentKey, action);
    }

}