Apache Doris 原生C++ UDF之Coding(2)
一、环境信息
1.1 硬件信息
- CPU :4C
- CPU型号:x64(AVX2)
- 内存 :10GB
- 硬盘 :66GB SSD
1.2 软件信息
- Linux版本 :CentOS-7
- Apahce Doris版本 :0.15-release
- CodeBlocks版本:20.03mingw
二、自定义TIME_TO_SEC函数
实现传入一个时间参数,将其时间部分转换成秒的UDF。

2.1 源码开发 & 实现一
2.1.1 测试主函数
//time_to_sec 的语法格式
// TIME_TO_SEC(time)
//语法格式说明
//time:传入时间,如果传入了日期部分,也不会管,只将时间部分转换成秒
//重点:是指将传入的时间转换成距离当天00:00:00的秒数,00:00:00为基数,等于 0 秒
#include <iostream>
#include <string>
#include <regex>
using namespace std;
int time_to_sec(string text)
{
// clear other str
regex r("^((?![0-9]{2}:[0-9]{2}:[0-9]{2}).)*");
string time = regex_replace(text, r, "");
cout << time << endl;
// handle abnormal
if(time.length() != 8)
return NULL;
// get hh mm ss
int HH = atoi(time.substr(0,2).c_str());
int MM = atoi(time.substr(3,2).c_str());
int SS = atoi(time.substr(6,2).c_str());
// return sum sec
return HH*3600 + MM*60 + SS;
}
int main()
{
cout<<time_to_sec("1987-01-01 00:39:38")<<endl;
return 0;
}
2.1.2 UDF头文件
C++
#pragma once
#include "udf.h"
#include <bits/stdc++.h>
namespace doris_udf {
IntVal

文章详细介绍了在ApacheDoris中使用C++编写自定义函数TIME_TO_SEC的过程,包括环境配置、源码实现、编译与测试,以及注意事项,最后建议使用JavaUDF以提高兼容性。
最低0.47元/天 解锁文章
1312





