RDD & Java Class (reflection) Build Dataframe

Source: Internet
Author: User
Import org.apache.spark.SparkConf Import org.apache.spark.SparkContext import Org.apache.spark.sql.SQLContext Object
    Rdd2dataframebyreflectionscala {case class person (name:string, Age:int) def main (args:array[string]): unit = { Val conf = new sparkconf ()//Create sparkconf Object Conf.setappname ("My Top Spark App")//Set the name of the application in the monitor page where the program runs can see the name con F.setmaster ("local") val sc = new Sparkcontext (conf) val sqlcontext = new SqlContext (SC) Import Sqlcontext.imp Licits._//From TXT read data to match the property defined by the person class String Integer val people = sc.textfile ("Peoples.txt"). Map (_.SPL
    
    
    It (",")). Map (P => Person (P (1), P (2). Trim.toint)). TODF () people.registertemptable ("People") Val teenagers = sqlcontext.sql ("SELECT name, age from People WHERE age >= 6 and age <= 19")/** * against Dat
    
    Aframe using the map operator, the return type is rdd<row>/teenagers.map (t => "Name:" + t (0)). Collect (). foreach (println) or by Field Name:teeNagers.map (t => "name:" + t.getas[string] ("name")). Collect (). foreach (println)}}
 

Related Article

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.