Impala用户定义的聚合函数C ++导致impalad崩溃

如何解决Impala用户定义的聚合函数C ++导致impalad崩溃

我的任务是使用C ++创建与Impala SQL一起使用的中值函数。我已经在笔记本电脑上安装了Cloudera的独立版本(5.16.2),所以我没有机会使生产服务器崩溃。我在尝试使其工作方面遇到困难。

我正在使用std::map作为关键字,并将结构作为值的double

struct sMedianInfoValue {
 int count; // count of the keys
 int iBegRowNbr; //if expanded out,the beginning row number
 int iEndRowNbr; //if expanded out,the ending row number
};

这是完整的UDAF:

#include <stdio.h>
#include <vector>
#include <algorithm>
#include <iostream>
#include <string.h>
#include <sstream>
#include <map>
#include <iterator>
#include <math.h>
#include "MIMedian.h"

using namespace std;

struct sMedianInfoValue {
 int count; // count of the keys
 int iBegRowNbr; //if expanded out,the ending row number
};

// Structure used to pass a pointer to the vector.
struct DblMapStruct {
 std::map<double,sMedianInfoValue> mKeyValueMap;
};

//Initialize the iTotalRows variable.
int iTotalRows = 0;

//*----------------------------------------------------------------------------------------------*
//* Helper function ComputeMedian.                                                               *
//*----------------------------------------------------------------------------------------------*
//Update the row numbers based on the counts in the map
void UpdateRowNbrs(std::map<double,sMedianInfoValue> pmKeyValueMap) {

 //Create an iterator into the map starting at the first entry.
 std::map<double,sMedianInfoValue>::iterator it = pmKeyValueMap.begin();

 //The previous row's value.
 int iPrevRowValue = 1;
 int iCount;
 int iBegRowNbr;
 int iEndRowNbr;
 double dKey;

 //Loop through the map updating the beginning and ending row numbers.
 while (it != pmKeyValueMap.end()) {

  dKey = it->first;
  iCount = it->second.count;
  iTotalRows += iCount;
  iBegRowNbr = iPrevRowValue;
  iEndRowNbr = iBegRowNbr + iCount - 1;
 
  iPrevRowValue = iEndRowNbr + 1;

  //Update the map
  pmKeyValueMap[dKey] = {iCount,iBegRowNbr,iEndRowNbr};
  it++;

 }
 
}

//Compute the median from the map.
double ComputeMedian(std::map<double,sMedianInfoValue> pmKeyValueMap) {

 double dMedian = 0.0;
 string sEVENODD;
 int iFirstValue=0;
 int iSecondValue=0;
 double iFirstKey;
 double iSecondKey;

 //Based on iTotalRows,determine if it's even or odd.
 (iTotalRows & 1) ? sEVENODD="ODD" : sEVENODD="EVEN"; 

 //If EVEN,create one variable iTotalRows/2 and another iTotalRows/2 + 1;
 //If ODD,create one variable FLOOR(iTotalRows/2) and another CEIL(iTotalRows/2);
 if (sEVENODD=="EVEN") {
  iFirstValue = iTotalRows/2;
  iSecondValue = 1 + iFirstValue;
 } 
 else if (sEVENODD=="ODD") {
  iFirstValue = floor(iTotalRows/2);
  iSecondValue = 1 + iFirstValue;
 }

 //Iterate through the map finding the key value corresponding to the range of beginning and ending values.
 std::map<double,sMedianInfoValue>::iterator it = pmKeyValueMap.begin();
 while (it != pmKeyValueMap.end()) {

  if ( (it->second.iBegRowNbr <= iFirstValue) && (iFirstValue <= it->second.iEndRowNbr) ) {
   iFirstKey=it->first;
  } 

  if ( (it->second.iBegRowNbr <= iSecondValue) && (iSecondValue <= it->second.iEndRowNbr) ) {
   iSecondKey=it->first;
  } 

  it++;
 }
 
 //Compute the average for the two keys.
 dMedian = (iFirstKey + iSecondKey)/2;

 return dMedian;

}

