WebPvMapReduce

本文介绍了一种使用Hadoop进行Web页面访问量(WebPV)分析的方法。通过MapReduce编程模型,从日志文件中提取省份ID,并统计各省份的访问次数。首先过滤非法数据并清洗字段,然后使用Map阶段提取省份ID,最后在Reduce阶段汇总访问次数。
###WebPvMapReduce 1

package com.myblue.myhdfs;

import java.io.IOException;
import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
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.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

public class WebPvMapReduce extends Configured implements Tool {

//map
public static class ModuleMapper extends
Mapper<LongWritable, Text, IntWritable, IntWritable> {

protected void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {

String lineValue = value.toString();
String[] splits = lineValue.split("\t");
//过滤非法数据,若该行数据少于30字段,则视为非法数据,不再处理
if (splits.length < 30) {
//参数:计数器组,计数器名
context.getCounter("Web Pv Counter", "Length limit 30").increment(1L);
return;
}

String url = splits[1];// 第2个字段为url
if (StringUtils.isBlank(url)) {
context.getCounter("Web Pv Counter", "Url is Blank") .increment(1L);
return;
}
String provinceIdValue = splits[23];// 第24个字段为provinceId
if (StringUtils.isBlank(provinceIdValue)) {
context.getCounter("Web Pv Counter", "Province is Blank").increment(1L);
return;
}

int provinceId = 0;
try {
provinceId = Integer.parseInt(provinceIdValue);
} catch (Exception e) {
System.out.println(e);
return;
}

IntWritable mapOutputKey = new IntWritable();
mapOutputKey.set(provinceId);
IntWritable mapOutputValue = new IntWritable(1);//本例输出恒为1
context.write(mapOutputKey, mapOutputValue);
}
}

//reduce
public static class ModuleReducer extends
Reducer<IntWritable, IntWritable, IntWritable, IntWritable> {

protected void reduce(IntWritable key, Iterable<IntWritable> values,
Context context) throws IOException, InterruptedException {

int sum = 0;
for (IntWritable value : values) {
sum += value.get();
}

IntWritable outputValue = new IntWritable();
outputValue.set(sum);
context.write(key, outputValue);
}
}

public int run(String[] args) throws Exception {

// 创建作业
Configuration conf = new Configuration();
Job job = Job.getInstance(conf);
job.setJarByClass(getClass());

// 输入、输出路径
FileInputFormat.addInputPath(job, new Path(args[0]));
Path outPath = new Path(args[1]);
FileSystem dfs = FileSystem.get(conf);
if (dfs.exists(outPath)) {
dfs.delete(outPath, true);
}
FileOutputFormat.setOutputPath(job, outPath);

// mapper
job.setMapperClass(ModuleMapper.class);
job.setMapOutputKeyClass(IntWritable.class);
job.setMapOutputValueClass(IntWritable.class);

// reducer
job.setReducerClass(ModuleReducer.class);
job.setOutputKeyClass(IntWritable.class);
job.setOutputValueClass(IntWritable.class);

// 提交作业
return job.waitForCompletion(true) ? 0 : 1;
}

public static void main(String[] args) throws Exception {
args=new String[]{"/input2","/output2"};
// 使用ToolRunner运行作业
Configuration conf = new Configuration();
int status = ToolRunner.run(conf, new WebPvMapReduce(), args);
System.exit(status);
}
}

###WebPvMapReduce 2

package com.myblue.myhdfs;

import java.io.IOException;

import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
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.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

