Hive Extract Data in a Array












1















I need to extract the 5th value from data string array in Hive,



arr = ("abc", "123-4567", "10", "ax", "cdpp asd", "00", "q", "na", "avail", "n", "n", "na")


How can I extract "cdpp asd" ie 5th value.



We can use SUBSTR, and INSTR but is there any other way to achieve this?










share|improve this question

























  • how are you supposed to extract a value from array by using substr or instr?

    – mangusta
    Nov 20 '18 at 7:49
















1















I need to extract the 5th value from data string array in Hive,



arr = ("abc", "123-4567", "10", "ax", "cdpp asd", "00", "q", "na", "avail", "n", "n", "na")


How can I extract "cdpp asd" ie 5th value.



We can use SUBSTR, and INSTR but is there any other way to achieve this?










share|improve this question

























  • how are you supposed to extract a value from array by using substr or instr?

    – mangusta
    Nov 20 '18 at 7:49














1












1








1








I need to extract the 5th value from data string array in Hive,



arr = ("abc", "123-4567", "10", "ax", "cdpp asd", "00", "q", "na", "avail", "n", "n", "na")


How can I extract "cdpp asd" ie 5th value.



We can use SUBSTR, and INSTR but is there any other way to achieve this?










share|improve this question
















I need to extract the 5th value from data string array in Hive,



arr = ("abc", "123-4567", "10", "ax", "cdpp asd", "00", "q", "na", "avail", "n", "n", "na")


How can I extract "cdpp asd" ie 5th value.



We can use SUBSTR, and INSTR but is there any other way to achieve this?







sql hive hiveql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 5:55









Aqueous Carlos

293213




293213










asked Nov 20 '18 at 5:50









BastianBastian

559




559













  • how are you supposed to extract a value from array by using substr or instr?

    – mangusta
    Nov 20 '18 at 7:49



















  • how are you supposed to extract a value from array by using substr or instr?

    – mangusta
    Nov 20 '18 at 7:49

















how are you supposed to extract a value from array by using substr or instr?

– mangusta
Nov 20 '18 at 7:49





how are you supposed to extract a value from array by using substr or instr?

– mangusta
Nov 20 '18 at 7:49












3 Answers
3






active

oldest

votes


















1














If your array is in string column then you can remove brackets and double quotes using regexp_replace and split resulted string to get an array using split():



 select split(regexp_replace('("abc", "123-4567", "10", "ax",   "cdpp asd", "00", "q", "na", "avail", "n", "n",   "na")','^\(|\)$|"',''),', *')[4];
OK
cdpp asd





share|improve this answer



















  • 1





    Thanks it helped !!!

    – Bastian
    Nov 22 '18 at 18:21











  • @user2452689 You are welcome!

    – leftjoin
    Nov 22 '18 at 18:24



















1














arr = ("abc", "123-4567", "10", "ax", "cdpp asd", "00", "q", "na", "avail", "n", "n", "na")

Select arr[4] from tablename;


Output:



cdpp asd





