Introduction to Apache Gora MR in nutch 2.0
-----------------
1. Introduction
Apapche Gora has built-in support for Apache hadoop, And Gora's datastore can be used as the input and output of inputformat and outputformat. However, all these output objects are serialized, gora extends Avro's datumwriters.
2. A simple example
The following is a simple example in Gora-tutorial to illustrate the usage of Gora mr. This example uses the output of the previous example as its input and uses Mr to analyze log information, used to count the visits of a single URL in a day, and then write the analysis results back to an example in hbase. Of course, you can also write the output back to SQL or even the database, this example can be found in T Gora-tutorial/src/main/Java/org/Apache/Gora/tutorial/log/loganalytics. java.
To analyze log data, the Mapper method uses pageview as the input, and the output key is a pair of <URL, timestamp>, and the value is 1, which indicates a time, aggregate the values of the same key in cer CER to get a total number, generate a metricdatum object, and serialize the object to the database.
Data Model Definition. Here we use the metricdatum object, which is defined in Gora-tutorial/src/main/Avro/metricdatum. JSON. We need to use bin/Gora compile to generate a Java file,
The JSON definition file is as follows:
{ "type": "record", "name": "MetricDatum", "namespace": "org.apache.gora.tutorial.log.generated", "fields" : [ {"name": "metricDimension", "type": "string"}, {"name": "timestamp", "type": "long"}, {"name": "metric", "type" : "long"} ]}
We can see that its type is record and its namespace is org. Apache. Gora. Tutorial. log. generated. There are three fields that define the field names and types respectively.
Next, we also need to define a dataing file for datastore. The content is as follows:
<class name="org.apache.gora.tutorial.log.generated.MetricDatum" keyClass="java.lang.String" table="Metrics"> <field name="metricDimension" family="common" qualifier="metricDimension"/> <field name="timestamp" family="common" qualifier="ts"/> <field name="metric" family="common" qualifier="metric"/> </class>
We can see that the class name, key type, and table name metrics are defined in the class. Field Names, family and qualifier are defined in field.
Now, run the Mr program and enter the following command:
$ bin/gora loganalytics
Some input is as follows:
12/07/21 13:35:16 INFO mapreduce.GoraRecordWriter: gora.buffer.write.limit = 1000012/07/21 13:35:17 INFO mapred.TaskRunner: Task:attempt_local_0001_r_000000_0 is done. And is in the process of commiting12/07/21 13:35:17 INFO mapred.LocalJobRunner: reduce > reduce12/07/21 13:35:17 INFO mapred.TaskRunner: Task 'attempt_local_0001_r_000000_0' done.12/07/21 13:35:17 INFO mapred.JobClient: map 100% reduce 100%12/07/21 13:35:17 INFO mapred.JobClient: Job complete: job_local_000112/07/21 13:35:17 INFO mapred.JobClient: Counters: 1212/07/21 13:35:17 INFO mapred.JobClient: FileSystemCounters12/07/21 13:35:17 INFO mapred.JobClient: FILE_BYTES_READ=56164512/07/21 13:35:17 INFO mapred.JobClient: FILE_BYTES_WRITTEN=112660612/07/21 13:35:17 INFO mapred.JobClient: Map-Reduce Framework12/07/21 13:35:17 INFO mapred.JobClient: Reduce input groups=303312/07/21 13:35:17 INFO mapred.JobClient: Combine output records=012/07/21 13:35:17 INFO mapred.JobClient: Map input records=1000012/07/21 13:35:17 INFO mapred.JobClient: Reduce shuffle bytes=012/07/21 13:35:17 INFO mapred.JobClient: Reduce output records=303312/07/21 13:35:17 INFO mapred.JobClient: Spilled Records=2000012/07/21 13:35:17 INFO mapred.JobClient: Map output bytes=51251012/07/21 13:35:17 INFO mapred.JobClient: Combine input records=012/07/21 13:35:17 INFO mapred.JobClient: Map output records=1000012/07/21 13:35:17 INFO mapred.JobClient: Reduce input records=1000012/07/21 13:35:17 INFO log.LogAnalytics: Log completed with success
After the preceding steps, you can view the metrics table of hbase and run the following command:
hbase(main):007:0> scan 'Metrics' ,{LIMIT=>1} ROW COLUMN+CELL /?a=1__-znawtuabsy&k=96804_1236 column=common:metric, timestamp=1342848916670, value=\x00\x00\x00\x00\x00\x00\x00\x09 902400000 /?a=1__-znawtuabsy&k=96804_1236 column=common:metricDimension, timestamp=1342848916670, value=/?a=1__-znawtuabsy&k=96804 902400000 /?a=1__-znawtuabsy&k=96804_1236 column=common:ts, timestamp=1342848916670, value=\x00\x00\x01\x1F\xFD \xD0\x00 902400000 1 row(s) in 0.0200 seconds
We can see that the analyzed data has been stored in the metrics table.
3. program code analysis
When defining a hadoop task, we can choose whether to use Gora as its input and output. Gora defines its own gorainputformat and goraoutputformat, gora also defines its own goramapper and gorareducer classes and provides some static initialization methods. In the following code, some static methods of Gora (mapper | reducer) are used for initialization. The following is the job initialization code:
public Job createJob(DataStore<Long, Pageview> inStore , DataStore<String, MetricDatum> outStore, int numReducer) throws IOException { Job job = new Job(getConf()); job.setJobName("Log Analytics"); job.setNumReduceTasks(numReducer); job.setJarByClass(getClass()); /* Mappers are initialized with GoraMapper.initMapper() or * GoraInputFormat.setInput()*/ GoraMapper.initMapperJob(job, inStore, TextLong.class, LongWritable.class , LogAnalyticsMapper.class, true); /* Reducers are initialized with GoraReducer#initReducer(). * If the output is not to be persisted via Gora, any reducer * can be used instead. */ GoraReducer.initReducerJob(job, outStore, LogAnalyticsReducer.class); return job; }
The following is the code of its run function:
@Override public int run(String[] args) throws Exception { DataStore<Long, Pageview> inStore; DataStore<String, MetricDatum> outStore; Configuration conf = new Configuration(); if(args.length > 0) { String dataStoreClass = args[0]; inStore = DataStoreFactory. getDataStore(dataStoreClass, Long.class, Pageview.class, conf); if(args.length > 1) { dataStoreClass = args[1]; } outStore = DataStoreFactory. getDataStore(dataStoreClass, String.class, MetricDatum.class, conf); } else {inStore = DataStoreFactory.getDataStore(Long.class, Pageview.class, conf);outStore = DataStoreFactory.getDataStore(String.class, MetricDatum.class, conf); } Job job = createJob(inStore, outStore, 3); boolean success = job.waitForCompletion(true); inStore.close(); outStore.close(); log.info("Log completed with " + (success ? "success" : "failure")); return success ? 0 : 1; }
We can see that two datastore inputs and outputs are generated.
Let's take a look at the custom loganalyticsmapper code, which is extended from goramapper and its input is <long, pageview>. Some code is as follows:
private TextLong tuple; protected void map(Long key, Pageview pageview, Context context) throws IOException ,InterruptedException { Utf8 url = pageview.getUrl(); long day = getDay(pageview.getTimestamp()); tuple.getKey().set(url.toString()); tuple.getValue().set(day); context.write(tuple, one); };
Loganalyticsreducer is also extended from gorareducer. Some code is as follows:
protected void reduce(TextLong tuple , Iterable<LongWritable> values, Context context) throws IOException ,InterruptedException { long sum = 0L; //sum up the values for(LongWritable value: values) { sum+= value.get(); } String dimension = tuple.getKey().toString(); long timestamp = tuple.getValue().get(); metricDatum.setMetricDimension(new Utf8(dimension)); metricDatum.setTimestamp(timestamp); String key = metricDatum.getMetricDimension().toString(); metricDatum.setMetric(sum); context.write(key, metricDatum); };
We can see that the output value type is metricdatum, which is the data model we previously defined in JSON and will be serialized into the database.
4. Reference
Http://gora.apache.org/docs/current/tutorial.html