使用spark 时,您需要了解它的execution process 和programming api (pyspark - http://spark.apache.org/docs/2.1.0/api/python/pyspark.sql.html)。它与pandas/python 执行完全不同。它的执行取决于lazy evaluation,每当您需要检查数据时,您需要执行show、first、collect 或take 之类的操作。如果没有这些操作,它会返回 dataframe 和 schema(所以在你的问题中)。
让我通过一个例子向你介绍一些事情:-
process_df = sqlContext.createDataFrame([
['2013-01-01','U2_P1','p@c.com','100','P_P'],
['2013-01-01','U2_P2','p@c.com','100','P_P1'],
['2014-01-01','U2_P1','p@c.com','100','P_P'],
['2014-01-01','U2_P2','p@c.com','100','P_P1'],
['2015-01-01','U2_P1','p@c.com','100','P_P'],
['2015-01-01','U2_P2','p@c.com','100','P_P1']
], ['date','p1id','p2id','amount','p3id'])
#This prints Schema instead of Data
print process_df
DataFrame[date: string, p1id: string, p2id: string, amount: string, p3id: string]
#This prints data instead of schema
process_df.show()
+----------+-----+-------+------+----+
| date| p1id| p2id|amount|p3id|
+----------+-----+-------+------+----+
|2013-01-01|U2_P1|p@c.com| 100| P_P|
|2013-01-01|U2_P2|p@c.com| 100|P_P1|
|2014-01-01|U2_P1|p@c.com| 100| P_P|
|2014-01-01|U2_P2|p@c.com| 100|P_P1|
|2015-01-01|U2_P1|p@c.com| 100| P_P|
|2015-01-01|U2_P2|p@c.com| 100|P_P1|
+----------+-----+-------+------+----+
agg_data = process_df.groupby(['date']).agg({'amount':'sum'})
#This prints Schema instead of Data
print agg_data
DataFrame[date: string, sum(amount): double]
from pyspark.sql import functions as F
#This prints data instead of schema
agg_data.show()
+----------+-----------+
| date|sum(amount)|
+----------+-----------+
|2015-01-01| 200.0|
|2014-01-01| 200.0|
|2013-01-01| 200.0|
+----------+-----------+
from pyspark.sql import functions as F
agg_data.select('date', F.col('sum(amount)').alias('sum')).show()
+----------+-----+
| date| sum|
+----------+-----+
|2015-01-01|200.0|
|2014-01-01|200.0|
|2013-01-01|200.0|
+----------+-----+
这是一个仅打印数据的示例,如果您需要将这些数据输入
然后python需要用到collect,take,first,head。这里有几个
例子:-
print agg_data.collect()
[Row(date=u'2015-01-01', sum(amount)=200.0),
Row(date=u'2014-01-01', sum(amount)=200.0),
Row(date=u'2013-01-01', sum(amount)=200.0)]
print agg_data.first()
Row(date=u'2015-01-01', sum(amount)=200.0)
print agg_data.take(1)
[Row(date=u'2015-01-01', sum(amount)=200.0)]
agg_data.head()
Row(date=u'2015-01-01', sum(amount)=200.0)
这就是我们可以将数据带到 python 并对其进行争论的方式。
Hope this will help a lot.