How to select max timestamp in a partition using Cassandra











up vote
1
down vote

favorite












I have a problem modeling my data using Cassandra. I would like to use it as an event store. My events have creation timestamp. Those event belong to a partition which is identified by an id.



Now I'd like to see most recent event for each id and then filter this ids according to the timestamp.



So I have something like this:



ID | CREATION_TIMESTAMP              | CONTENT
---+---------------------------------+----------------
1 | 2018-11-09 12:15:45.841000+0000 | {SOME_CONTENT}
1 | 2018-11-09 12:15:55.654656+0000 | {SOME_CONTENT}
2 | 2018-11-09 12:15:35.982354+0000 | {SOME_CONTENT}
2 | 2018-11-09 12:35:25.321655+0000 | {SOME_CONTENT}
2 | 2018-11-09 13:15:15.068498+0000 | {SOME_CONTENT}


I tried grouping by partition id and querying for max of creation_timestamp but that is not allowed and I should specify partition id using EQ or IN. Additional reading led me to believe that this is entirely wrong way of approaching this problem but I don't know whether NoSQL is not suitable tool for the job or I am simply approaching this problem from wrong angle?










share|improve this question




























    up vote
    1
    down vote

    favorite












    I have a problem modeling my data using Cassandra. I would like to use it as an event store. My events have creation timestamp. Those event belong to a partition which is identified by an id.



    Now I'd like to see most recent event for each id and then filter this ids according to the timestamp.



    So I have something like this:



    ID | CREATION_TIMESTAMP              | CONTENT
    ---+---------------------------------+----------------
    1 | 2018-11-09 12:15:45.841000+0000 | {SOME_CONTENT}
    1 | 2018-11-09 12:15:55.654656+0000 | {SOME_CONTENT}
    2 | 2018-11-09 12:15:35.982354+0000 | {SOME_CONTENT}
    2 | 2018-11-09 12:35:25.321655+0000 | {SOME_CONTENT}
    2 | 2018-11-09 13:15:15.068498+0000 | {SOME_CONTENT}


    I tried grouping by partition id and querying for max of creation_timestamp but that is not allowed and I should specify partition id using EQ or IN. Additional reading led me to believe that this is entirely wrong way of approaching this problem but I don't know whether NoSQL is not suitable tool for the job or I am simply approaching this problem from wrong angle?










    share|improve this question


























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I have a problem modeling my data using Cassandra. I would like to use it as an event store. My events have creation timestamp. Those event belong to a partition which is identified by an id.



      Now I'd like to see most recent event for each id and then filter this ids according to the timestamp.



      So I have something like this:



      ID | CREATION_TIMESTAMP              | CONTENT
      ---+---------------------------------+----------------
      1 | 2018-11-09 12:15:45.841000+0000 | {SOME_CONTENT}
      1 | 2018-11-09 12:15:55.654656+0000 | {SOME_CONTENT}
      2 | 2018-11-09 12:15:35.982354+0000 | {SOME_CONTENT}
      2 | 2018-11-09 12:35:25.321655+0000 | {SOME_CONTENT}
      2 | 2018-11-09 13:15:15.068498+0000 | {SOME_CONTENT}


      I tried grouping by partition id and querying for max of creation_timestamp but that is not allowed and I should specify partition id using EQ or IN. Additional reading led me to believe that this is entirely wrong way of approaching this problem but I don't know whether NoSQL is not suitable tool for the job or I am simply approaching this problem from wrong angle?










      share|improve this question















      I have a problem modeling my data using Cassandra. I would like to use it as an event store. My events have creation timestamp. Those event belong to a partition which is identified by an id.



      Now I'd like to see most recent event for each id and then filter this ids according to the timestamp.



      So I have something like this:



      ID | CREATION_TIMESTAMP              | CONTENT
      ---+---------------------------------+----------------
      1 | 2018-11-09 12:15:45.841000+0000 | {SOME_CONTENT}
      1 | 2018-11-09 12:15:55.654656+0000 | {SOME_CONTENT}
      2 | 2018-11-09 12:15:35.982354+0000 | {SOME_CONTENT}
      2 | 2018-11-09 12:35:25.321655+0000 | {SOME_CONTENT}
      2 | 2018-11-09 13:15:15.068498+0000 | {SOME_CONTENT}


      I tried grouping by partition id and querying for max of creation_timestamp but that is not allowed and I should specify partition id using EQ or IN. Additional reading led me to believe that this is entirely wrong way of approaching this problem but I don't know whether NoSQL is not suitable tool for the job or I am simply approaching this problem from wrong angle?







      cassandra nosql






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 23 at 16:00









      marc_s

      566k12610931245




      566k12610931245










      asked Nov 9 at 13:04









      Sok Pomaranczowy

      4252624




      4252624
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          3
          down vote













          You can easily achieve this by having your CREATION_TIMESTAMP as clustering column and ordered DESC. Then you would query by your id and using limit 1 (which will return the most recent event since the data is order DESC in that partition key).






          share|improve this answer




























            up vote
            0
            down vote













            can you please share your table definition .



            by looking at your data you can use ID as partition key and CREATION_TIMESTAMP as clustering column.



            you can use select MAX(CREATION_TIMESTAMP) from keyspace.table where ID='value';






            share|improve this answer





















              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',
              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%2f53226263%2fhow-to-select-max-timestamp-in-a-partition-using-cassandra%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              3
              down vote













              You can easily achieve this by having your CREATION_TIMESTAMP as clustering column and ordered DESC. Then you would query by your id and using limit 1 (which will return the most recent event since the data is order DESC in that partition key).






              share|improve this answer

























                up vote
                3
                down vote













                You can easily achieve this by having your CREATION_TIMESTAMP as clustering column and ordered DESC. Then you would query by your id and using limit 1 (which will return the most recent event since the data is order DESC in that partition key).






                share|improve this answer























                  up vote
                  3
                  down vote










                  up vote
                  3
                  down vote









                  You can easily achieve this by having your CREATION_TIMESTAMP as clustering column and ordered DESC. Then you would query by your id and using limit 1 (which will return the most recent event since the data is order DESC in that partition key).






                  share|improve this answer












                  You can easily achieve this by having your CREATION_TIMESTAMP as clustering column and ordered DESC. Then you would query by your id and using limit 1 (which will return the most recent event since the data is order DESC in that partition key).







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 9 at 13:16









                  Horia

                  2,140213




                  2,140213
























                      up vote
                      0
                      down vote













                      can you please share your table definition .



                      by looking at your data you can use ID as partition key and CREATION_TIMESTAMP as clustering column.



                      you can use select MAX(CREATION_TIMESTAMP) from keyspace.table where ID='value';






                      share|improve this answer

























                        up vote
                        0
                        down vote













                        can you please share your table definition .



                        by looking at your data you can use ID as partition key and CREATION_TIMESTAMP as clustering column.



                        you can use select MAX(CREATION_TIMESTAMP) from keyspace.table where ID='value';






                        share|improve this answer























                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          can you please share your table definition .



                          by looking at your data you can use ID as partition key and CREATION_TIMESTAMP as clustering column.



                          you can use select MAX(CREATION_TIMESTAMP) from keyspace.table where ID='value';






                          share|improve this answer












                          can you please share your table definition .



                          by looking at your data you can use ID as partition key and CREATION_TIMESTAMP as clustering column.



                          you can use select MAX(CREATION_TIMESTAMP) from keyspace.table where ID='value';







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 11 at 5:12









                          Jay Tokala

                          161




                          161






























                               

                              draft saved


                              draft discarded



















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53226263%2fhow-to-select-max-timestamp-in-a-partition-using-cassandra%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

                              Schultheiß

                              Android Play Services Check

                              Where to put API Key in Google Cloud Vision for PHP