Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8533929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:04:41+00:00 2026-06-11T10:04:41+00:00

Hive has this pretty nice Array type that is very useful in theory but

  • 0

Hive has this pretty nice Array type that is very useful in theory but when it comes to practice I found very little information on how to do any kind of opeartions with it.
We store a serie of numbers in an array type column and need to SUM them in a query, preferably from n-th to m-th element. Is it possible with standard HiveQL or does it require a UDF or customer mapper/reducer?

Note: we’re using Hive 0.8.1 in EMR environment.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T10:04:43+00:00Added an answer on June 11, 2026 at 10:04 am

    I’d write a simple UDF for this purpose. You need to have hive-exec in your build path.
    E.g In case of Maven:

    <dependency>
      <groupId>org.apache.hive</groupId>
      <artifactId>hive-exec</artifactId>
      <version>0.8.1</version>
    </dependency>
    

    A simple raw implementation would look like this:

    package com.myexample;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.apache.hadoop.hive.ql.exec.UDF;
    import org.apache.hadoop.io.IntWritable;
    
    public class SubArraySum extends UDF {
    
        public IntWritable evaluate(ArrayList<Integer> list, 
          IntWritable from, IntWritable to) {
            IntWritable result = new IntWritable(-1);
            if (list == null || list.size() < 1) {
                return result;
            }
    
            int m = from.get();
            int n = to.get();
    
            //m: inclusive, n:exclusive
            List<Integer> subList = list.subList(m, n);
    
            int sum = 0;
            for (Integer i : subList) {
                sum += i;
            }
            result.set(sum);
            return result;
        }
    }
    

    Next, build a jar and load it in Hive shell:

    hive> add jar /home/user/jar/myjar.jar;
    hive> create temporary function subarraysum as 'com.myexample.SubArraySum';
    

    Now you can use it to calculate the sum of the array you have.

    E.g:

    Let’s assume that you have an input file having tab-separated columns in it :

    1   0,1,2,3,4
    2   5,6,7,8,9
    

    Load it into mytable:

    hive> create external table mytable (
      id int,
      nums array<int>
    )
    ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'
    COLLECTION ITEMS TERMINATED BY ','
    STORED AS TEXTFILE
    LOCATION '/user/hadoopuser/hive/input';
    

    Execute some queries then:

    hive> select * from mytable;
    1   [0,1,2,3,4]
    2   [5,6,7,8,9]
    

    Sum it in range m,n where m=1, n=3

    hive> select subarraysum(nums, 1,3) from mytable;
    3
    13
    

    Or

    hive> select sum(subarraysum(nums, 1,3)) from mytable;
    16
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get GeoIP working with hive. I found this: http://www.jointhegrid.com/hive-udf-geo-ip-jtg/index.jsp , which
Is it good practice to hive off complicated tasks that manage or determine the
Quick Hive/Hadoop question from a new user. I have a DOUBLE column that has
Im trying to create a hive provider that would be able to work towards
I'm running a Hadoop job using Hive actually that is supposed to uniq lines
I want to do table comparison in Hive. It is very hard to do
We have found a problem with our deployment to a production server that runs
Suppose that a couple hundred Gigs after starting to use HIVE I want to
First of all, I realize this is a messy situation, but it's not of
I saw a code from Android Hive, and I learned how to send array

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.