RDD with (key, (key2, value))












-1















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!










share|improve this question



























    -1















    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!










    share|improve this question

























      -1












      -1








      -1








      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!










      share|improve this question














      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 1 at 10:55









      PandaPanda

      31




      31
























          1 Answer
          1






          active

          oldest

          votes


















          0














          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)])]





          share|improve this answer
























          • This works fine, thanks

            – Panda
            Jan 1 at 15:49











          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
          });


          }
          });














          draft saved

          draft discarded


















          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









          0














          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)])]





          share|improve this answer
























          • This works fine, thanks

            – Panda
            Jan 1 at 15:49
















          0














          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)])]





          share|improve this answer
























          • This works fine, thanks

            – Panda
            Jan 1 at 15:49














          0












          0








          0







          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)])]





          share|improve this answer













          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)])]






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 1 at 11:45









          YOLOYOLO

          5,5001424




          5,5001424













          • 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





          This works fine, thanks

          – Panda
          Jan 1 at 15:49




















          draft saved

          draft discarded




















































          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.




          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          Monofisismo

          Angular Downloading a file using contenturl with Basic Authentication

          Olmecas