小编ivy*_*wit的帖子

pyspark,比较数据帧中的两行

我试图将数据帧中的一行与下一行进行比较以查看时间戳的差异.目前的数据如下:

 itemid | eventid | timestamp
 ----------------------------
 134    | 30      | 2016-07-02 12:01:40
 134    | 32      | 2016-07-02 12:21:23
 125    | 30      | 2016-07-02 13:22:56
 125    | 32      | 2016-07-02 13:27:07
Run Code Online (Sandbox Code Playgroud)

我已经尝试将一个函数映射到数据帧上,以便进行这样的比较:(注意:我正在尝试获得差异大于4小时的行)

items = df.limit(10)\
          .orderBy('itemid', desc('stamp'))\
          .map(lambda x,y: (x.stamp - y.stamp) > 14400).collect()
Run Code Online (Sandbox Code Playgroud)

但是我收到以下错误:

Py4JJavaError: An error occurred while calling 
z:org.apache.spark.api.python.PythonRDD.collectAndServe
Run Code Online (Sandbox Code Playgroud)

我认为这是由于我错误地使用了map功能.帮助使用地图或不同的解决方案将不胜感激.

更新: @ zero323的答案提供了有关我不正确使用映射的信息,但是我使用的系统在2.02之前运行Spark版本并且我正在使用Cassandra中的数据.

我设法用mapPartitions解决它.请参阅下面的答案.

更新(2017/03/27): 由于最初标记了这篇文章的答案,我对Spark的理解有了显着改善.我在下面更新了我的答案,以显示我当前的解决方案.

python apache-spark apache-spark-sql pyspark pyspark-sql

4
推荐指数
2
解决办法
5578
查看次数

Python元类冲突/类型错误

我正在调试一个 python 脚本(python 不是我的首选语言),这是我第一次在 python 中使用元类。当我运行如下代码时,出现元类冲突错误。

TypeError: Error when calling the metaclass bases
    metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases
Run Code Online (Sandbox Code Playgroud)

在尝试解决它时,我注释掉了 中的元类声明MySQLMSoftware,认为它是多余的,因为它继承自具有相同元类声明的类,但这导致:

TypeError: Error when calling the metaclass bases
    module.__init__() takes at most 2 arguments (3 given)
Run Code Online (Sandbox Code Playgroud)

任何见解、指导或方向将不胜感激。我一直在阅读有关 python 的元类实现的信息,到目前为止这个问题还没有出现。

MSoftware.py

from abc import ABCMeta, abstractmethod

class MSoftware(object) :
    __metaclass__ = ABCMeta

    def __init__(self,name,spark=None,mysql=None):
        self.name = name
        ...
Run Code Online (Sandbox Code Playgroud)

MySQLM软件.py

from mouse import Mouse, MSoftware
from …
Run Code Online (Sandbox Code Playgroud)

python python-2.7

3
推荐指数
1
解决办法
5574
查看次数