RDD with (key, (key2, value))
I have an RDD in pyspark of the form (key, other things), where "other things" is a list of fields. I would like to get another RDD that uses a second key from the list of fields. For example, if my initial RDD is:
(User1, 1990 4 2 green...)
(User1, 1990 2 2 green...)
(User2, 1994 3 8 blue...)
(User1, 1987 3 4 blue...)
I would like to get (User1, [(1990, x), (1987, y)]),(User2, (1994 z))
where x, y, z would be an aggregation on the other fields, eg x is the count of how may rows I have with User1 and 1990 (two in this case), and I get a list with one tuple per year.
I am looking at the key value functions from:
https://www.oreilly.com/library/view/learning-spark/9781449359034/ch04.html
But don't seem to find anything that will give and aggregation twice: once for user and one for year. My initial attempt was with combineByKey() but I get stuck in getting a list from the values.
Any help would be appreciated!
pyspark rdd
add a comment |
I have an RDD in pyspark of the form (key, other things), where "other things" is a list of fields. I would like to get another RDD that uses a second key from the list of fields. For example, if my initial RDD is:
(User1, 1990 4 2 green...)
(User1, 1990 2 2 green...)
(User2, 1994 3 8 blue...)
(User1, 1987 3 4 blue...)
I would like to get (User1, [(1990, x), (1987, y)]),(User2, (1994 z))
where x, y, z would be an aggregation on the other fields, eg x is the count of how may rows I have with User1 and 1990 (two in this case), and I get a list with one tuple per year.
I am looking at the key value functions from:
https://www.oreilly.com/library/view/learning-spark/9781449359034/ch04.html
But don't seem to find anything that will give and aggregation twice: once for user and one for year. My initial attempt was with combineByKey() but I get stuck in getting a list from the values.
Any help would be appreciated!
pyspark rdd
add a comment |
I have an RDD in pyspark of the form (key, other things), where "other things" is a list of fields. I would like to get another RDD that uses a second key from the list of fields. For example, if my initial RDD is:
(User1, 1990 4 2 green...)
(User1, 1990 2 2 green...)
(User2, 1994 3 8 blue...)
(User1, 1987 3 4 blue...)
I would like to get (User1, [(1990, x), (1987, y)]),(User2, (1994 z))
where x, y, z would be an aggregation on the other fields, eg x is the count of how may rows I have with User1 and 1990 (two in this case), and I get a list with one tuple per year.
I am looking at the key value functions from:
https://www.oreilly.com/library/view/learning-spark/9781449359034/ch04.html
But don't seem to find anything that will give and aggregation twice: once for user and one for year. My initial attempt was with combineByKey() but I get stuck in getting a list from the values.
Any help would be appreciated!
pyspark rdd
I have an RDD in pyspark of the form (key, other things), where "other things" is a list of fields. I would like to get another RDD that uses a second key from the list of fields. For example, if my initial RDD is:
(User1, 1990 4 2 green...)
(User1, 1990 2 2 green...)
(User2, 1994 3 8 blue...)
(User1, 1987 3 4 blue...)
I would like to get (User1, [(1990, x), (1987, y)]),(User2, (1994 z))
where x, y, z would be an aggregation on the other fields, eg x is the count of how may rows I have with User1 and 1990 (two in this case), and I get a list with one tuple per year.
I am looking at the key value functions from:
https://www.oreilly.com/library/view/learning-spark/9781449359034/ch04.html
But don't seem to find anything that will give and aggregation twice: once for user and one for year. My initial attempt was with combineByKey() but I get stuck in getting a list from the values.
Any help would be appreciated!
pyspark rdd
pyspark rdd
asked Jan 1 at 10:55
PandaPanda
31
31
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You can do the following using groupby
:
# sample rdd
l = [("User1", "1990"),
("User1", "1990"),
("User2", "1994"),
("User1", "1987") ]
rd = sc.parallelize(l)
# returns a tuples of count of year
def f(l):
dd = {}
for i in l:
if i not in dd:
dd[i] =1
else:
dd[i]+=1
return list(dd.items())
# using groupby and applying the function on x[1] (which is a list)
rd1 = rd.groupByKey().map(lambda x : (x[0], f(x[1]))).collect()
[('User1', [('1990', 2), ('1987', 1)]), ('User2', [('1994', 1)])]
This works fine, thanks
– Panda
Jan 1 at 15:49
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53994865%2frdd-with-key-key2-value%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can do the following using groupby
:
# sample rdd
l = [("User1", "1990"),
("User1", "1990"),
("User2", "1994"),
("User1", "1987") ]
rd = sc.parallelize(l)
# returns a tuples of count of year
def f(l):
dd = {}
for i in l:
if i not in dd:
dd[i] =1
else:
dd[i]+=1
return list(dd.items())
# using groupby and applying the function on x[1] (which is a list)
rd1 = rd.groupByKey().map(lambda x : (x[0], f(x[1]))).collect()
[('User1', [('1990', 2), ('1987', 1)]), ('User2', [('1994', 1)])]
This works fine, thanks
– Panda
Jan 1 at 15:49
add a comment |
You can do the following using groupby
:
# sample rdd
l = [("User1", "1990"),
("User1", "1990"),
("User2", "1994"),
("User1", "1987") ]
rd = sc.parallelize(l)
# returns a tuples of count of year
def f(l):
dd = {}
for i in l:
if i not in dd:
dd[i] =1
else:
dd[i]+=1
return list(dd.items())
# using groupby and applying the function on x[1] (which is a list)
rd1 = rd.groupByKey().map(lambda x : (x[0], f(x[1]))).collect()
[('User1', [('1990', 2), ('1987', 1)]), ('User2', [('1994', 1)])]
This works fine, thanks
– Panda
Jan 1 at 15:49
add a comment |
You can do the following using groupby
:
# sample rdd
l = [("User1", "1990"),
("User1", "1990"),
("User2", "1994"),
("User1", "1987") ]
rd = sc.parallelize(l)
# returns a tuples of count of year
def f(l):
dd = {}
for i in l:
if i not in dd:
dd[i] =1
else:
dd[i]+=1
return list(dd.items())
# using groupby and applying the function on x[1] (which is a list)
rd1 = rd.groupByKey().map(lambda x : (x[0], f(x[1]))).collect()
[('User1', [('1990', 2), ('1987', 1)]), ('User2', [('1994', 1)])]
You can do the following using groupby
:
# sample rdd
l = [("User1", "1990"),
("User1", "1990"),
("User2", "1994"),
("User1", "1987") ]
rd = sc.parallelize(l)
# returns a tuples of count of year
def f(l):
dd = {}
for i in l:
if i not in dd:
dd[i] =1
else:
dd[i]+=1
return list(dd.items())
# using groupby and applying the function on x[1] (which is a list)
rd1 = rd.groupByKey().map(lambda x : (x[0], f(x[1]))).collect()
[('User1', [('1990', 2), ('1987', 1)]), ('User2', [('1994', 1)])]
answered Jan 1 at 11:45
YOLOYOLO
5,5001424
5,5001424
This works fine, thanks
– Panda
Jan 1 at 15:49
add a comment |
This works fine, thanks
– Panda
Jan 1 at 15:49
This works fine, thanks
– Panda
Jan 1 at 15:49
This works fine, thanks
– Panda
Jan 1 at 15:49
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53994865%2frdd-with-key-key2-value%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown