Now that we know the approach to getting answers to our problems elegantly, let's go ahead and write the mapper that we discussed in the previous section.
Here is the relevant part of the NetworkLogsMapper
class to map our network logs to Elasticsearch.
Let's take a look at the mapper class now:
public class NetworkLogsMapper extends Mapper<Object, Text, Text, MapWritable> { public void map(Object key, Text value, Context context) throws IOException, InterruptedException { MapWritable map = new MapWritable(); String line = value.toString().trim(); String[] parts = line.split(" \\("); String keyVals = parts[0].substring(15, parts[0].length()).trim();
The preceding code reads the input log line and divides it into two easily parsable segments that contain the key/value pairs, parts
and keyVals
:
int i = 0; StringTokenizer part1tokenizer = new StringTokenizer(keyVals); while...