The format of input data as follows:
+--------------------+-------------+--------------------+
| date | user | product |
+--------------------+-------------+--------------------+
| 2016-10-01 | Tom | computer |
+--------------------+-------------+--------------------+
| 2016-10-01 | Tom | iphone |
+--------------------+-------------+--------------------+
| 2016-10-01 | Jhon | book |
+--------------------+-------------+--------------------+
| 2016-10-02 | Tom | pen |
+--------------------+-------------+--------------------+
| 2016-10-02 | Jhon | milk |
+--------------------+-------------+--------------------+
And the format of output as follows:
+-----------+-----------------------+
| user | products |
+-----------------------------------+
| Tom | computer,iphone,pen |
+-----------------------------------+
| Jhon | book,milk |
+-----------------------------------+
The output shows all products every user bought order by date.
I want to process these data using Spark, who Can you help me, please? Thank you.
Better to use map-reduceBykey() combination rather than groupBy.. Also assuming the data doesn't have the
#Read the data using val ordersRDD = sc.textFile("/file/path")
val ordersRDD = sc.parallelize( List(("2016-10-01","Tom","computer"),
("2016-10-01","Tom","iphone"),
("2016-10-01","Jhon","book"),
("2016-10-02","Tom","pen"),
("2016-10-02","Jhon","milk")))
#group by (date, user), sort by key & reduce by user & concatenate products
val dtusrGrpRDD = ordersRDD.map(rec => ((rec._2, rec._1), rec._3))
.sortByKey().map(x=>(x._1._1, x._2))
.reduceByKey((acc, v) => acc+","+v)
#if needed, make it to DF
scala> dtusrGrpRDD.toDF("user", "product").show()
+----+-------------------+
|user| product|
+----+-------------------+
| Tom|computer,iphone,pen|
|Jhon| book,milk|
+----+-------------------+
If you are using a HiveContext (which you should be):
Example using python:
from pyspark.sql.functions import collect_set
df = ... load your df ...
new_df = df.groupBy("user").agg(collect_set("product").alias("products"))
If you don't want the resulting list in products deduped, you can use collect_list instead.
For dataframes it is two-liner:
import org.apache.spark.sql.functions.collect_list
//collect_set nistead of collect_list if you don't want duplicates
val output = join.groupBy("user").agg(collect_list($"product"))
GroupBy will give you a grouped user set post which you can iterate and collect_list or collect_set on the grouped dataset.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.