Scala dataframe Generation Tips

Source: Internet
Author: User
Tags arrays
Simple conversion of case1:list () to Dataframe ()
Step1: We first create a case class
Case Class ResultSet (Masterhotel:int,
Quantity:double,
Date:string,
Rank:int,
Frcst_cii:double,
Hotelid:int)

Step2
Initialize the ResultSet class, there are many ways to get the data definition ResultSet class from the relational database,
Direct definition of a resultset list, etc.
Val x1=list (ResultSet (1001,12, "2016-10-01", 1, 13.44,1001),
ResultSet (1002,12, "2016-10-01", 3, 13.44,1002),
ResultSet (1004,15, "2016-10-02", 10, 18.44,1004),
ResultSet (1005,5, "2016-10-02", 40, 5.67,1005)
)
Val Dataset1=sqlcontext.createdataframe (x1)
Scala> Dataset1.show ()
+-----------+--------+----------+----+---------+-------+
|masterhotel|quantity| date|rank|frcst_cii|hotelid|
+-----------+--------+----------+----+---------+-------+
|    1001|   12.0|2016-10-01|    1|   13.44| 1001|
|    1002|   12.0|2016-10-01|    3|   13.44| 1002|
|    1004|  15.0|2016-10-02|    10|   18.44| 1004|
|     1005|  5.0|2016-10-02|     40|   5.67| 1005|
+-----------+--------+----------+----+---------+-------+

CASE2: How an array of tuple elements is converted to a list, then the class is converted to Dataframe ()
Val X2=array (1001,12, "2016-10-01", 1, 13.44,1001),
(1002,12, "2016-10-01", 3, 13.44,1002),
(1004,15, "2016-10-02", 10, 18.44,1004),
(1005,5, "2016-10-02", 40, 5.67,1005)
)
Val x3= (0 until X2.length). Map (i = resultset (x2 (i). _1,X2 (i) _2,x2 (i). _3,X2 (i). _4,X2 (i). _5,X2 (i). _6)//How to access the tuple
Val X4=x3.tolist
Val Dataset2=sqlcontext.createdataframe (x4)
Scala> Dataset2.show ()
+-----------+--------+----------+----+---------+-------+
|masterhotel|quantity| date|rank|frcst_cii|hotelid|
+-----------+--------+----------+----+---------+-------+
|    1001|   12.0|2016-10-01|    1|   13.44| 1001|
|    1002|   12.0|2016-10-01|    3|   13.44| 1002|
|    1004|  15.0|2016-10-02|    10|   18.44| 1004|
|     1005|  5.0|2016-10-02|     40|   5.67| 1005|
+-----------+--------+----------+----+---------+-------+

CASE3: Arrays and lists can be converted to each other
Scala> Val A=array. toList
A:list[int] = List (1, 2)
Scala> A.toarray

Res69:array[int] = Array (1, 2)


Case4:dataframe () can be converted directly into an array of arrays in CAE2

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.