summaryrefslogtreecommitdiffstats
path: root/vespa-hadoop/src/test/java/com/yahoo/vespa/hadoop/pig/MapReduceTest.java
blob: d56cd818de23951398f9f72bd995d4c69fb3760a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hadoop.pig;

import com.fasterxml.jackson.core.JsonEncoding;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.yahoo.vespa.hadoop.mapreduce.VespaOutputFormat;
import com.yahoo.vespa.hadoop.mapreduce.util.VespaConfiguration;
import com.yahoo.vespa.hadoop.mapreduce.util.VespaCounters;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.LocalFileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hdfs.HdfsConfiguration;
import org.apache.hadoop.hdfs.MiniDFSCluster;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.test.PathUtils;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.StringTokenizer;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class MapReduceTest {

    protected static File hdfsBaseDir;
    protected static FileSystem hdfs;
    protected static Configuration conf;
    protected static MiniDFSCluster cluster;

    protected static Path metricsJsonPath;
    protected static Path metricsCsvPath;

    @BeforeAll
    public static void setUp() throws IOException {
        hdfsBaseDir = new File(PathUtils.getTestDir(MapReduceTest.class).getCanonicalPath());

        conf = new HdfsConfiguration();
        conf.set(MiniDFSCluster.HDFS_MINIDFS_BASEDIR, hdfsBaseDir.getAbsolutePath());
        conf.set(VespaConfiguration.DRYRUN, "true");
        conf.set(VespaConfiguration.ENDPOINT, "endpoint-does-not-matter-in-dryrun");

        cluster = new MiniDFSCluster.Builder(conf).build();
        hdfs = FileSystem.get(conf);

        metricsJsonPath = new Path("metrics_json");
        metricsCsvPath = new Path("metrics_csv");
        copyToHdfs("src/test/resources/operations_data.json", metricsJsonPath, "data");
        copyToHdfs("src/test/resources/tabular_data.csv", metricsCsvPath, "data");
    }

    @AfterAll
    public static void tearDown() throws IOException {
        Path testDir = new Path(hdfsBaseDir.getParent());
        hdfs.delete(testDir, true);
        cluster.shutdown();
        LocalFileSystem localFileSystem = FileSystem.getLocal(conf);
        localFileSystem.delete(testDir, true);
    }

    @Test
    public void requireThatMapOnlyJobSucceeds() throws Exception {
        Job job = Job.getInstance(conf);
        job.setJarByClass(MapReduceTest.class);
        job.setMapperClass(FeedMapper.class);
        job.setOutputFormatClass(VespaOutputFormat.class);
        job.setMapOutputValueClass(Text.class);

        FileInputFormat.setInputPaths(job, metricsJsonPath);

        boolean success = job.waitForCompletion(true);
        assertTrue(success, "Job Failed");

        VespaCounters counters = VespaCounters.get(job);
        assertEquals(10, counters.getDocumentsSent());
        assertEquals(0, counters.getDocumentsFailed());
        assertEquals(10, counters.getDocumentsOk());
    }

    @Test
    public void requireThatMapReduceJobSucceeds() throws Exception {
        Job job = Job.getInstance(conf);
        job.setJarByClass(MapReduceTest.class);
        job.setMapperClass(FeedMapper.class);
        job.setOutputFormatClass(VespaOutputFormat.class);
        job.setMapOutputValueClass(Text.class);
        job.setReducerClass(FeedReducer.class);
        job.setNumReduceTasks(1);

        FileInputFormat.setInputPaths(job, metricsJsonPath);

        boolean success = job.waitForCompletion(true);
        assertTrue(success, "Job Failed");

        VespaCounters counters = VespaCounters.get(job);
        assertEquals(10, counters.getDocumentsSent());
        assertEquals(0, counters.getDocumentsFailed());
        assertEquals(10, counters.getDocumentsOk());
    }


    @Test
    public void requireThatTransformMapJobSucceeds() throws Exception {
        Job job = Job.getInstance(conf);
        job.setJarByClass(MapReduceTest.class);
        job.setMapperClass(ParsingMapper.class);
        job.setOutputFormatClass(VespaOutputFormat.class);
        job.setMapOutputValueClass(Text.class);
        job.setReducerClass(FeedReducer.class);
        job.setNumReduceTasks(1);

        FileInputFormat.setInputPaths(job, metricsCsvPath);

        boolean success = job.waitForCompletion(true);
        assertTrue(success, "Job Failed");

        VespaCounters counters = VespaCounters.get(job);
        assertEquals(10, counters.getDocumentsSent());
        assertEquals(0, counters.getDocumentsFailed());
        assertEquals(10, counters.getDocumentsOk());
        assertEquals(0, counters.getDocumentsSkipped());
    }


    private static void copyToHdfs(String localFile, Path hdfsDir, String hdfsName) throws IOException {
        Path hdfsPath = new Path(hdfsDir, hdfsName);
        FSDataOutputStream out = hdfs.create(hdfsPath);

        try (InputStream in = new BufferedInputStream(new FileInputStream(localFile))) {
            int len;
            byte[] buffer = new byte[1024];
            while ((len = in.read(buffer)) > 0) {
                out.write(buffer, 0, len);
            }
        } finally {
            out.close();
        }
    }

    public static class FeedMapper extends Mapper<LongWritable, Text, LongWritable, Text> {
        public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            context.write(key, value);
        }
    }

    public static class FeedReducer extends Reducer<Object, Text, LongWritable, Text> {
        public void reduce(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            context.write(key, value);
        }
    }

    public static class ParsingMapper extends Mapper<LongWritable, Text, LongWritable, Text> {
        public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String line = value.toString();
            if (line == null || line.length() == 0)
                return;

            StringTokenizer tokenizer = new StringTokenizer(line);
            long date = Long.parseLong(tokenizer.nextToken());
            String metricName = tokenizer.nextToken();
            long metricValue = Long.parseLong(tokenizer.nextToken());
            String application = tokenizer.nextToken();

            String docid = "id:"+application+":metric::"+metricName+"-"+date;

            ByteArrayOutputStream out = new ByteArrayOutputStream();
            JsonGenerator g = new JsonFactory().createGenerator(out, JsonEncoding.UTF8);

            g.writeStartObject();
            g.writeObjectFieldStart("fields");
            g.writeNumberField("date", date);
            g.writeStringField("name", metricName);
            g.writeNumberField("value", metricValue);
            g.writeStringField("application", application);
            g.writeEndObject();
            g.writeStringField("put", docid);
            g.writeEndObject();
            g.close();

            context.write(key, new Text(out.toString()));
        }
    }


}