[Spark] [Python] Mapping single Rows to multiple Pairs

Source: Internet
Author: User

Mapping single Rows to multiple Pairs
Objective:

Take the following data,

Input Data

00001 sku010:sku933:sku022
00002 sku912:sku331
00003 sku888:sku022:sku010:sku594
00004 sku411


Convert to this:
A key value, with these several key values, respectively, listed:

(00001,sk010)
(00001,sku933)
(00001,sku022)

...
(00002,sku912)
(00002,sku331)
(00003,sku888)

This is called the Mapping single Rows to multiple Pairs

The steps are as follows:

[Email protected] ~]$ vim Act001.txt
[Email protected] ~]$
[email protected] ~]$ cat Act001.txt
00001ku010:sku933:sku022
00002sku912:sku331
00003sku888:sku022:sku010:sku594
00004sku411
[Email protected] ~]$ HDFs dfs-put act001.txt
[Email protected] ~]$
[Email protected] ~]$ HDFs dfs-cat act001.txt
00001ku010:sku933:sku022
00002sku912:sku331
00003sku888:sku022:sku010:sku594
00004sku411
[Email protected] ~]$

In [6]: Mydata01=mydata.map (lambda line:line.split ("T"))

In [7]: Type (MYDATA01)
OUT[7]: Pyspark.rdd.PipelinedRDD

In [8]: Mydata02=mydata01.map (lambda fields: (Fields[0],fields[1]))

In [9]: type (MYDATA02)
OUT[9]: Pyspark.rdd.PipelinedRDD

In [10]:

In [all]: Mydata03 = mydata02.flatmapvalues (Lambda skus:skus.split (":"))

In []: type (MYDATA03)
OUT[12]: Pyspark.rdd.PipelinedRDD

in [+]: Mydata03.take (1)
OUT[13]: [(U ' 00001 ', U ' ku010 ')]

[Spark] [Python] Mapping single Rows to multiple Pairs

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.