key points:

1. group by key and sort by using distribute by and sort by.

2. get top k elements by a UDF (user defined function) RANK

---------Here is the source code.--------------

package com.example.hive.udf;
import org.apache.hadoop.hive.ql.exec.UDF;

public final class Rank extends UDF{
private int counter;
private String last_key;
public int evaluate(final String key){
if ( !key.equalsIgnoreCase(this.last_key) ) {
this.counter = 0;
this.last_key = key;
}
return this.counter++;
}
}

The details are as the following.

---original data, table region(region_nbr, region_id)---

100 10
200 12
300 33
100 4
100 8
200 20
300 31
300 3
400 4
200 2

-----what I need is as below-----

100 10
100 8
200 20
200 12
300 33
300 31
400 4

---

1. step1. compile java with a shell, compile_udf.sh.

#!/bin/bash

if [ $# != 1 ]; then
echo "Usage: $0 <java file>"
exit 1
fi

CNAME=${1%.java}
JARNAME=$CNAME.jar
JARDIR=/tmp/hive_jars/$CNAME
HIVE_HOME2="/usr/local/hive-0.9.0"
CLASSPATH=$(ls $HIVE_HOME2/lib/hive-serde-*.jar):$(ls $HIVE_HOME2/lib/hive-exec-*.jar):$(ls /home/oicq/hadoop/hadoop-1.0.2/hadoop-core-*.jar)

function tell {
echo
echo "$1 successfully compiled. In Hive run:"
#echo "$> add jar $JARNAME;"
#echo "$> create temporary function $CNAME as 'com.example.hive.udf.$CNAME';"
echo
}

mkdir -p $JARDIR
javac -classpath $CLASSPATH -d $JARDIR/ $1 && jar -cf $JARNAME -C $JARDIR/ . && tell $1

step 2. run hive

hive -e "add jar /data/ginobili/UDF/Rank.jar; create temporary function Rank as 'com.example.hive.udf.Rank'; select region_nbr, region_id from ( select region_nbr, region_id, Rank(region_nbr) as rank from (select * from test_gino.region distribute by region_nbr sort by region_nbr, region_id desc)a )b where rank < 2"

REFERENCE

http://stackoverflow.com/questions/9390698/hive-getting-top-n-records-in-group-by-query

http://findingscience.com/hadoop/hive/2011/01/07/compiling-user-defined-functions-for-hive-on-hadoop.html

http://stackoverflow.com/questions/11405446/find-top-10-latest-record-for-each-buyer-id-for-yesterdays-date

get top k elements of the same key in hive的更多相关文章

  1. Leetcode 347. Top K Frequent Elements

    Given a non-empty array of integers, return the k most frequent elements. For example,Given [1,1,1,2 ...

  2. 347. Top K Frequent Elements

    Given a non-empty array of integers, return the k most frequent elements. For example,Given [1,1,1,2 ...

  3. [Swift]LeetCode347. 前K个高频元素 | Top K Frequent Elements

    Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...

  4. C#版(打败99.28%的提交) - Leetcode 347. Top K Frequent Elements - 题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

  5. 347. Top K Frequent Elements (sort map)

    Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...

  6. [LeetCode] Top K Frequent Elements 前K个高频元素

    Given a non-empty array of integers, return the k most frequent elements. For example,Given [1,1,1,2 ...

  7. [leetcode]347. Top K Frequent Elements K个最常见元素

    Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...

  8. Top K Frequent Elements 前K个高频元素

    Top K Frequent Elements 347. Top K Frequent Elements [LeetCode] Top K Frequent Elements 前K个高频元素

  9. [LeetCode] 347. Top K Frequent Elements 前K个高频元素

    Given a non-empty array of integers, return the k most frequent elements. Example 1: Input: nums = [ ...

随机推荐

  1. java——Arrays.asList()方法

    Arrays.asList() 是将数组作为列表 问题来源于: public class Test { public static void main(String[] args) { int[] a ...

  2. 远程连接mysql

    win系统下,连接别人的mysql或者让别人链接自己的mysql: 打开命令行cmd 进入mysql: mysql -u root -p mysql>use mysql;  mysql>s ...

  3. asp.net mvc cooike 购物车 如何实现

    先上代码: 1. ShoppingCartService 类 using System; using System.Collections.Generic; using System.Linq; us ...

  4. 现代的新语言--Swift初探

    新的语言 WWDC简短的介绍,新的语言Swift就问世了,尽管新语言的名字导致贴吧下歌手粉丝和开发人员们争抢地盘- -,只是雨燕就是这么来了. WWDC keynote里给Swift打上了非常多标签: ...

  5. C# Tips:获得当前登录计算机的用户(本地用户/域用户)

    须要using的namespace: using System.Security.Principal; 获得登录计算机的用户: WindowsIdentity windowsIdentity = Wi ...

  6. Win32函数Sleep的精度测试

    用了三种方法,第一种使用高精度性能计数器:第二种是使用多媒体定时器,另一种是<Windows图形编程>里提供的CPU周期来获取.推荐第一种方式测量: 先看第一种: #include < ...

  7. web_api vs2015 新加标题无法打开

    HomeController 去掉特性[Authorize]

  8. 【VS2015正式版下载】Visual Studio 2015 正式版开放下载 Visual Studio 2015 神key

    说明: 微软定于2015年7月20日发布Visual Studio 2015正式版,目前其官方网站已经提供正式版本的下载. 可在https://www.visualstudio.com/en-us/d ...

  9. django: startproject

    python 的 django 框架的安装教程很多,这里不列举安装过程,直接开始记开发应用过程. 1  startprojec,新建项目 $ django-admin.py startproject ...

  10. NodeJs获取不到POST参数

    NodeJs报错,从网页端获取不到POST参数,提示错误类似如下 TypeError: Cannot read property 'username' of undefined     at C:\U ...