//*----------------------------------------------------------------------------------------------*
//* UDAF Initialization function.                                                                *
//*----------------------------------------------------------------------------------------------*
void MIMedianInit(FunctionContext* context,StringVal* val) {

  val->ptr = context->Allocate(sizeof(DblMapStruct));

  if (val->is_null) {
    *val = StringVal::null();
    return;
  }

  val->is_null = false;
  val->len = sizeof(DblMapStruct);
  memset(val->ptr,val->len);
  
}

//*----------------------------------------------------------------------------------------------*
//* UDAF Update function.                                                                        *
//*----------------------------------------------------------------------------------------------*
void MIMedianUpdate(FunctionContext* context,const DoubleVal& input,StringVal* val) {

  if (input.is_null || val->is_null) return;

  // Reconstitute the structure of the map.
  DblMapStruct* map = reinterpret_cast<DblMapStruct*>(val->ptr);

  // Insert the data into the map.
  int iCount=0;
  double dKey = input.val;

  //Update the map
  //Key not found in map.
  if (map->mKeyValueMap.find(dKey) == map->mKeyValueMap.end()) {

   //Key not found in map.
   iCount=1;
   sMedianInfoValue str_tmp = {iCount,0};
   map->mKeyValueMap.insert(std::make_pair(dKey,str_tmp));

  }
  else {

   //Key found in map...must update count.
   map->mKeyValueMap.at(dKey).count++;

  }

}

//*----------------------------------------------------------------------------------------------*
//* UDAF Merge function.                                                                         *
//*----------------------------------------------------------------------------------------------*
void MIMedianMerge(FunctionContext* context,const StringVal& src,StringVal* dst) {

 if (src.is_null || dst->is_null) return;

  // Reconstitute the structures of the maps.
  DblMapStruct* map_src = reinterpret_cast<DblMapStruct*>(src.ptr);
  DblMapStruct* map_dst = reinterpret_cast<DblMapStruct*>(dst->ptr);
  double dKey_src;
  int iCount_src;
  int iCount_dst;

  //Iterate source map and insert/update destination map.
  std::map<double,sMedianInfoValue>::iterator it = map_src->mKeyValueMap.begin();
  while (it != map_src->mKeyValueMap.end()) {

   dKey_src = it->first;
   iCount_src = it->second.count;
   iCount_dst = 0;

   //Update the map
   if (map_dst->mKeyValueMap.find(dKey_src) == map_dst->mKeyValueMap.end()) {

    //Key not found in map.
    iCount_dst=1;
    sMedianInfoValue str_tmp = {iCount_dst,0};
    map_dst->mKeyValueMap.insert(std::make_pair(dKey_src,str_tmp));

   }
   else {

    //Key found in map...get the existing count for this key.
    iCount_dst=map_dst->mKeyValueMap.at(dKey_src).count;
    map_dst->mKeyValueMap.at(dKey_src).count = iCount_src + iCount_dst;

   }

   it++;
  } 

}

//*----------------------------------------------------------------------------------------------*
//* UDAF Serialize function.                                                                     *
//*----------------------------------------------------------------------------------------------*
StringVal MIMedianSerialize(FunctionContext* context,const StringVal& val) {
 if (val.is_null) return StringVal::null();

 StringVal sResult = StringVal::CopyFrom(context,val.ptr,val.len);
 context->Free(val.ptr);

 return sResult;

}

