Skip to content

Commit c29e58d

Browse files
author
Olivier Girardot
committed
SPARK-6992 : Fix documentation example for Spark SQL on StructType
This patch is fixing the Java examples for Spark SQL when defining programmatically a Schema and mapping Rows.
1 parent 7d0ab55 commit c29e58d

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

docs/sql-programming-guide.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -555,13 +555,16 @@ by `SQLContext`.
555555

556556
For example:
557557
{% highlight java %}
558-
// Import factory methods provided by DataType.
559-
import org.apache.spark.sql.types.DataType;
558+
import org.apache.spark.api.java.function.Function;
559+
// Import factory methods provided by DataTypes.
560+
import org.apache.spark.sql.types.DataTypes;
560561
// Import StructType and StructField
561562
import org.apache.spark.sql.types.StructType;
562563
import org.apache.spark.sql.types.StructField;
563564
// Import Row.
564565
import org.apache.spark.sql.Row;
566+
// Import RowFactory.
567+
import org.apache.spark.sql.RowFactory;
565568

566569
// sc is an existing JavaSparkContext.
567570
SQLContext sqlContext = new org.apache.spark.sql.SQLContext(sc);
@@ -575,16 +578,16 @@ String schemaString = "name age";
575578
// Generate the schema based on the string of schema
576579
List<StructField> fields = new ArrayList<StructField>();
577580
for (String fieldName: schemaString.split(" ")) {
578-
fields.add(DataType.createStructField(fieldName, DataType.StringType, true));
581+
fields.add(DataTypes.createStructField(fieldName, DataTypes.StringType, true));
579582
}
580-
StructType schema = DataType.createStructType(fields);
583+
StructType schema = DataTypes.createStructType(fields);
581584

582585
// Convert records of the RDD (people) to Rows.
583586
JavaRDD<Row> rowRDD = people.map(
584587
new Function<String, Row>() {
585588
public Row call(String record) throws Exception {
586589
String[] fields = record.split(",");
587-
return Row.create(fields[0], fields[1].trim());
590+
return RowFactory.create(fields[0], fields[1].trim());
588591
}
589592
});
590593

0 commit comments

Comments
 (0)