Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dsl -> functions, toDF() #9

Merged
merged 2 commits into from
Feb 13, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions python/docs/pyspark.sql.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,11 @@ pyspark.sql.types module
:members:
:undoc-members:
:show-inheritance:


pyspark.sql.functions module
------------------------
.. automodule:: pyspark.sql.functions
:members:
:undoc-members:
:show-inheritance:
3 changes: 1 addition & 2 deletions python/pyspark/sql/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@

from pyspark.sql.context import SQLContext, HiveContext
from pyspark.sql.types import Row
from pyspark.sql.dataframe import DataFrame, GroupedData, Column, Dsl, SchemaRDD
from pyspark.sql.dataframe import DataFrame, GroupedData, Column, SchemaRDD

__all__ = [
'SQLContext', 'HiveContext', 'DataFrame', 'GroupedData', 'Column', 'Row',
'Dsl',
]
27 changes: 24 additions & 3 deletions python/pyspark/sql/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,25 @@
__all__ = ["SQLContext", "HiveContext"]


def _monkey_patch_RDD(sqlCtx):
def toDF(self, schema=None, sampleRatio=None):
"""
Convert current :class:`RDD` into a :class:`DataFrame`

This is a shorthand for `sqlCtx.createDataFrame(rdd, schema, sampleRatio)`

:param schema: a StructType or list of names of columns
:param samplingRatio: the sample ratio of rows used for inferring
:return: a DataFrame

>>> rdd.toDF().collect()
[Row(name=u'Alice', age=1)]
"""
return sqlCtx.createDataFrame(self, schema, sampleRatio)

RDD.toDF = toDF


class SQLContext(object):

"""Main entry point for Spark SQL functionality.
Expand Down Expand Up @@ -70,6 +89,7 @@ def __init__(self, sparkContext, sqlContext=None):
self._jsc = self._sc._jsc
self._jvm = self._sc._jvm
self._scala_SQLContext = sqlContext
_monkey_patch_RDD(self)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in case i forget, can u add to the end of docstring for SQLContext that this adds a toDF method to RDD?


@property
def _ssql_ctx(self):
Expand Down Expand Up @@ -442,7 +462,7 @@ def jsonFile(self, path, schema=None, samplingRatio=1.0):
Row(f1=2, f2=None, f3=Row(field4=22,..., f4=[Row(field7=u'row2')])
Row(f1=None, f2=u'row3', f3=Row(field4=33, field5=[]), f4=None)

>>> df3 = sqlCtx.jsonFile(jsonFile, df1.schema())
>>> df3 = sqlCtx.jsonFile(jsonFile, df1.schema)
>>> sqlCtx.registerRDDAsTable(df3, "table2")
>>> df4 = sqlCtx.sql(
... "SELECT field1 AS f1, field2 as f2, field3 as f3, "
Expand Down Expand Up @@ -495,7 +515,7 @@ def jsonRDD(self, rdd, schema=None, samplingRatio=1.0):
Row(f1=2, f2=None, f3=Row(field4=22..., f4=[Row(field7=u'row2')])
Row(f1=None, f2=u'row3', f3=Row(field4=33, field5=[]), f4=None)

>>> df3 = sqlCtx.jsonRDD(json, df1.schema())
>>> df3 = sqlCtx.jsonRDD(json, df1.schema)
>>> sqlCtx.registerRDDAsTable(df3, "table2")
>>> df4 = sqlCtx.sql(
... "SELECT field1 AS f1, field2 as f2, field3 as f3, "
Expand Down Expand Up @@ -766,7 +786,8 @@ def _test():
Row(field1=2, field2="row2"),
Row(field1=3, field2="row3")]
)
globs['df'] = sqlCtx.createDataFrame(rdd)
_monkey_patch_RDD(sqlCtx)
globs['df'] = rdd.toDF()
jsonStrings = [
'{"field1": 1, "field2": "row1", "field3":{"field4":11}}',
'{"field1" : 2, "field3":{"field4":22, "field5": [10, 11]},'
Expand Down
Loading