Book Image

Elasticsearch for Hadoop

By : Vishal Shukla
Book Image

Elasticsearch for Hadoop

By: Vishal Shukla

Overview of this book

Table of Contents (15 chapters)
Elasticsearch for Hadoop
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Writing the NetworkLogsMapper job


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.

Writing the mapper class

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...