我是Hive的新手,我想事先赦免我的无知,以免下面的任何事情发生.我有一张表如下:
SELECT a.storeid, a.smonth, a.sales FROM table a;
1001 1 35000.0
1002 2 35000.0
1001 2 25000.0
1002 3 110000.0
1001 3 40000.0
1002 1 40000.0
Run Code Online (Sandbox Code Playgroud)
我的目标输出如下:
1001 1 35000.0 35000.0
1001 2 25000.0 60000.0
1001 3 40000.0 100000.0
1002 1 40000.0 40000.0
1002 2 35000.0 75000.0
1002 3 110000.0 185000.0
Run Code Online (Sandbox Code Playgroud)
我编写了一个简单的hive udf sum类来实现上述功能,并在查询中使用了SORT BY storeid,smonth:
SELECT a.storeid, a.smonth, a.sales, rsum(sales)
FROM (SELECT * FROM table SORT BY storeid, smonth) a;
Run Code Online (Sandbox Code Playgroud)
显然,它不会产生上述输出,因为只有一个映射器,并且调用了相同的udf实例,它在总集合上生成一个运行总和.我的目标是为每个storeid重置udf类中的runningSum实例变量,以便evaluate函数返回上面的输出.我使用了以下内容:1.传递storeid变量rsum(sales,storeid),然后我们可以在udf类中正确处理这种情况.2.使用2个映射器,如以下查询中所示:
set mapred.reduce.tasks=2;
SELECT a.storeid, a.smonth, a.sales, rsum(sales)
FROM …
Run Code Online (Sandbox Code Playgroud) 我正在尝试在 stl unordered_map 上创建一个模板化的包装类。我将哈希函数类作为模板参数传递给包装类,并提供了字符串特化。下面的代码编译并工作,但如果包含注释部分,则编译错误说:
“/usr/include/c++/6/bits/unordered_map.h:143:28: 错误:没有匹配的函数调用'HashFunction
::HashFunction()' const hasher& __hf = hasher(),"。
但是,我一定会有散列函数类的ctor。我尝试了各种方法,但无法使其正常工作。请提供您的想法/意见。
#include <iostream>
#include <unordered_map>
using namespace std;
template< class Key >
class HashFunction
{
public:
//HashFunction( const Key & inKey );
size_t operator()(const Key &inKey) const;
private:
unsigned mHashCode;
};
//template<>
//HashFunction< string >::HashFunction( const string & inKey )
//{
//mHashCode=std::hash<string>{}(inKey);
//}
template <>
size_t HashFunction< string >::operator()(const string &inKey) const
{
return std::hash<string>{}(inKey);
//return mHashCode;
}
template< class Key, class Val, class Hash = HashFunction< Key …
Run Code Online (Sandbox Code Playgroud)