I am learning spark and want to work on the intersection of all values in the file
The format of the file looks like the following:
a
1, 2, 3, 0, ...
b
0, 5, 20, 3, ...
c
0, 7, 9, 10, 2, 20, ...
d
empty
e
empty
I tried doing the following:
rdd = spark.sparkContext.textFile('data.txt')
rdd1 = rdd.map(lambda x: x.split('\t')).map(lambda x: (x[0], x[1])).map(lambda x : (x[0], list(x[1].split('\n,'))))
ab = rdd1.map(lambda x: (x[0], (x[1]))).reduceByKey(lambda x, y: (set(x[0]))).map(lambda x: (x[0], list(set(x[1]))))
And I now have the data in the following format as key-value pairs.
[('a', [1, 2, 3, 0, ...]), ('b', [0, 5, 20, 3,...]), ('c', [0, 7, 9, 10, 2, 20, ...]), ...]
I need to find the intersection of each value and other values in the data and attach the keys where the length of the intersection if >= 2.
Like:
[key, [list of keys in the entire data whose length of intersections with the current key is >=2]]
For example, the values of key a has intersection [0, 3] with values of key b. values of Key a also has intersection [0, 2] with values of key b. Similarly, for key b, same thing should happen in the case of a and b. Then b and c have intersection [0, 20]. Finally, d will be assigned e and vice versa because both are empty.
Sample output:
[('a', [b, c]), ('b', [a, c]), ('c', [a, b]), ('d', [e]), ('e', [d])]