2017-03-06 81 views
0

此功能:使用PySpark SQL函數

from pyspark.sql import functions as F 
lg = F.log(5.2) 

http://spark.apache.org/docs/latest/api/python/pyspark.sql.html

回報:

Py4JError: An error occurred while calling z:org.apache.spark.sql.functions.col. Trace: 
py4j.Py4JException: Method col([class java.lang.Double]) does not exist 
    at py4j.reflection.ReflectionEngine.getMethod(ReflectionEngine.java:318) 
    at py4j.reflection.ReflectionEngine.getMethod(ReflectionEngine.java:339) 
    at py4j.Gateway.invoke(Gateway.java:274) 
    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:745) 

文檔指向使用一個數據幀中的函數:

>>> df.select(log(10.0, df.age).alias('ten')).rdd.map(lambda l: str(l.ten)[:7]).collect() 
['0.30102', '0.69897'] 
>>> df.select(log(df.age).alias('e')).rdd.map(lambda l: str(l.e)[:7]).collect() 
['0.69314', '1.60943'] 

應該也有能力獨立使用log函數的值嗎?

回答

2

pyspark.sql中的函數應該用於數據幀列。這些函數期望列作爲參數傳遞。因此它正在尋找一個帶有你傳遞的名字的列對象(在這個例子中是5.2),因此就是錯誤。

對於將log應用於您應該使用的任何值math.log而不是