public class WebPvMapReduce2 extends Configured implements Tool{
//Map
public static class Map extends Mapper<LongWritable, Text, IntWritable, IntWritable>{
private IntWritable mapOutputKey = new IntWritable();
private static final IntWritable mapOutPutValue = new IntWritable(1);
@Override
protected void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
//1 value toString hadoop ---> java
String lineValue = value.toString();
//2 split \t
String[] wordValues = lineValue.split("\t");
//3 清洗数据
if(wordValues.length < 30){
//context.getCounter("Web Pv Count", "Length limit 30").increment(1l);
return;
}
if(StringUtils.isEmpty(wordValues[1])){
//context.getCounter("Web Pv Count", "Url is Blank");
return;
}
String provinceIdValue = wordValues[23];
if(StringUtils.isEmpty(provinceIdValue)){
//context.getCounter("Web Pv Count", "Province is Blank");
return;
}
int provinceId = 0;
try {
provinceId = Integer.parseInt(provinceIdValue);
} catch (Exception e) {
System.out.println(e);
return;
}
//4 写入框架
mapOutputKey.set(provinceId);
context.write(mapOutputKey, mapOutPutValue);
}
}
public static class Reduce extends Reducer<IntWritable, IntWritable, IntWritable, IntWritable>{
private IntWritable reduceOutputValue = new IntWritable();
@Override
protected void reduce(IntWritable key, Iterable<IntWritable> vales,Context context)
throws IOException, InterruptedException {
//sum value
int sum = 0;
for (IntWritable value : vales) {
sum += value.get();
}
//write frame
reduceOutputValue.set(sum);
context.write(key, reduceOutputValue);
}
}
//run
public int run(String[] args) throws Exception {
//1 get Configuration
Configuration conf = getConf();
//2 get job
Job job = Job.getInstance(conf);
//run jar
job.setJarByClass(this.getClass());
//3 set job
//3.1 input
Path inputPath = new Path(args[0]);
FileInputFormat.addInputPath(job, inputPath);
//3.2 map
job.setMapperClass(Map.class);
job.setMapOutputKeyClass(IntWritable.class);
job.setMapOutputValueClass(IntWritable.class);
//3.3 reduce
job.setReducerClass(Reduce.class);
job.setOutputKeyClass(IntWritable.class);
job.setOutputValueClass(IntWritable.class);
//3.4 output
Path outputPath = new Path(args[1]);
FileSystem fs = FileSystem.get(conf);
if(fs.exists(outputPath)){
fs.delete(outputPath,true);
}
FileOutputFormat.setOutputPath(job, outputPath);
//4 submit job
boolean isSuccess = job.waitForCompletion(true);
return isSuccess ? 0 : 1;
}

public static void main(String[] args) throws Exception{
//1 new Configuration
args = new String[]{"/input","/output"};
Configuration conf = new Configuration();
//2 run
int status = ToolRunner.run(conf, new WebPvMapReduce2(), args);
//3 exits
System.exit(status);
}
}

深度学习作为人工智能的关键分支,依托多层神经网络架构对高维数据进行模式识别与函数逼近,广泛应用于连续变量预测任务。在Python编程环境中,得益于TensorFlow、PyTorch等框架的成熟生态,研究者能够高效构建面向回归分析的神经网络模型。本资源库聚焦于通过循环神经网络及其优化变体解决时序预测问题,特别针对传统RNN在长程依赖建模中的梯度异常现象,引入具有门控机制的长短期记忆网络(LSTM)以增强序列建模能力。 实践案例涵盖从数据预处理到模型评估的全流程:首先对原始时序数据进行标准化处理与滑动窗口分割,随后构建包含嵌入层、双向LSTM层及全连接层的网络结构。在模型训练阶段,采用自适应矩估计优化器配合早停策略,通过损失函数曲线监测过拟合现象。性能评估不仅关注均方根误差等量化指标,还通过预测值与真实值的轨迹可视化进行定性分析。 资源包内部分为三个核心模块:其一是经过清洗的金融时序数据集,包含标准化后的股价波动记录;其二是模块化编程实现的模型构建、训练与验证流程;其三是基于Matplotlib实现的动态结果展示系统。所有代码均遵循面向对象设计原则,提供完整的类型注解与异常处理机制。 该实践项目揭示了深度神经网络在非线性回归任务中的优势:通过多层非线性变换,模型能够捕获数据中的高阶相互作用,而Dropout层与正则化技术的运用则保障了泛化能力。值得注意的是,当处理高频时序数据时,需特别注意序列平稳性检验与季节性分解等预处理步骤,这对预测精度具有决定性影响。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值