/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.telefonica.iot.bigdata.hadoop.mr.mappers;

import com.telefonica.iot.bigdata.utils.Utils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

/**
 *
 * @author frb
 */
public class KeyGenerator extends Mapper<Object, Text, Text, Text> {
    
    private ArrayList<String> keyFields;
        
    @Override
    public void setup(Mapper.Context context) throws IOException, InterruptedException {
        String keyFieldsStr = context.getConfiguration().get("KEY_FIELDS", "");
        keyFields = new ArrayList<>(Arrays.asList(keyFieldsStr.split("&")));
    } // setup

    @Override
    public void map(Object key, Text value, Mapper.Context context) throws IOException, InterruptedException {
        JSONParser parser = new JSONParser();
        JSONObject jsonObject;

        try {
            jsonObject = (JSONObject) parser.parse(value.toString());
        } catch (ParseException e) {
            throw new InterruptedException(e.getMessage());
        } // try catch

        String k = "";
        Iterator it = jsonObject.keySet().iterator();

        while (it.hasNext()) {
            String field = (String) it.next();
            Object val = jsonObject.get(field);

            if (keyFields.contains(field)) {
                k += Utils.serializeJsonValue(val);
            } // if
        } // while

        context.write(new Text(k), value);
    } // map
    
} // KeyGenerator

@@ -1,184 +1,906 @@
 /*
+*
 %0A * 
-To change this license header, choose License Headers in Project Properties.%0A * To change this template file, choose Tools %7C Templates%0A * and open the template in the editor.
+Copyright 2017 Telefonica Investigaci%C3%B3n y Desarrollo, S.A.U%0A *%0A * This file is part of BigDataResources.%0A *%0A * BigDataResources is free software: you can redistribute it and/or modify it under the terms of the GNU Affero%0A * General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your%0A * option) any later version.%0A * BigDataResources is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the%0A * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License%0A * for more details.%0A *%0A * You should have received a copy of the GNU Affero General Public License along with BigDataResources. If not, see%0A * http://www.gnu.org/licenses/.%0A *%0A * For those usages not covered by the GNU Affero General Public License please contact with iot_support at tid dot es
 %0A */

Add license header to KeyGenerator.java