- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我想为 Hadoop 创建一个并行 scanLeft(计算关联运算符的前缀和)函数(特别是烫伤;请参阅下文了解如何完成)。
给定一个 hdfs 文件中的数字序列(每行一个),我想用连续偶数/奇数对的总和计算一个新序列。例如:
输入序列:
0,1,2,3,4,5,6,7,8,9,10
输出序列:
0+1、2+3、4+5、6+7、8+9、10
即
1,5,9,13,17,10
我认为,为了做到这一点,我需要为 Hadoop 编写一个 InputFormat 和 InputSplits 类,但我不知道该怎么做。
参见本节 3.3 here .以下是 Scala 中的示例算法:
// for simplicity assume input length is a power of 2
def scanadd(input : IndexedSeq[Int]) : IndexedSeq[Int] =
if (input.length == 1)
input
else {
//calculate a new collapsed sequence which is the sum of sequential even/odd pairs
val collapsed = IndexedSeq.tabulate(input.length/2)(i => input(2 * i) + input(2*i+1))
//recursively scan collapsed values
val scancollapse = scanadd(collapse)
//now we can use the scan of the collapsed seq to calculate the full sequence
val output = IndexedSeq.tabulate(input.length)(
i => i.evenOdd match {
//if an index is even then we can just look into the collapsed sequence and get the value
// otherwise we can look just before it and add the value at the current index
case Even => scancollapse(i/2)
case Odd => scancollapse((i-1)/2) + input(i)
}
output
}
我知道这可能需要相当多的优化才能与 Hadoop 很好地协同工作。直接翻译这个我认为会导致 Hadoop 代码非常低效。例如,显然在 Hadoop 中您不能使用 IndexedSeq。如果您看到任何具体问题,我将不胜感激。不过,我认为它可能会运作良好。
最佳答案
多余的。你是说这段代码?
val vv = (0 to 1000000).grouped(2).toVector
vv.par.foldLeft((0L, 0L, false))((a, v) =>
if (a._3) (a._1, a._2 + v.sum, !a._3) else (a._1 + v.sum, a._2, !a._3))
关于scala - 计算 Hadoop 上偶数/奇数对的总和?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14164723/
我的任务是编写一个java程序,首先询问用户将输入多少个数字,然后输出输入的奇数和偶数个数。它限制为整数 0-100。我的问题是:我的代码中缺少什么? import java.util.Scanner
我正在寻找有关 VBA 脚本的帮助。我一直在试图弄清楚如何使用 mod 功能。 这是我到目前为止所做的: Function AddOddNumbersWithMod(nr) Dim i, su
我只是想从 .NET 调用 kernel32 中的 GetPrivateProfileString 和 GetPrivateProfileSection,但遇到了一些我不明白的奇怪问题。 让我们从这个
我需要做的是在列表中交替应用 2 个函数。例如: (*2) (-3) [4,5,6,7,8] 会导致[8,2,12,4,16] , 因为 4*2 , 5-3 , 6*2 , 7-3 , 8*2 ...
我尝试在 JavaScript 中创建一个函数来判断一个数字是否为偶数,或者它是否是一个数字。我收到此错误: 这是 CodeCademy 中的类(class)。 最佳答案 您正在检查函数 isNaN
当我运行此命令时,不会打印任何内容,我尝试根据用户输入的内容打印一条消息,显示奇数或偶数。 import java.util.Scanner; public class Questions {
我必须编写一个程序来读取 3 个数字(使用输入框),并根据它们的值写入以下消息之一: 所有 3 个数字都是奇数或 所有 3 个数字都是偶数或 2 个数字是奇数,1 个数字是偶数或 1 个数字是奇数
我正在构建一个谷歌图像搜索的示例。我有一个图像网格(搜索结果)。当单击其中一张图像时,我正在使用 jquery 创建的部分标记中加载 html 文档。奇怪的是,如果你查看开发人员工具,html 已加载
我试图仅在偶数行上打印单词 * Even * ,而不包括第一行和第二行(最终不包括最后两行),但它打印 15 行,然后在整个过程中随机打印 * Even * 。使用 6 表示宽度,使用 15 表示高度
我有一个数学函数,它取决于由 给出的三个变量 {n、a 和 b} {a = n+1, b=n} 当 n 为偶数时 {b = n+1, a=n} 当 n 为奇数时 我的函数被调用了很多 次,n 各不相同
我有一个查询,其中有一个条件来检查房间号是否为奇数/偶数。问题在于房间号与建筑物信息一起存储在字符串中。 以下是数据库中数据的格式: ABC-0101A (Odd) ABC-0112B (Even
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
我有一个包含类似内容的页面。 ################################# # __________________________ # # | |
这个问题在这里已经有了答案: Can I combine :nth-child() or :nth-of-type() with an arbitrary selector? (9 个回答) Ho
我正在尝试了解这个素数分解的特定解决方案(取自 http://rosettacode.org/wiki/Prime_decomposition#Python:_Using_floating_point
我知道这可能是一个愚蠢而简单的问题,但我对编程还很陌生。我有以下关于我在一个程序中看到的 if 运算符的问题。这是代码: d= -12.4; if(d) printf("%d \n", abs
我正在尝试制作一个脚本,根据天气情况输出用户名,他们被分配奇数或偶数值。我想我已经设法让奇数的工作,但偶数的不会输出。这是它的样子。 'commentid' 是确定将它们分配给奇数还是偶数的值。 ";
我的 NPE 的 Stacktrace 开始于: Caused by: java.lang.NullPointerException at pl.yourvision.crm.web.serv
我正在尝试查找给定数字(用户输入)是偶数还是奇数。 I'm simply applying AND operation on binary digits of a no. with 1, If the
有谁知道用于可变范围的桶的哈希函数(对于字符串,如果它重要的话),它总是奇数(或质数,如果需要的话)? 本质上,我正在寻找一个散列函数,它将在 n 个桶上提供均匀分布,其中 n 是奇数(或质数,因为
我是一名优秀的程序员,十分优秀!