Apache Doris 原生C++ UDF之Coding(2)1

简介: Apache Doris 原生C++ UDF之Coding(2)1

Apache Doris 原生C++ UDF之Coding(2)

一、环境信息

1.1 硬件信息

1.2 软件信息

二、自定义TIME_TO_SEC函数

2.1 源码开发 & 实现一

2.1.1 测试主函数

2.1.2 UDF头文件

2.1.3 UDF源文件

2.1.4 实现方式一小结

2.2 源码开发 & 实现二

2.2.1 测试主函数

2.2.2 UDF头文件

2.2.3 UDF源文件

2.2.4 实现方式二小结

三、编译结果

四、函数使用

4.1 创建 UDF 函数

4.2 使用UDF 函数

五、总结

一、环境信息

1.1 硬件信息

  1. 1.CPU :4C
  2. 2.CPU型号:x64(AVX2)
  3. 3.内存 :10GB
  4. 4.硬盘 :66GB SSD

1.2 软件信息

  1. 1.Linux版本 :CentOS-7
  2. 2.Apahce Doris版本 :0.15-release
  3. 3.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 TIME_TO_SEC(FunctionContext* context, const StringVal& time);
/// --- Prepare / Close Functions ---
/// ---------------------------------
/// The UDF can optionally include a prepare function. The prepare function is called
/// before any calls to the UDF to evaluate values.
void AddUdfPrepare(FunctionContext* context, FunctionContext::FunctionStateScope scope);
/// The UDF can also optionally include a close function. The close function is called
/// after all calls to the UDF have completed.
void AddUdfClose(FunctionContext* context, FunctionContext::FunctionStateScope scope);
}

2.1.3 UDF源文件

C++
#include "time_to_sec.h"
namespace doris_udf {
IntVal TIME_TO_SEC(FunctionContext* context, const StringVal& time) {
    // handle null
    if (time.is_null) {
        return IntVal::null();
    }
    // clear other str
    using namespace std;
    const string timestr((char *)time.ptr);
    const regex r("^((?![0-9]{2}:[0-9]{2}:[0-9]{2}).)*");
    const string replace_str = "";
    string hms_time = regex_replace(timestr, r, replace_str);
    // handle str abnormal
    if(hms_time.length() != 8) {
        return IntVal::null();
    }
    // get hh mm ss
    int HH = atoi(hms_time.substr(0,2).c_str());
    int MM = atoi(hms_time.substr(3,2).c_str());
    int SS = atoi(hms_time.substr(6,2).c_str());
    // return sum sec
    return HH*3600 + MM*60 + SS;
}
/// --- Prepare / Close Functions ---
/// ---------------------------------
void AddUdfPrepare(FunctionContext* context, FunctionContext::FunctionStateScope scope) {}
void AddUdfClose(FunctionContext* context, FunctionContext::FunctionStateScope scope) {}
}

2.1.4 实现方式一小结

不建议使用,doris对其中的regex相关函数并不友好,会直接导致be所有节点crash。

2.2 源码开发 & 实现二

2.2.1 测试主函数

C++
//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
   string segSign = ":";
   string::size_type pos1 = text.find(segSign);
   if(pos1 == string::npos)
      cout << "没找到!" << endl;
   else
      cout << "找到了!下标:" << pos1<<endl;
    string time = text.substr(pos1-2,8);
    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.2.2 UDF头文件

C++
#pragma once
#include "udf.h"
#include <bits/stdc++.h>
namespace doris_udf {
IntVal TIME_TO_SEC(FunctionContext* context, const StringVal& time);
/// --- Prepare / Close Functions ---
/// ---------------------------------
/// The UDF can optionally include a prepare function. The prepare function is called
/// before any calls to the UDF to evaluate values.
void AddUdfPrepare(FunctionContext* context, FunctionContext::FunctionStateScope scope);
/// The UDF can also optionally include a close function. The close function is called
/// after all calls to the UDF have completed.
void AddUdfClose(FunctionContext* context, FunctionContext::FunctionStateScope scope);
}

2.2.3 UDF源文件

C++
#include "time_to_sec.h"
namespace doris_udf {
IntVal TIME_TO_SEC(FunctionContext* context, const StringVal& time) {
    // handle null
    if (time.is_null) {
        return IntVal::null();
    }
    // clear other str
    using namespace std;
    string timestr((char *)time.ptr);
    string segSign = ":";
    string::size_type pos = timestr.find(segSign);
    string hms_time;
    if(pos == string::npos)
        return IntVal::null();
     else
        hms_time = timestr.substr(pos-2,8);
    // handle str abnormal
    if(hms_time.length() != 8) {
        return IntVal::null();
    }
    // get hh mm ss
    int HH = atoi(hms_time.substr(0,2).c_str());
    int MM = atoi(hms_time.substr(3,2).c_str());
    int SS = atoi(hms_time.substr(6,2).c_str());
    // return sum sec
    IntVal result;
    result.val = HH*3600 + MM*60 + SS;
    return {result.val};
}
/// --- Prepare / Close Functions ---
/// ---------------------------------
void AddUdfPrepare(FunctionContext* context, FunctionContext::FunctionStateScope scope) {}
void AddUdfClose(FunctionContext* context, FunctionContext::FunctionStateScope scope) {}
}

