gpt4 book ai didi

apache-spark - 打印 ResultIterable 对象的内容

转载 作者:行者123 更新时间:2023-12-02 09:23:23 25 4
gpt4 key购买 nike

如何打印具有行和列列表的 pyspark.resultiterable.ResultIterable 对象的内容

有内置函数吗?

我想要类似 dataframe.show()

enter image description here

最佳答案

我遇到了同样的问题并最终解决了它,所以让我分享我的做法......

假设我们有两个 RDD。

rdd1 = sc.parallelize([(1,'A'),(2,'B'),(3,'C')])
rdd2 = sc.parallelize([(1,'a'),(2,'b'),(3,'c')])

让我们将这些 RDD 组合起来以获得 ResultIterable。

cogrouped = rdd1.cogroup(rdd2)
for t in cogrouped.collect():
print t

>>
(1, (<pyspark.resultiterable.ResultIterable object at 0x107c49450>, <pyspark.resultiterable.ResultIterable object at 0x107c95690>))
(2, (<pyspark.resultiterable.ResultIterable object at 0x107c95710>, <pyspark.resultiterable.ResultIterable object at 0x107c95790>))
(3, (<pyspark.resultiterable.ResultIterable object at 0x107c957d0>, <pyspark.resultiterable.ResultIterable object at 0x107c95810>))

现在我们想看看这些 ResultIterables 里面有什么。我们可以这样做:

def iterate(iterable):
r = []
for v1_iterable in iterable:
for v2 in v1_iterable:
r.append(v2)

return tuple(r)

x = cogrouped.mapValues(iterate)
for e in x.collect():
print e

或者像这样

def iterate2(iterable):
r = []
for x in iterable.__iter__():
for y in x.__iter__():
r.append(y)
return tuple(r)

y = cogrouped.mapValues(iterate2)
for e in y.collect():
print e

在这两种情况下我们都会得到相同的结果:

(1, ('A', 'a'))
(2, ('B', 'b'))
(3, ('C', 'c'))

希望这对将来的人有所帮助。

关于apache-spark - 打印 ResultIterable 对象的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40043703/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com