To extract data from the log, the log file is as follows:
2014-05-10 13:36:40,140307000287,536dbacc4700aab274729cca,login2014-05-10 13:37:46,140310000378,536dbae74700aab274729ccb,login2014-05-10 13:39:20,140310000382,536dbb284700aab274729ccd,login2014-05-10 13:39:31,140331001080,536dbb864700aab274729ccf,login2014-05-10 13:39:45,140331001105,536dbba04700aab274729cd4,login2014-05-10 13:39:45,140328000969,536dbba04700aab274729ce4,login2014-05-10 13:39:45,140408001251,536dbba04700aab274729cd8,login2014-05-10 13:39:45,140328000991,536dbba04700aab274729ce9,login2014-05-10 13:39:45,140324000633,536dbba14700aab274729cf5,login2014-05-10 13:39:45,140331001077,536dbba04700aab274729cdd,login2014-05-10 13:39:45,140408001242,536dbba04700aab274729cd7,login2014-05-10 13:39:45,140327000941,536dbba14700aab274729cf1,login2014-05-10 13:39:45,140408001265,536dbba04700aab274729ce5,login2014-05-10 13:39:45,140324000673,536dbba04700aab274729cd3,login2014-05-10 13:39:45,140331001066,536dbba04700aab274729cd5,login2014-05-10 13:39:45,140408001292,536dbba14700aab274729cee,login2014-05-10 13:39:45,140328000966,536dbba14700aab274729cec,login2014-05-10 13:39:45,140312000501,536dbba04700aab274729ce1,login2014-05-10 13:39:45,140306000216,536dbba14700aab274729d02,login2014-05-10 13:39:45,140327000856,536dbba04700aab274729ce2,login2014-05-10 13:39:46,140328000985,536dbba14700aab274729cf7,login2014-05-10 13:39:46,140306000245,536dbba14700aab274729d0d,login2014-05-10 13:39:46,140326000797,536dbba14700aab274729cf6,login2014-05-10 13:39:46,140328000993,536dbba14700aab274729d12,login2014-05-10 13:39:46,140331001115,536dbba14700aab274729d10,login2014-05-10 13:39:46,140325000744,536dbba04700aab274729ce0,login2014-05-10 13:39:46,140328000982,536dbba14700aab274729d0a,login2014-05-10 13:39:46,140331001063,536dbba04700aab274729ce3,login2014-05-10 13:39:46,140331001067,536dbba14700aab274729d1c,login2014-05-10 13:39:46,140401001157,536dbba04700aab274729ce8,login2014-05-10 13:39:46,140408001216,536dbba14700aab274729cef,login2014-05-10 13:39:46,140401001174,536dbba14700aab274729d27,login2014-05-10 13:39:46,140306000215,536dbba04700aab274729cde,login2014-05-10 13:39:46,140331001064,536dbba04700aab274729cdc,login2014-05-10 13:39:46,140326000825,536dbba04700aab274729cd9,login2014-05-10 13:39:46,140408001294,536dbba14700aab274729d0f,login
I want to obtain the device ID before login, calculate the number, and finally get the result:
Total number of logins for each device
536dbba04700aab274729cdc 5
536dbba04700aab274729ce3 4
Well, create a logmapper class, which is responsible for data map. The first two template parameters are used for keyin and valuein, and the last two template parameters are used for keyout and valueout, which are both representative types.
Assume that a <keyin, valuein> component is a pair, and many of the pair inputs are in a group. After these pair pairs are mapped by certain algorithms, they become many pair groups.
Official documents: http://hadoop.apache.org/docs/r2.4.1/api/org/apache/hadoop/mapreduce/Mapper.html
Maps input key/value pairs to a set of intermediate key/value pairs.
Note that the er Class here uses mapreduce, and the old package is called mapred.
The differences between the two are described here:
Http://stackoverflow.com/questions/7598422/is-it-better-to-use-the-mapred-or-the-mapreduce-package-to-create-a-hadoop-job
Longwritable and intwritable are two classes used to create variables of the long and INT types. They can help serialize long and INT values into word throttling, so there are two key methods to read and write:
void |
readFields(DataInput in)
Deserialize the fields of this object fromin. |
| |
|
void |
write(DataOutput out)
Serialize the fields of this objectout. |
This is related to the serialization Algorithm Used in hadoop internal RPC calls.
My mapper code:
package org.freebird.mapper;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.mapreduce.Mapper;/** * Describe class LogMapper here. * * * Created: Wed Sep 24 15:14:17 2014 * * @author <a href="mailto:[email protected]"></a> * @version 1.0 */public class LogMapper extends Mapper<LongWritable, Text, Text, IntWritable> { private final static IntWritable ONE = new IntWritable(1); public void map(Object key, Text value, org.apache.hadoop.mapreduce.Mapper.Context ctx) throws IOException, InterruptedException { String[] line = value.toString().split(","); if (line.length == 4) { String dId = line[2]; ctx.write(new Text(dId), ONE); } }}
This er subclass overwrites the map function. After the string is used and the number is separated, the third element is taken as the device ID and then written to the context object as the key.
Here, the value is set to 1, because the sum is simple in the reduce stage.
Context Reference: http://hadoop.apache.org/docs/r1.1.1/api/org/apache/hadoop/mapreduce/Mapper.Context.html
The write method is not a general concept of hasmap to add key, value, but to generate a new pair object, which contains key and value. If multiple keys are the same, multiple pair objects are generated and handed over to the reduce stage for processing.
The basic process is that the framework uses fileinputformat to read files. By default, it splits records based on the file size. Here the splitter is called inputsplitter. You can use inputsplitter to split the file into several blocks, and then there will be several er corresponding to it.
In inputsplitter, recordreader is used to read the file's fast record, generate the key/value pair, and call the Mapper's map function for processing.
Of course, some of these processes can be customized. For example, the inputsplitter algorithm can be modified, and the recordreader can also be customized.
There is also a very effective method to avoid mapper from passing too much data to Cer.
For example, the previous example is 1. In fact, you can use a hashmap to group keys. If there is a hashmap, value is added to 1, and if there is no hashmap, it is added to hashmap.
Finally, the key/value data After grouping statistics is sent to Cer CER through the context. Write method, which can greatly improve the efficiency.
Implementation of mapreduce 2.x programming Series 2 Map stage