2.2.4 实现方式二小结

基本完全使用字符串的API实现,简单高效并且兼容性较好,最终选定实现二。

相关文章
|
11天前
|
关系型数据库 Apache 流计算
手把手教你实现 OceanBase 数据到阿里云数据库 SelectDB 内核版 Apache Doris 的便捷迁移|实用指南
本文介绍了如何将数据从 OceanBase 迁移到阿里云数据库 SelectDB 内核版 Apache Doris。提供 3 种数据同步方法 1. 使用 DataX,下载 DataX 并编写配置文件,通过 OceanBaseReader 和 DorisWriter 进行数据迁移。 2. 利用 Apache Doris 的 Catalog功 能,将 OceanBase 表映射到 Doris 并插入数据。 3. 通过Flink CDC,设置 OceanBase 环境,配置 Flink 连接器,实现实时数据同步。
手把手教你实现 OceanBase 数据到阿里云数据库 SelectDB 内核版 Apache Doris 的便捷迁移|实用指南
|
4天前
|
SQL 存储 Apache
Apache Doris 2.1.3 版本正式发布
Apache Doris 2.1.3 版本正式发布!该版本在功能特性上对数据湖、物化视图、负载管理等方面进行了多项更新,进一步简化湖仓一体架构、加速了查询性能。 欢迎大家下载体验~
|
10天前
|
SQL 存储 调度
从 Volcano 火山模型到 Pipeline 执行模型,阿里云数据库 SelectDB 内核 Apache Doris 执行模型的迭代
一个合适的执行模型对于提高查询效率和系统性能至关重要。本文全面剖析 Apache Doris Pipeline 执行模型的设计与改造历程,并在 2.1 版本对并发执行模式与调度模式进一步优化,解决了执行并发受限、执行及调度开销大等问题。
从 Volcano 火山模型到 Pipeline 执行模型,阿里云数据库 SelectDB 内核 Apache Doris 执行模型的迭代
|
2天前
|
消息中间件 JSON Kafka
AutoMQ 生态集成 Apache Doris
Apache Doris 是一个高性能的分析型数据库,以其亚秒级查询响应和对复杂分析的支持而知名。它适合报表分析、即席查询等场景,能从 AutoMQ 通过 Routine Load 导入 Kafka 主题数据。本文详述了如何配置 Doris 环境,创建测试数据,以及设置 Routine Load 作业从 AutoMQ 导入 JSON 数据到 Doris 表的过程。最后,文中展示了验证数据成功导入的方法。Apache Doris 提供了低成本、高弹性的数据处理解决方案,其团队由 Apache RocketMQ 和 Linux LVS 的核心成员组成。
10 0
|
11天前
|
SQL 大数据 BI
从离线到实时:无锡锡商银行基于 Apache Doris 的数据仓库演进实践
从离线到实时:无锡锡商银行基于 Apache Doris 的数据仓库演进实践
|
11天前
|
存储 监控 Apache
查询提速11倍、资源节省70%,阿里云数据库内核版 Apache Doris 在网易日志和时序场景的实践
网易的灵犀办公和云信利用 Apache Doris 改进了大规模日志和时序数据处理,取代了 Elasticsearch 和 InfluxDB。Doris 实现了更低的服务器资源消耗和更高的查询性能,相比 Elasticsearch,查询速度提升至少 11 倍,存储资源节省达 70%。Doris 的列式存储、高压缩比和倒排索引等功能,优化了日志和时序数据的存储与分析,降低了存储成本并提高了查询效率。在灵犀办公和云信的实际应用中,Doris 显示出显著的性能优势,成功应对了数据增长带来的挑战。
查询提速11倍、资源节省70%,阿里云数据库内核版 Apache Doris 在网易日志和时序场景的实践
|
11天前
|
存储 SQL Apache
阿里云数据库内核 Apache Doris 基于 Workload Group 的负载隔离能力解读
阿里云数据库内核 Apache Doris 基于 Workload Group 的负载隔离能力解读
阿里云数据库内核 Apache Doris 基于 Workload Group 的负载隔离能力解读
|
11天前
|
Kubernetes 关系型数据库 Apache
Apache Doris 2.1.2 版本正式发布!
Apache Doris 2.1.2 版本正式发布!该版本提交了若干改进项以及问题修复,进一步提升了系统的性能及稳定性,欢迎大家下载体验!
|
11天前
|
Java 数据处理 调度
更高效准确的数据库内部任务调度实践,阿里云数据库SelectDB 内核 Apache Doris 内置 Job Scheduler 的实现与应用
Apache Doris 2.1 引入了内置的 Job Scheduler,旨在解决依赖外部调度系统的问题,提供秒级精确的定时任务管理。
|
11天前
|
SQL 监控 Apache
钱大妈生鲜如何利用 CCR 实现 Apache Doris 集群读写分离
钱大妈基于 阿里云 SelectDB 内核 Apache Doris 搭建了实时数仓,为业务提供实时精准分析的数据查询及分析服务。凭借 Apache Doris 强大的性能,钱大妈能够实时监控生鲜产品的流通情况,为商品结构的优化和食品新鲜度的保障提供坚实的数据支撑。

推荐镜像

更多