Github user marmbrus commented on a diff in the pull request:
https://github.com/apache/spark/pull/1598#discussion_r15714321
--- Diff: python/pyspark/sql.py ---
@@ -522,71 +866,112 @@ def _ssql_ctx(self):
return self._scala_SQLContext
def inferSchema(self, rdd):
- """Infer and apply a schema to an RDD of L{dict}s.
+ """Infer and apply a schema to an RDD of L{Row}s.
+
+ We peek at the first row of the RDD to determine the fields' names
+ and types. Nested collections are supported, which include array,
+ dict, list, Row, tuple, namedtuple, or object.
- We peek at the first row of the RDD to determine the fields names
- and types, and then use that to extract all the dictionaries.
Nested
- collections are supported, which include array, dict, list, set,
and
- tuple.
+ Each row in `rdd` should be Row object or namedtuple or objects,
+ using dict is deprecated.
+ >>> rdd = sc.parallelize(
+ ... [Row(field1=1, field2="row1"),
+ ... Row(field1=2, field2="row2"),
+ ... Row(field1=3, field2="row3")])
>>> srdd = sqlCtx.inferSchema(rdd)
- >>> srdd.collect() == [{"field1" : 1, "field2" : "row1"},
{"field1" : 2, "field2": "row2"},
- ... {"field1" : 3, "field2": "row3"}]
- True
+ >>> srdd.collect()[0]
+ Row(field1=1, field2=u'row1')
- >>> from array import array
+ >>> NestedRow = Row("f1", "f2")
+ >>> nestedRdd1 = sc.parallelize([
+ ... NestedRow(array('i', [1, 2]), {"row1": 1.0}),
+ ... NestedRow(array('i', [2, 3]), {"row2": 2.0})])
>>> srdd = sqlCtx.inferSchema(nestedRdd1)
- >>> srdd.collect() == [{"f1" : [1, 2], "f2" : {"row1" : 1.0}},
- ... {"f1" : [2, 3], "f2" : {"row2" : 2.0}}]
- True
+ >>> srdd.collect()
+ [Row(f1=[1, 2], f2={u'row1': 1.0}), ..., f2={u'row2': 2.0})]
+ >>> nestedRdd2 = sc.parallelize([
+ ... NestedRow([[1, 2], [2, 3]], [1, 2]),
+ ... NestedRow([[2, 3], [3, 4]], [2, 3])])
>>> srdd = sqlCtx.inferSchema(nestedRdd2)
- >>> srdd.collect() == [{"f1" : [[1, 2], [2, 3]], "f2" : [1, 2]},
- ... {"f1" : [[2, 3], [3, 4]], "f2" : [2, 3]}]
- True
+ >>> srdd.collect()
+ [Row(f1=[[1, 2], [2, 3]], f2=[1, 2]), ..., f2=[2, 3])]
"""
- if (rdd.__class__ is SchemaRDD):
- raise ValueError("Cannot apply schema to %s" %
SchemaRDD.__name__)
- elif not isinstance(rdd.first(), dict):
- raise ValueError("Only RDDs with dictionaries can be converted
to %s: %s" %
- (SchemaRDD.__name__, rdd.first()))
- jrdd = self._pythonToJavaMap(rdd._jrdd)
- srdd = self._ssql_ctx.inferSchema(jrdd.rdd())
- return SchemaRDD(srdd, self)
+ if isinstance(rdd, SchemaRDD):
+ raise TypeError("Cannot apply schema to SchemaRDD")
+
+ first = rdd.first()
+ if not first:
+ raise ValueError("The first row in RDD is empty, "
+ "can not infer schema")
+ if type(first) is dict:
+ warnings.warn("Using RDD of dict to inferSchema is deprecated")
+
+ schema = _infer_schema(first)
+ rdd = rdd.mapPartitions(lambda rows: _drop_schema(rows, schema))
+ return self.applySchema(rdd, schema)
def applySchema(self, rdd, schema):
- """Applies the given schema to the given RDD of L{dict}s.
+ """
+ Applies the given schema to the given RDD of L{tuple} or L{list}s.
--- End diff --
... These tuples or lists can contain complex nested structures like lists
maps for nested rows. It is important that the schema matches the types of the
objects in each row or exceptions could be thrown at runtime.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---