Skip to content

Commit 8f67b5b

Browse files
committed
added python sql example
1 parent 729952a commit 8f67b5b

File tree

1 file changed

+52
-0
lines changed
  • examples/src/main/python

1 file changed

+52
-0
lines changed

examples/src/main/python/sql.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#
2+
# Licensed to the Apache Software Foundation (ASF) under one or more
3+
# contributor license agreements. See the NOTICE file distributed with
4+
# this work for additional information regarding copyright ownership.
5+
# The ASF licenses this file to You under the Apache License, Version 2.0
6+
# (the "License"); you may not use this file except in compliance with
7+
# the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
18+
import sys
19+
20+
from pyspark import SparkContext
21+
from pyspark.sql import SQLContext
22+
23+
24+
if __name__ == "__main__":
25+
if len(sys.argv) != 2:
26+
print >> sys.stderr, "Usage: sql <file>"
27+
exit(-1)
28+
sc = SparkContext(appName="PythonWordCount")
29+
sqlContext = SQLContext(sc)
30+
31+
# A JSON dataset is pointed to by path.
32+
# The path can be either a single text file or a directory storing text files.
33+
path = "examples/src/main/resources/people.json"
34+
# Create a SchemaRDD from the file(s) pointed to by path
35+
people = sqlContext.jsonFile(path)
36+
37+
# The inferred schema can be visualized using the printSchema() method.
38+
people.printSchema()
39+
# root
40+
# |-- age: IntegerType
41+
# |-- name: StringType
42+
43+
# Register this SchemaRDD as a table.
44+
people.registerAsTable("people")
45+
46+
# SQL statements can be run by using the sql methods provided by sqlContext.
47+
teenagers = sqlContext.sql("SELECT name FROM people WHERE age >= 13 AND age <= 19")
48+
49+
for each in teenagers.collect():
50+
print each[0]
51+
52+
sc.stop()

0 commit comments

Comments
 (0)