/**
* @author DT Big Data Dream Factory
* Sina Weibo http://weibo.com/ilovepains/
* Public Account: Dt_spark
* Live Address yy channel: 68917580
*/
Object Sparksqlwindowfunctionops {
def main (args:array[string]) {
/**
* Create a Sparkconf object to set configuration information for the Spark Program runtime
* For example, using Setmaster to set the URL of the master of the Spark Cluster to which the program is linked, if set to local, it represents
* Spark Program is running locally, suitable for beginners in general machine configuration
*/
Val conf = new sparkconf (). Setappname ("Sparksqlwindowfunctionops"). Setmaster ("spark://hadoop2001:7077")
/**
* Create Sparkcontext objects
* The Sparkcontext object is the only entry for all the functions of the Spark program, whether Scala, Java, Python, etc. must have a sparkcontext.
* Sparkcontext's core role: initializing the core components required for the spark application to run, including Dagscheduler,taskscheduler,schedulerbackend
* Colleagues will also be responsible for the spark program to master registration program, etc.
* Sparkcontext is one of the most critical objects in the entire spark application
*/
Val sc = new Sparkcontext (conf)
Val hivecontext = new Hivecontext (SC)
Hivecontext.sql ("Use Hive")
Hivecontext.sql ("DROP TABLE IF EXISTS scores")
Hivecontext.sql ("CREATE TABLE IF not EXISTS scores (name String,score INT)"
+ "ROW FORMAT delimited TERMINATED by ' LINES TERMINATED by ' \\n '")
Hivecontext.sql ("LOAD DATA LOCAL inpath '/root/test/testdate/topngroup.txt ' into TABLE scores")
/**
* Use sub-query method to complete the extraction of target data, using the window function row_number within the target data to group sorting:
* PARTITION by: Specify Key for window function grouping
* ORDER BY: Sorting by group
*/
Val result = Hivecontext.sql ("Select Name,score" +
"From (" +
"Select Name,score," +
"Row_number () over (PARTITION by name ORDER by score DESC) rank" +
"From scores" +
") Sub_scores" +
"WHERE rank <= 4")
Result.show ()//print out the results on the driver console
Hivecontext.sql ("DROP TABLE IF EXISTS sortedresultscores")
Result.saveastable ("Sortedresultscores")
}
}
DT Big Data DreamWorks was built by Liaoliang and his team to nurture 1 million of the best big data talent in the community, and Spark is now the mainstream data processing framework and future trend for the big business.
Focus on DT Big Data Dream Factory public number:
Dt_spark
Check out the free public lessons for absolutely detailed information.
yy Perpetual free live channel: 68917580
Liaoliang Teacher Contact Information:
Spark window function Application (-)