- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在尝试学习 Hadoop。我已经编写了一个 map reduce 代码,用于查找 IP 命中总数和唯一 IP 地址,然后找到平均值(IP 命中总数/唯一 ID)。
但是我得到了所有 IP 的输出以及点击次数。但我无法获得相同的平均值。
代码:
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
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.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
public final class IPAddress {
private final static IntWritable ONE = new IntWritable(1);
static int totalHits = 0, uniqueIP = 0;
public final static void main(final String[] args) throws Exception
{
final Configuration conf = new Configuration();
final Job job = new Job(conf, "IPAddress");
job.setJarByClass(IPAddress.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
job.setMapperClass(IPMap.class);
job.setCombinerClass(IPReduce.class);
job.setReducerClass(IPReduce.class);
job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class);
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
job.waitForCompletion(true);
int average = totalHits/uniqueIP;
System.out.print("Average is :"+average+"\n");
}
public static final class IPMap extends Mapper<LongWritable, Text, Text, IntWritable>
{
private final Text mapKey = new Text();
public final void map(final LongWritable key, final Text value, final Context context) throws IOException, InterruptedException
{
final String line = value.toString();
final String[] data = line.trim().split("- -");
if (data.length > 1)
{
final String ipAddress = data[0];
mapKey.set(ipAddress);
context.write(mapKey, ONE);
}
}
}
public static final class IPReduce extends Reducer<Text, IntWritable, Text, IntWritable>
{
public final void reduce(final Text key, final Iterable<IntWritable> values, final Context context) throws IOException, InterruptedException
{
int count = 0, sum = 0, distinctIpCount=0;
for (final IntWritable val : values)
{
count += val.get();
sum += count;
distinctIpCount++;
}
totalHits = count;
uniqueIP = distinctIpCount;
context.write(key, new IntWritable(count));
}
}
}
最佳答案
关于执行 MapReduce 作业的一个重点是,即使您在一个类中提供所有代码,MapReduce 框架也会提取您提供的映射器和缩减器类并将它们发送到工作节点执行,而main()
方法在您开始工作的本地 JVM 上运行。这意味着 mapper 和 reducer 类方法对您在 mapper 和 reducer 类之外定义的任何变量不可见。
具体到您的用例,如果您想计算出所有 IP 地址的平均命中率,您可以在调用作业时仅使用一个 reducer (-D mapred.reduce.tasks=1
), 这样你就可以在 IPReducer
中定义 totalHits
和 uniqueIP
并且所有的 reduce()
调用都会看到这些变量的相同实例。然后,您可以在所有 reduce()
完成后运行的 reducer 的 cleanup()
方法中计算平均值。
您将无法轻松地将其发送回主程序以打印到屏幕,但您可以将结果作为作业输出(提供了相同的 Context
对象) ,或者如果您想将每个 IP 计数保留为主要作业输出,请使用 HDFS API 将平均值写入 HDFS 文件。 .
关于java - Hadoop - 查找 IP 命中总数和唯一 IP 地址,然后找到平均值(总 IP 命中数/uniqueIP),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28646339/
关于 APC 操作码缓存,什么是“命中与未命中”?我已经安装了 APC 并且它运行良好,但我有“一些”失误,我想知道这是否“不好”。此外,我正在运行 Openx,因此,我很快就会填满“缓存完整计数”。
我试过这个: def test_send_confirm_hit(monkeypatch): hit = False def called(): global hit
是否可以将找到的单词插入到替换中? $(function() { content = 'hallo mein name ist peter und ich komme aus berlin.
我有一个允许用户将文件上传到文件夹的网站。首先,我检查文件是否存在,如果存在,然后检查复选框的值以确定用户是否要覆盖现有文件。如果点击上传并且未选中该框,我会执行一个带有消息和后退按钮的 die()
我有多个不同的进程通过 IPC 进行通信,当使用 gdb 调试单个进程时,每当遇到断点时,我都会尝试向其他进程发送消息。有没有一种方法可以自动在遇到断点时自动调用一个函数/一段代码(NotifyAll
目前,通过管道传输到 jq 的 cat 命令帮助我解析工作目录中的多个 JSON 文件,并根据正则表达式模式匹配文件中所有可用的电子邮件 ID。但是,我很想识别正则表达式模式被命中/匹配的文件名 ca
我们希望将 podname 解析为 IP,以在 akka 集群中配置种子节点。这是通过在 Kubernetes 中使用 headless (headless)服务和有状态集的概念来实现的。但是,如何在
Maven 项目具有以下文件夹结构: src/main/java src/main/resources src/test/java src/test/resources 如果我们导航到 Maven 项
我只使用 c 几个星期,所以很可能会出现我忽略的明显错误。我看过其他线程,但我不明白我正在读的很多内容。该程序假设有一个无限大的牌组。 已知问题: clearBuffer 当前未使用,我正在尝试不同的
我已将我的 AdMob 代码实现到我的 XML 文件中,如下所示: 在我的 Activity 的 onCreate 方法中: // load ads
我的作业是通过示例程序确定给定跟踪文件的缓存读/写/未命中/命中次数。 举例来说,这是示例跟踪输出的前 10 行。 0x37c852: W 0xbfd4b18c 0x37cfe0: W 0xbfd
https://plnkr.co/edit/2h6fV5yTjeUqLP3SvbvO?p=preview 预期 登录后应用程序重定向到 $state container,其中包含 dashboard
ElasticSearch 独立于 from 和 size 参数,基于查询的所有命中构建聚合结果。在大多数情况下,这是我们想要的,但我有一个特殊情况,我需要将聚合限制为前 N 个命中。 limits
我使用 Intel PCM 进行细粒度的 CPU 测量。在我的代码中,我试图测量缓存效率。 基本上,我首先将一个小数组放入 L1 缓存(通过多次遍历),然后启动计时器,再遍历数组一次(希望使用缓存),
我在为 javascript 滑动元素定义点击区域时遇到问题。 参见示例: http://www.warface.co.uk/clients/warface.co.uk/ 请滑过右侧的灰色框以显示按钮
我正在尝试在 foldersystem 中使用 os.walk() 找到几个 'my_file.bat',如果文件名匹配它应该用 subprocess.call() 或 .run() 调用。问题是 o
我有一个端点,我可以在其中请求我使用 Siesta 查询的多条数据(例如 https://example.com/things?ids=1,2,3) .如果我只缓存了一些 things ,我试图弄清楚
这是我的代码: public static function test(){ try{ $apiContext = ApiContext::create(
我使用 PHP 在需要时传递登录表单,代码如下: $htmlForm = ''.''; switch(LOGIN_METHOD) { case 'both': $htmlFor
我正在使用 nginx-lua带有 redis 的模块提供 ember-app 的静态文件. index文件内容存储在redis作为 value由 nginx 正确提供服务当(根)domain/IP被
我是一名优秀的程序员,十分优秀!