Spark window function Application (-)

Source: Internet
Author: User

/**
* @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 (-)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.