Skip to content

Instantly share code, notes, and snippets.

@clairvy
Created July 25, 2010 03:07
Show Gist options
  • Select an option

  • Save clairvy/489255 to your computer and use it in GitHub Desktop.

Select an option

Save clairvy/489255 to your computer and use it in GitHub Desktop.
HADOOP_HOME = $(HOME)/modules/hadoop-$(HADOOP_VERSION)
JAVAC = javac -J-Dfile.encoding=UTF8 -Xlint:unchecked -Xlint:deprecation
JAVA = java -Dfile.encoding=UTF8
HADOOP_VERSION = 0.20.2
DST = build
SRC = src
MKDIR = mkdir
WGET = wget
#CLASSPATH = $(HADOOP_HOME)/hadoop-$(HADOOP_VERSION)-core.jar:$(HADOOP_HOME)/hadoop-$(HADOOP_VERSION)-test.jar:$(HADOOP_HOME)/lib/mockito-all-1.8.0.jar
CLASSPATH = $(HADOOP_HOME)/hadoop-$(HADOOP_VERSION)-core.jar:$(HADOOP_HOME)/lib/mockito-all-1.8.0.jar:`pwd`/lib/junit-4.8.2.jar
CLASSPATH_TEST = $(CLASSPATH):$(HADOOP_HOME)/lib/commons-logging-1.0.4.jar:`pwd`/build
default : build
setup :
if [ ! -d build ]; then $(MKDIR) build; fi
if [ ! -d lib ]; then $(MKDIR) lib; fi
if [ ! -e lib/junit-4.8.2.jar ]; then $(WGET) -P lib http://github.com/downloads/KentBeck/junit/junit-4.8.2.jar; fi
test : build
$(JAVA) -classpath $(CLASSPATH_TEST) org.junit.runner.JUnitCore MaxTemperatureMapperTest
.PHONY : build
build : setup $(DST)/MaxTemperatureMapperTest.class
$(DST)/MaxTemperatureMapperTest.class : $(SRC)/MaxTemperatureMapperTest.java
$(JAVAC) -classpath $(CLASSPATH) -d $(DST) $(SRC)/MaxTemperatureMapperTest.java $(SRC)/MaxTemperatureMapper.java
clean :
$(RM) $(RMF) $(DST)/*
import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.MapReduceBase;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Reporter;
import org.apache.hadoop.mapred.Mapper;
public class MaxTemperatureMapper extends MapReduceBase
implements Mapper<LongWritable, Text, Text, IntWritable> {
public void map(LongWritable key, Text value,
OutputCollector<Text, IntWritable> output, Reporter reporter)
throws IOException {
String line = value.toString();
String year = line.substring(15, 19);
int airTemperature = Integer.parseInt(line.substring(87, 92));
output.collect(new Text(year), new IntWritable(airTemperature));
}
}
import static org.mockito.Matchers.anyObject;
import static org.mockito.Mockito.*;
import java.io.IOException;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapred.OutputCollector;
import org.junit.*;
public class MaxTemperatureMapperTest {
@Test
public void processesValidRecord() throws IOException {
MaxTemperatureMapper mapper = new MaxTemperatureMapper();
Text value = new Text("0043011990999991950051518004+68750+023550FM-12+0382" +
"99999V0203201N00261220001CN9999999N9-00111+99999999999");
OutputCollector<Text, IntWritable> output = mock(OutputCollector.class);
mapper.map(null, value, output, null);
verify(output).collect(new Text("1950"), new IntWritable(-11));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment