I have two tables: Table A
|Group ID | User ids in group|
| -------- | -------------- |
| 11 | [45,46,47,48] |
| 20 | [49,10,11,12] |
| 31 | [55,7,48,43] |
and Table B:
| User ids| Related Id |
| ------- | -------------- |
| 1 | [5,6,7,8] |
| 2 | [6, 9, 10,11] |
| 3 | [1, 2, 5, 7] |
And I have a reference table that has the info: Reference table:
| User ids | Group ID |
| -------- | -------------- |
| 1 | 11 |
| 2 | 20 |
| 3 | 31 |
This is just a minimal sample, I have this situation with millions of rows on each table. I am trying to use pyspark (or sql but I haven't figured out a way to do it here) to iterate through User ids column in the reference table and get the intersection between the lists of User ids in group from Table A and Related Id from Table B.
So in the end, I would like to have a table of the form:
| User ids | Intersection |
| -------- | -------------- |
| 2 | [10, 11] |
| 3 | [7] |
In Pyspark Id have a function of the form:
def test_function(user_id, ref_df, tableB_df, tableA_df):
group_id = int(ref_df.filter(ref_df.userID == user_id).collect()[0][1])
group_list = tableA_df.filter(tableA_df.groupID == group_id)
related_id_list = tableB_df.filter(tableB_df.userID == user_id)
return group_list.intsersection(related_id_list)
abc = ref_df.rdd.map(lambda x: test_function(x, ref_df, tableB_df, tableA_df))
However, when I run this function I am getting the following error:
An error was encountered: Could not serialize object: TypeError: can't pickle _thread.RLock objects
Can anyone give any suggestion on how to solve this problem? Or how to modify my approach to solve this problem? Since my table has millions of rows, I want to use pyspark as best as possible to make use of the parallelization abilities as much as possible. Thanks for all your help.