Giter Site home page Giter Site logo

gabriekong / hive-udaf-maxrow Goto Github PK

View Code? Open in Web Editor NEW

This project forked from scribd/hive-udaf-maxrow

0.0 1.0 0.0 225 KB

A simple aggregate function (UDAF) for Hive -- like max() but it allows you to refer to additional columns in the maximal row.

Java 100.00%

hive-udaf-maxrow's Introduction

hive-udaf-maxrow

hive-udaf-maxrow is a simple user-defined aggregate function (UDAF) for Hive.

The maxrow() aggregate function is similar to the built-in max() function, but it allows you to refer to additional columns in the maximal row.

Example

For example, given the following data in a Hive table:

idtssomedata
12data-1,2
13data-1,3
14data-1,4
25data-2,5
23data-2,3
24data-2,4
36data-3,6
31data-3,1
34data-3,4

You can query this table using the maxrow() function:

hive> ADD JAR hive-udaf-maxrow.jar;
hive> CREATE TEMPORARY FUNCTION maxrow AS 'com.scribd.hive.udaf.GenericUDAFMaxRow';
hive> SELECT id, maxrow(ts, somedata) FROM sometable GROUP BY id;
idmaxrow
1{"col0":4,"col1":"data-1,4"}
2{"col0":5,"col1":"data-2,5"}
3{"col0":6,"col1":"data-3,6"}

While maxrow() looks only at its first parameter ("ts" in this case) to compute the maximum value, it carries along any additional values ("somedata" in this case).

Since maxrow() returns a "struct" value (see below), you can parse the result with Hive's built-in "dot" notation. For example:

hive> SELECT id, m.col0 as ts, m.col1 as somedata FROM (
          SELECT id, maxrow(ts, somedata) as m FROM sometable GROUP BY id
      ) s;
idtssomedata
14data-1,4
25data-2,5
36data-3,6

Limitations

As can be seen from the example above, there are a couple of limitations due to how Hive UDAFs work:

  • A UDAF can only output a value for a single column. Therefore, maxrow() returns a complex-valued "struct" object.
  • Hive does not provide the UDAF with the name of the columns that are being passed as input to the UDAF. Therefore, maxrow() generates simple names such as "col0", "col1", etc.

Building

To build hive-udaf-maxrow, you need to specify the location of your Hadoop and Hive jar files using the HADOOP_HOME and HIVE_HOME environment variables. The build classpath will include all of the jar files in these directories and and their lib/ subdirectories. For example:

> HADOOP_HOME=/path/to/hadoop HIVE_HOME=/path/to/hive ant

A successful build will create the dist/hive-udaf-maxrow.jar file. You can add this jar file to your Hive session using the ADD JAR command shown above.

hive-udaf-maxrow's People

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.