• <bdo id='pZEpj'></bdo><ul id='pZEpj'></ul>
  1. <legend id='pZEpj'><style id='pZEpj'><dir id='pZEpj'><q id='pZEpj'></q></dir></style></legend>
    <i id='pZEpj'><tr id='pZEpj'><dt id='pZEpj'><q id='pZEpj'><span id='pZEpj'><b id='pZEpj'><form id='pZEpj'><ins id='pZEpj'></ins><ul id='pZEpj'></ul><sub id='pZEpj'></sub></form><legend id='pZEpj'></legend><bdo id='pZEpj'><pre id='pZEpj'><center id='pZEpj'></center></pre></bdo></b><th id='pZEpj'></th></span></q></dt></tr></i><div id='pZEpj'><tfoot id='pZEpj'></tfoot><dl id='pZEpj'><fieldset id='pZEpj'></fieldset></dl></div>
  2. <small id='pZEpj'></small><noframes id='pZEpj'>

      <tfoot id='pZEpj'></tfoot>
    1. 使用 Apache Spark 将键值对缩减为键列表对

      时间:2023-09-12
      <legend id='z4iOR'><style id='z4iOR'><dir id='z4iOR'><q id='z4iOR'></q></dir></style></legend>

      <tfoot id='z4iOR'></tfoot>
        <i id='z4iOR'><tr id='z4iOR'><dt id='z4iOR'><q id='z4iOR'><span id='z4iOR'><b id='z4iOR'><form id='z4iOR'><ins id='z4iOR'></ins><ul id='z4iOR'></ul><sub id='z4iOR'></sub></form><legend id='z4iOR'></legend><bdo id='z4iOR'><pre id='z4iOR'><center id='z4iOR'></center></pre></bdo></b><th id='z4iOR'></th></span></q></dt></tr></i><div id='z4iOR'><tfoot id='z4iOR'></tfoot><dl id='z4iOR'><fieldset id='z4iOR'></fieldset></dl></div>
        • <small id='z4iOR'></small><noframes id='z4iOR'>

                <bdo id='z4iOR'></bdo><ul id='z4iOR'></ul>
                  <tbody id='z4iOR'></tbody>
                本文介绍了使用 Apache Spark 将键值对缩减为键列表对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我正在编写一个 Spark 应用程序,并希望将一组键值对 (K, V1), (K, V2), ..., (K, Vn) 组合成一个键-多值对(K, [V1, V2, ..., Vn]).我觉得我应该能够使用具有某种风味的 reduceByKey 函数来做到这一点:

                I am writing a Spark application and want to combine a set of Key-Value pairs (K, V1), (K, V2), ..., (K, Vn) into one Key-Multivalue pair (K, [V1, V2, ..., Vn]). I feel like I should be able to do this using the reduceByKey function with something of the flavor:

                My_KMV = My_KV.reduce(lambda a, b: a.append([b]))
                

                发生这种情况时我得到的错误是:

                The error that I get when this occurs is:

                NoneType"对象没有附加"属性.

                'NoneType' object has no attribue 'append'.

                我的键是整数,值 V1,...,Vn 是元组.我的目标是使用键和值列表(元组)创建一对.

                My keys are integers and values V1,...,Vn are tuples. My goal is to create a single pair with the key and a list of the values (tuples).

                推荐答案

                Map和ReduceByKey

                reduce的输入类型和输出类型必须相同,所以如果你想聚合一个列表,你必须map输入到列表.然后将这些列表合并为一个列表.

                Input type and output type of reduce must be the same, therefore if you want to aggregate a list, you have to map the input to lists. Afterwards you combine the lists into one list.

                组合列表

                您需要一种将列表合并为一个列表的方法.Python 提供了一些组合列表的方法.

                You'll need a method to combine lists into one list. Python provides some methods to combine lists.

                append 修改第一个列表,并且总是返回 None.

                append modifies the first list and will always return None.

                x = [1, 2, 3]
                x.append([4, 5])
                # x is [1, 2, 3, [4, 5]]
                

                extend 做同样的事情,但解开列表:

                extend does the same, but unwraps lists:

                x = [1, 2, 3]
                x.extend([4, 5])
                # x is [1, 2, 3, 4, 5]
                

                这两种方法都返回 None,但您需要一个返回组合列表的方法,因此只需 使用加号.

                Both methods return None, but you'll need a method that returns the combined list, therefore just use the plus sign.

                x = [1, 2, 3] + [4, 5]
                # x is [1, 2, 3, 4, 5]
                

                火花

                file = spark.textFile("hdfs://...")
                counts = file.flatMap(lambda line: line.split(" ")) 
                         .map(lambda actor: (actor.split(",")[0], actor))  
                
                         # transform each value into a list
                         .map(lambda nameTuple: (nameTuple[0], [ nameTuple[1] ])) 
                
                         # combine lists: ([1,2,3] + [4,5]) becomes [1,2,3,4,5]
                         .reduceByKey(lambda a, b: a + b)
                

                <小时>

                组合键

                也可以使用 combineByKey 来解决这个问题,它在内部用于实现 reduceByKey,但它更复杂并且 在 Spark 中使用一种专门的按键组合器可以更快"一个>.对于上面的解决方案,您的用例已经足够简单了.

                It's also possible to solve this with combineByKey, which is used internally to implement reduceByKey, but it's more complex and "using one of the specialized per-key combiners in Spark can be much faster". Your use case is simple enough for the upper solution.

                GroupByKey

                也可以使用 groupByKey、但它会减少并行化,因此对于大数据集可能会慢得多.

                It's also possible to solve this with groupByKey, but it reduces parallelization and therefore could be much slower for big data sets.

                这篇关于使用 Apache Spark 将键值对缩减为键列表对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:TypeError:列表索引必须是整数,而不是 str Python 下一篇:python中的Hadoop Streaming Job失败错误

                相关文章

                最新文章

                <i id='NNJjG'><tr id='NNJjG'><dt id='NNJjG'><q id='NNJjG'><span id='NNJjG'><b id='NNJjG'><form id='NNJjG'><ins id='NNJjG'></ins><ul id='NNJjG'></ul><sub id='NNJjG'></sub></form><legend id='NNJjG'></legend><bdo id='NNJjG'><pre id='NNJjG'><center id='NNJjG'></center></pre></bdo></b><th id='NNJjG'></th></span></q></dt></tr></i><div id='NNJjG'><tfoot id='NNJjG'></tfoot><dl id='NNJjG'><fieldset id='NNJjG'></fieldset></dl></div>
                <legend id='NNJjG'><style id='NNJjG'><dir id='NNJjG'><q id='NNJjG'></q></dir></style></legend>

                    <bdo id='NNJjG'></bdo><ul id='NNJjG'></ul>
                1. <tfoot id='NNJjG'></tfoot>

                  <small id='NNJjG'></small><noframes id='NNJjG'>