为什么限制函数只将int作为参数?

时间:2017-12-14 14:25:24

标签: python pyspark limit

我正在使用pyspark并且想要获取我的数据帧的前n行。有一个名为limit的函数。但是,它只需要int作为参数。有没有办法用更大的价值来称呼它?此代码将导致异常:

import sys

dataframe = spark.createDataFrame([('Alice',), ('Bob',)])
dataframe.limit(sys.maxsize)

错误:

An error occurred while calling o127.limit. Trace:
py4j.Py4JException: Method limit([class java.lang.Long]) does not exist
    at py4j.reflection.ReflectionEngine.getMethod(ReflectionEngine.java:318)
    at py4j.reflection.ReflectionEngine.getMethod(ReflectionEngine.java:326)
    at py4j.Gateway.invoke(Gateway.java:272)
    at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132)
    at py4j.commands.CallCommand.execute(CallCommand.java:79)
    at py4j.GatewayConnection.run(GatewayConnection.java:214)
    at java.lang.Thread.run(Thread.java:748)

我认为提取long行是有效的要求,不是吗?

1 个答案:

答案 0 :(得分:2)

如问题limit中所述,函数仅需int作为输入。但是sys.maxsize返回long,它显示在异常(limit([class java.lang.Long]))中。为了毫无例外地运行此功能,您需要type castint

这是转换后的片段: -

import sys

dataframe = spark.createDataFrame([('Alice',), ('Bob',)])
dataframe.limit(int(sys.maxsize))