How to add sparse vectors after group by, using Spark SQL?

Viewed 1142

I am doing a News recommendation system and I need to build a table for users and news they read. my raw data just like this :

001436800277225 ["9161492","9161787","9378531"]
009092130698762 ["9394697"]
010003000431538 ["9394697","9426473","9428530"]
010156461231357 ["9350394","9414181"]
010216216021063 ["9173862","9247870"]
010720006581483 ["9018786"]
011199797794333 ["9017977","9091134","9142852","9325464","9331913"]
011337201765123 ["9161294","9198693"]
011414545455156 ["9168185","9178348","9182782","9359776"]
011425002581540 ["9083446","9161294","9309432"]

and I use spark-SQL do explode and one hot encoding,

df = getdf()
df1 = df.select('uuid',explode('news').alias('news'))
stringIndexer = StringIndexer(inputCol="news", outputCol="newsIndex")
model = stringIndexer.fit(df1)
indexed = model.transform(df1)
encoder = OneHotEncoder(inputCol="newsIndex", outputCol="newsVec")
encoded = encoder.transform(indexed)
encoded.show(20,False)

After that, my data become:

+---------------+-------+---------+----------------------+
|uuid           |news   |newsIndex|newsVec               |
+---------------+-------+---------+----------------------+
|014324000386050|9398253|10415.0  |(105721,[10415],[1.0])|
|014324000386050|9428530|70.0     |(105721,[70],[1.0])   |
|014324000631752|654112 |1717.0   |(105721,[1717],[1.0]) |
|014324000674240|730531 |2282.0   |(105721,[2282],[1.0]) |
|014324000674240|694306 |1268.0   |(105721,[1268],[1.0]) |
|014324000674240|712016 |4766.0   |(105721,[4766],[1.0]) |
|014324000674240|672307 |7318.0   |(105721,[7318],[1.0]) |
|014324000674240|698073 |1241.0   |(105721,[1241],[1.0]) |
|014324000674240|728044 |5302.0   |(105721,[5302],[1.0]) |
|014324000674240|672256 |1619.0   |(105721,[1619],[1.0]) |
|014324000674240|730236 |2376.0   |(105721,[2376],[1.0]) |
|014324000674240|730235 |14274.0  |(105721,[14274],[1.0])|
|014324000674240|728509 |1743.0   |(105721,[1743],[1.0]) |
|014324000674240|704528 |10310.0  |(105721,[10310],[1.0])|
|014324000715399|774134 |8876.0   |(105721,[8876],[1.0]) |
|014324000725836|9357431|3479.0   |(105721,[3479],[1.0]) |
|014324000725836|9358028|15621.0  |(105721,[15621],[1.0])|
|014324000730349|812106 |4599.0   |(105721,[4599],[1.0]) |
|014324000730349|699237 |754.0    |(105721,[754],[1.0])  |
|014324000730349|748109 |4854.0   |(105721,[4854],[1.0]) |
+---------------+-------+---------+----------------------+

But one id have multiple rows, so I want to groupBy('uuid') and then add these vectors. But just use groupBy and then add will have error. How could I do that?

1 Answers
Related