share|improve this answer

































    0














    1、Maybe you can try to write a UDF to cast this string to an Array arr, then you can use arr[4] to visit the 5th value;



    2、Or you can use the following way to get the 5th value:



        select tf.* from (
    select regexp_replace('("abc", "123-4567", "10", "ax", "cdpp asd", "00", "q", "na", "avail", "n", "n", "na")','\(|\)|"','') as str
    ) t lateral view posexplode(split(str,', ')) tf as pos,val
    where tf.pos = 4;


    Note: this way require the array string has no brackets.






    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',
      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%2f53386979%2fhive-extract-data-in-a-array%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      If your array is in string column then you can remove brackets and double quotes using regexp_replace and split resulted string to get an array using split():



       select split(regexp_replace('("abc", "123-4567", "10", "ax",   "cdpp asd", "00", "q", "na", "avail", "n", "n",   "na")','^\(|\)$|"',''),', *')[4];
      OK
      cdpp asd





      share|improve this answer



















      • 1





        Thanks it helped !!!

        – Bastian
        Nov 22 '18 at 18:21











      • @user2452689 You are welcome!

        – leftjoin
        Nov 22 '18 at 18:24
















      1














      If your array is in string column then you can remove brackets and double quotes using regexp_replace and split resulted string to get an array using split():



       select split(regexp_replace('("abc", "123-4567", "10", "ax",   "cdpp asd", "00", "q", "na", "avail", "n", "n",   "na")','^\(|\)$|"',''),', *')[4];
      OK
      cdpp asd





      share|improve this answer



















      • 1





        Thanks it helped !!!

        – Bastian
        Nov 22 '18 at 18:21











      • @user2452689 You are welcome!

        – leftjoin
        Nov 22 '18 at 18:24














      1












      1








      1







      If your array is in string column then you can remove brackets and double quotes using regexp_replace and split resulted string to get an array using split():



       select split(regexp_replace('("abc", "123-4567", "10", "ax",   "cdpp asd", "00", "q", "na", "avail", "n", "n",   "na")','^\(|\)$|"',''),', *')[4];
      OK
      cdpp asd





      share|improve this answer













      If your array is in string column then you can remove brackets and double quotes using regexp_replace and split resulted string to get an array using split():



       select split(regexp_replace('("abc", "123-4567", "10", "ax",   "cdpp asd", "00", "q", "na", "avail", "n", "n",   "na")','^\(|\)$|"',''),', *')[4];
      OK
      cdpp asd






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Nov 20 '18 at 12:01









      leftjoinleftjoin

      8,35922051




      8,35922051








      • 1





        Thanks it helped !!!

        – Bastian
        Nov 22 '18 at 18:21











      • @user2452689 You are welcome!

        – leftjoin
        Nov 22 '18 at 18:24














      • 1





        Thanks it helped !!!

        – Bastian
        Nov 22 '18 at 18:21











      • @user2452689 You are welcome!

        – leftjoin
        Nov 22 '18 at 18:24








      1




      1





      Thanks it helped !!!

      – Bastian
      Nov 22 '18 at 18:21





      Thanks it helped !!!

      – Bastian
      Nov 22 '18 at 18:21













      @user2452689 You are welcome!

      – leftjoin
      Nov 22 '18 at 18:24





      @user2452689 You are welcome!

      – leftjoin
      Nov 22 '18 at 18:24













      1














      arr = ("abc", "123-4567", "10", "ax", "cdpp asd", "00", "q", "na", "avail", "n", "n", "na")

      Select arr[4] from tablename;


      Output:



      cdpp asd





      share|improve this answer






























        1














        arr = ("abc", "123-4567", "10", "ax", "cdpp asd", "00", "q", "na", "avail", "n", "n", "na")

        Select arr[4] from tablename;


        Output:



        cdpp asd





        share|improve this answer




























          1












          1








          1







          arr = ("abc", "123-4567", "10", "ax", "cdpp asd", "00", "q", "na", "avail", "n", "n", "na")

          Select arr[4] from tablename;


          Output:



          cdpp asd





          share|improve this answer















          arr = ("abc", "123-4567", "10", "ax", "cdpp asd", "00", "q", "na", "avail", "n", "n", "na")

          Select arr[4] from tablename;


          Output:



          cdpp asd






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 20 '18 at 8:07









          Nick

          25.9k101836




          25.9k101836










          answered Nov 20 '18 at 7:13









          NareshNaresh

          113




          113























              0














              1、Maybe you can try to write a UDF to cast this string to an Array arr, then you can use arr[4] to visit the 5th value;



              2、Or you can use the following way to get the 5th value:



                  select tf.* from (
              select regexp_replace('("abc", "123-4567", "10", "ax", "cdpp asd", "00", "q", "na", "avail", "n", "n", "na")','\(|\)|"','') as str
              ) t lateral view posexplode(split(str,', ')) tf as pos,val
              where tf.pos = 4;


              Note: this way require the array string has no brackets.






              share|improve this answer




























                0














                1、Maybe you can try to write a UDF to cast this string to an Array arr, then you can use arr[4] to visit the 5th value;



                2、Or you can use the following way to get the 5th value:



                    select tf.* from (
                select regexp_replace('("abc", "123-4567", "10", "ax", "cdpp asd", "00", "q", "na", "avail", "n", "n", "na")','\(|\)|"','') as str
                ) t lateral view posexplode(split(str,', ')) tf as pos,val
                where tf.pos = 4;


                Note: this way require the array string has no brackets.






                share|improve this answer


























                  0












                  0








                  0







                  1、Maybe you can try to write a UDF to cast this string to an Array arr, then you can use arr[4] to visit the 5th value;



                  2、Or you can use the following way to get the 5th value:



                      select tf.* from (
                  select regexp_replace('("abc", "123-4567", "10", "ax", "cdpp asd", "00", "q", "na", "avail", "n", "n", "na")','\(|\)|"','') as str
                  ) t lateral view posexplode(split(str,', ')) tf as pos,val
                  where tf.pos = 4;


                  Note: this way require the array string has no brackets.






                  share|improve this answer













                  1、Maybe you can try to write a UDF to cast this string to an Array arr, then you can use arr[4] to visit the 5th value;



                  2、Or you can use the following way to get the 5th value:



                      select tf.* from (
                  select regexp_replace('("abc", "123-4567", "10", "ax", "cdpp asd", "00", "q", "na", "avail", "n", "n", "na")','\(|\)|"','') as str
                  ) t lateral view posexplode(split(str,', ')) tf as pos,val
                  where tf.pos = 4;


                  Note: this way require the array string has no brackets.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 20 '18 at 11:25









                  StrongYoungStrongYoung

                  3571213




                  3571213






























                      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%2f53386979%2fhive-extract-data-in-a-array%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

                      MongoDB - Not Authorized To Execute Command

                      in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

                      How to fix TextFormField cause rebuild widget in Flutter