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. QT 让窗口(或控件)居中

    代码如下: XXX::XXX(QWidget *parent /* = 0 */) { .................. //注意,resize一定要放在这段代码的前面 resize(300, 3 ...

  2. zabbix 插件使用问题

    [elk@dr-mysql01 frontend]$ ../../bin/logstash -f std02.conf Settings: Default pipeline workers: 8 Pi ...

  3. rsyslog 只读取变化的日志

    <pre name="code" class="html">input(type="imfile" File="/us ...

  4. cf498C Array and Operations

    C. Array and Operations time limit per test 1 second memory limit per test 256 megabytes input stand ...

  5. 【转】C++动态创建二维数组,二维数组指针

    原作者博客:蒋国宝的IT技术博客 今天完成一道题目需要自己用指针创建一个二维的数组,不得不承认指针的确是恶心. int **result; ; ; result = new int*[row]; ; ...

  6. JScript_Test

    Hello SyntaxHighlighter function helloSyntaxHighlighter() { return "hi!"; } function hello ...

  7. windows使用命令行杀进程

    在windows有时使用任务管理器杀进程,一直杀不掉: 这个时候,可以使用命令行: 先使用tasklist 命令查看当前系统中的进程列表,然后针对你要杀的进程使用taskkill命令 如要杀nginx ...

  8. Eclipse集成PDT+XDebug调试PHP脚本 https://svn.jcxsoftware.com/node?page=5 [转]

    win7+xampp-win32-1.8.2-2-VC9+eclipse-jee-indigo-SR2-win32-x86_64.zip http://pjdong1990.iteye.com/blo ...

  9. Python笔记:使用pywin32处理excel文件

    因为前端同事须要批量的对excel文件做特殊处理,删除指定行,故写了此脚本.同一时候配合config.ini方便不熟悉py的同事使用 #!/usr/bin/env python #-*- coding ...

  10. jQuery简介以及jQuery选择器

    一 简介 1 定义:jQuery库是JavaScript的封装库 2 优点: 1) : 代码开源 2) : 选择器强大 3) : 完善的Ajax 4) : 浏览器兼容性高 5) : 文档完善(帮助文档 ...