//*----------------------------------------------------------------------------------------------*
//* UDAF Finalize function.                                                                      *
//*----------------------------------------------------------------------------------------------*
DoubleVal MIMedianFinalize(FunctionContext* context,const StringVal& val) {
  if (val.is_null) return DoubleVal::null();

  //Pointer to the map structure.
  DblMapStruct* map_src = reinterpret_cast<DblMapStruct*>(val.ptr);

  //Update the beginning and ending row counts in the map structure (they're all set to zero now).
  UpdateRowNbrs(map_src->mKeyValueMap);

  // Compute the median.
  DoubleVal dvMedian(ComputeMedian(map_src->mKeyValueMap));

  context->Free(val.ptr);
  
  return dvMedian;
}

这里是MIMedian.h

#ifndef IMPALA_MEDIAN_H
#define IMPALA_MEDIAN_H

#include <impala_udf/udf.h>
#include <assert.h>
#include <sstream>

using namespace impala_udf;
using namespace std;

void MIMedianInit(FunctionContext*,StringVal*);
void MIMedianUpdate(FunctionContext*,const DoubleVal&,StringVal*);
void MIMedianMerge(FunctionContext*,const StringVal&,StringVal*);
StringVal MIMedianSerialize(FunctionContext*,const StringVal&);
DoubleVal MIMedianFinalize(FunctionContext*,const StringVal&);

#endif

该函数编译成功,没有错误或警告:

rm -f MIMedian.cc.o
rm -f libmimedian.so
/usr/bin/c++ -fPIC -g -ggdb -std=c++11 -o MIMedian.cc.o -c MIMedian.cc
/usr/bin/c++ -fPIC -g -ggdb -std=c++11 -shared -Wl,-soname,libmimedian.so -o libmimedian.so MIMedian.cc.o -lImpalaUdf

我将文件libmimedian.so复制到HDFS并使用impala-shell在Impala中创建函数:

create aggregate function miimedian(double) returns double intermediate string location '/user/hive/warehouse/udf/libmimedian.so' init_fn='MIMedianInit' update_fn='MIMedianUpdate' merge_fn='MIMedianMerge' finalize_fn='MIMedianFinalize' serialize_fn='MIMedianSerialize';

当我使用此功能运行SQL查询时...

select grp,miimedian(col1) as median from tab3 group by grp order by 1;

...我在impala-shell中收到以下错误...

Socket error 104: Connection reset by peer
[Not connected] > 

/var/lib/impala文件夹中,我看到一个hs_err_pid####.log文件,它在日志文件的顶部指示了以下内容...

# Problematic frame:
# C  [libstdc++.so.6+0x748aa]

...再往下走...

Stack: [0x00007fa3926a9000,0x00007fa392ea9000],sp=0x00007fa392ea70b8,free space=8184k
Native frames: (J=compiled Java code,j=interpreted,Vv=VM code,C=native code)
C  [libstdc++.so.6+0x748aa]

C  [libmimedian.5498.0.so+0xce4d]  std::_Rb_tree<double,std::pair<double const,sMedianInfoValue>,std::_Select1st<std::pair<double const,sMedianInfoValue> >,std::less<double>,std::allocator<std::pair<double const,sMedianInfoValue> > >::_M_get_insert_unique_pos(double const&)+0xfd

C  [libmimedian.5498.0.so+0xc27c]  std::pair<std::_Rb_tree_iterator<std::pair<double const,bool> std::_Rb_tree<double,sMedianInfoValue> > >::_M_insert_unique<std::pair<double,sMedianInfoValue> >(std::pair<double,sMedianInfoValue>&&)+0x44

C  [libmimedian.5498.0.so+0xbd70]  std::pair<std::_Rb_tree_iterator<std::pair<double const,bool> std::map<double,sMedianInfoValue,sMedianInfoValue> > >::insert<std::pair<double,void>(std::pair<double,sMedianInfoValue>&&)+0x2e

C  [libmimedian.5498.0.so+0xb382]  MIMedianUpdate(impala_udf::FunctionContext*,impala_udf::DoubleVal const&,impala_udf::StringVal*)+0xe1

我完全不知所措!我使用std :: map的小测试程序可以完美地工作,包括更新功能和中值的计算。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-