2017-03-12 1 views
0

Wie verknüpfe ich Blogposts mit Tags in Spark?Beiträge mit Hilfe von Spark Scala zu Tags zuordnen

val posts = Seq("BMW is a good car", 
"AUDI beats Tesla on speed race", 
"BMW exposes its new vehicle at Montreal", 
"Mercedes introduces beast offroad track") 

val rdd = sc.makeRDD(posts) 

val tags = Seq("BMW", "AUDI", "Mercedes") 

auf Daten So basiert oben ich RDD[(String, Iterable[String]] neu erhalten möchten:

("BMW", Iterable ("BMW ist ein gutes Auto", "BMW sein neues Fahrzeug in Montreal macht")

("AUDI", Iterable ("AUDI schlägt Tesla auf Speed-Rennen"))

wie es Irgendwelche Ideen ("Mercedes", Iterable() "Mercedes Tier Offroad-Strecke führt") konnte getan werden?

+0

In einfachsten Form rdd.filter (row => row.contains ("BMW")) .Map (line => ("BMW", line)) .groupByKey() . sammeln() – Pushkr

Antwort

2
// broadcast the tags 
val tags_broadcast = sc.broadcast(tags) 

// extract the tags each string contains in the rdd, make a pair rdd where the first element 
// is the tag and second element is the string, then call groupByKey method 
rdd.flatMap(s => tags_broadcast.value.filter(s.contains(_)).map((_, s))).groupByKey.collect 

// res110: Array[(String, Iterable[String])] = Array((AUDI,CompactBuffer(AUDI beats Tesla on speed race)), (BMW,CompactBuffer(BMW is a good car, BMW exposes its new vehicle at Montreal)), (Mercedes,CompactBuffer(Mercedes introduces beast offroad track))) 
Verwandte Themen