问题三.ETL
这里我们需要提取每一条数据中的ip、url、pageId、contry、province、city字段。其中ip、url 和 pageId可以直接通过字符串的处理得到。contry、province 和 city 通过定义的工具类进行处理后得到。
Mapper
package ETL;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import utils.GetPageId;
import utils.LogParser;
import java.io.IOException;
import java.util.Map;
public class ETLMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
//解析日志字符串,获取日志信息
private static final IntWritable one = new IntWritable(1);
private Text outputKey = new Text();
private LogParser logParser = new LogParser();
private Logger logger = LoggerFactory.getLogger(ETLMapper.class);
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
// 解析日志记录
Map<String, String> logInfo = logParser.parse(value.toString());
if (logInfo == null) {
logger.error("日志记录的格式不正确或解析失败:" + value.toString());
return;
}
// 获取需要的字段
String ip = logInfo.get("ip");
String url = logInfo.get("url");
String country = logInfo.get("country");
String province = logInfo.get("province");
String city = logInfo.get("city");
// 调用 GetPageId 获取 topicId
String topicId = GetPageId.getPageId(url);
logInfo.put("pageId", topicId);
// 检查所有字段是否全部为空
if (ip != null || url != null || topicId != null || country != null || province != null || city != null) {
StringBuilder sb = new StringBuilder();
if (ip != null && !ip.isEmpty()) sb.append("IP: ").append(ip).append(", ");
if (url != null && !url.isEmpty()) sb.append("URL: ").append(url).append(", ");
if (topicId != null && !topicId.isEmpty()) sb.append("PageId: ").append(topicId).append(", ");
if (country != null && !country.isEmpty()) sb.append("Country: ").append(country).append(", ");
if (province != null && !province.isEmpty()) sb.append("Province: ").append(province).append(", ");
if (city != null && !city.isEmpty()) sb.append("City: ").append(city);
// 移除末尾的逗号和空格
String outputString = sb.toString().replaceAll(", $", "");
outputKey.set(outputString);
context.write(outputKey, one);
} else {
logger.error("所有字段为空,日志记录:" + value.toString());
}
}
}
首先,通过调用 logParser.parse 方法解析日志记录,并将解析结果存储在 logInfo 变量中。如果解析失败或者解析结果为空,则记录错误并返回。
然后,从 logInfo 中获取需要的字段,包括 ip、url、country、province、city。
接下来,通过调用 GetPageId.getPageId 方法获取 topicId,并将其存储到 logInfo 中的 "pageId" 键中。
进一步,检查所有字段是否全部为空。如果其中至少有一个字段不为空,则按照一定的格式构建输出字符串,并将其设置为 outputKey 的值。
最后,通过调用 context.write 方法输出键值对,其中键为 outputKey,值为 one
Reduce
package ETL;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
import java.io.IOException;
public class ETLReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
private IntWritable result = new IntWritable();
@Override
protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
int sum = 0;
for (IntWritable val : values) {
sum += val.get();//迭代累加
}
result.set(sum);
context.write(key, result);//写出结果
}
}
protected void reduce(Text key, Iterable<IntWritable> values, Context context)
key: 当前分组的键。
values: 属于当前键的所有值的集合。
context: 用于与 Hadoop 框架进行交互的上下文对象
Driver
package ETL;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
public class ETLDriver {
public static void main(String[] args) throws Exception {
if (args.length != 2) {
System.err.println("Usage: LogETLDriver <input path> <output path>");
System.exit(-1);
}
Configuration conf = new Configuration();
Job job = Job.getInstance(conf, "Log ETL");
job.setJarByClass(ETLDriver.class);
job.setMapperClass(ETLMapper.class);
job.setCombinerClass(ETLReducer.class);
job.setReducerClass(ETLReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}