|
18 | 18 | package org.apache.spark.examples.sql; |
19 | 19 |
|
20 | 20 | import java.io.Serializable; |
| 21 | +import java.util.Arrays; |
21 | 22 | import java.util.List; |
22 | 23 |
|
23 | 24 | import org.apache.spark.SparkConf; |
@@ -56,6 +57,8 @@ public static void main(String[] args) throws Exception { |
56 | 57 | JavaSparkContext ctx = new JavaSparkContext(sparkConf); |
57 | 58 | JavaSQLContext sqlCtx = new JavaSQLContext(ctx); |
58 | 59 |
|
| 60 | + |
| 61 | + System.out.println("=== Data source: RDD ==="); |
59 | 62 | // Load a text file and convert each line to a Java Bean. |
60 | 63 | JavaRDD<Person> people = ctx.textFile("examples/src/main/resources/people.txt").map( |
61 | 64 | new Function<String, Person>() { |
@@ -84,16 +87,88 @@ public String call(Row row) { |
84 | 87 | return "Name: " + row.getString(0); |
85 | 88 | } |
86 | 89 | }).collect(); |
| 90 | + for (String name: teenagerNames) { |
| 91 | + System.out.println(name); |
| 92 | + } |
87 | 93 |
|
| 94 | + System.out.println("=== Data source: Parquet File ==="); |
88 | 95 | // JavaSchemaRDDs can be saved as parquet files, maintaining the schema information. |
89 | 96 | schemaPeople.saveAsParquetFile("people.parquet"); |
90 | 97 |
|
91 | | - // Read in the parquet file created above. Parquet files are self-describing so the schema is preserved. |
| 98 | + // Read in the parquet file created above. |
| 99 | + // Parquet files are self-describing so the schema is preserved. |
92 | 100 | // The result of loading a parquet file is also a JavaSchemaRDD. |
93 | 101 | JavaSchemaRDD parquetFile = sqlCtx.parquetFile("people.parquet"); |
94 | 102 |
|
95 | 103 | //Parquet files can also be registered as tables and then used in SQL statements. |
96 | 104 | parquetFile.registerAsTable("parquetFile"); |
97 | | - JavaSchemaRDD teenagers2 = sqlCtx.sql("SELECT name FROM parquetFile WHERE age >= 13 AND age <= 19"); |
| 105 | + JavaSchemaRDD teenagers2 = |
| 106 | + sqlCtx.sql("SELECT name FROM parquetFile WHERE age >= 13 AND age <= 19"); |
| 107 | + teenagerNames = teenagers2.map(new Function<Row, String>() { |
| 108 | + public String call(Row row) { |
| 109 | + return "Name: " + row.getString(0); |
| 110 | + } |
| 111 | + }).collect(); |
| 112 | + for (String name: teenagerNames) { |
| 113 | + System.out.println(name); |
| 114 | + } |
| 115 | + |
| 116 | + System.out.println("=== Data source: JSON Dataset ==="); |
| 117 | + // A JSON dataset is pointed by path. |
| 118 | + // The path can be either a single text file or a directory storing text files. |
| 119 | + String path = "examples/src/main/resources/people.json"; |
| 120 | + // Create a JavaSchemaRDD from the file(s) pointed by path |
| 121 | + JavaSchemaRDD peopleFromJsonFile = sqlCtx.jsonFile(path); |
| 122 | + |
| 123 | + // Because the schema of a JSON dataset is automatically inferred, to write queries, |
| 124 | + // it is better to take a look at what is the schema. |
| 125 | + peopleFromJsonFile.printSchema(); |
| 126 | + // The schema of people is ... |
| 127 | + // root |
| 128 | + // |-- age: IntegerType |
| 129 | + // |-- name: StringType |
| 130 | + |
| 131 | + // Register this JavaSchemaRDD as a table. |
| 132 | + peopleFromJsonFile.registerAsTable("people"); |
| 133 | + |
| 134 | + // SQL statements can be run by using the sql methods provided by sqlCtx. |
| 135 | + JavaSchemaRDD teenagers3 = sqlCtx.sql("SELECT name FROM people WHERE age >= 13 AND age <= 19"); |
| 136 | + |
| 137 | + // The results of SQL queries are JavaSchemaRDDs and support all the normal RDD operations. |
| 138 | + // The columns of a row in the result can be accessed by ordinal. |
| 139 | + teenagerNames = teenagers3.map(new Function<Row, String>() { |
| 140 | + public String call(Row row) { return "Name: " + row.getString(0); } |
| 141 | + }).collect(); |
| 142 | + for (String name: teenagerNames) { |
| 143 | + System.out.println(name); |
| 144 | + } |
| 145 | + |
| 146 | + // Alternatively, a JavaSchemaRDD can be created for a JSON dataset represented by |
| 147 | + // a RDD[String] storing one JSON object per string. |
| 148 | + List<String> jsonData = Arrays.asList( |
| 149 | + "{\"name\":\"Yin\",\"address\":{\"city\":\"Columbus\",\"state\":\"Ohio\"}}"); |
| 150 | + JavaRDD<String> anotherPeopleRDD = ctx.parallelize(jsonData); |
| 151 | + JavaSchemaRDD peopleFromJsonRDD = sqlCtx.jsonRDD(anotherPeopleRDD); |
| 152 | + |
| 153 | + // Take a look at the schema of this new JavaSchemaRDD. |
| 154 | + peopleFromJsonRDD.printSchema(); |
| 155 | + // The schema of anotherPeople is ... |
| 156 | + // root |
| 157 | + // |-- address: StructType |
| 158 | + // | |-- city: StringType |
| 159 | + // | |-- state: StringType |
| 160 | + // |-- name: StringType |
| 161 | + |
| 162 | + peopleFromJsonRDD.registerAsTable("people2"); |
| 163 | + |
| 164 | + JavaSchemaRDD peopleWithCity = sqlCtx.sql("SELECT name, address.city FROM people2"); |
| 165 | + List<String> nameAndCity = peopleWithCity.map(new Function<Row, String>() { |
| 166 | + public String call(Row row) { |
| 167 | + return "Name: " + row.getString(0) + ", City: " + row.getString(1); |
| 168 | + } |
| 169 | + }).collect(); |
| 170 | + for (String name: nameAndCity) { |
| 171 | + System.out.println(name); |
| 172 | + } |
98 | 173 | } |
99 | 174 | } |
0 commit comments