内置 SQL 函数
FluxMQ 提供了丰富的内置函数库,包含 80+ 个强大的函数,帮助您在规则引擎中进行数据转换、处理和计算。这些函数涵盖了数据类型转换、字符串处理、时间操作、数学计算、加密编码、JSON 处理、数组操作等多个方面。
版本说明
FluxMQ 2.1.0 版本大幅增强了 SQL 函数库,新增了 50+ 个内置函数,使您能够更灵活地处理 MQTT 消息和 IoT 数据。
🔄 数据类型转换函数
基础类型转换
| 函数名 | 说明 | 示例 |
|---|---|---|
str(src) | 将任意类型转换为字符串 | str(123) → "123" |
int8(src) | 转换为 8 位整数(byte) | int8("127") → 127 |
int16(src) | 转换为 16 位整数(short) | int16("32767") → 32767 |
int32(src) | 转换为 32 位整数(int) | int32("123456") → 123456 |
int64(src) | 转换为 64 位整数(long) | int64("9223372036854775807") → 9223372036854775807 |
toFloat(src) | 转换为单精度浮点数 | toFloat("3.14") → 3.14 |
toDouble(src) | 转换为双精度浮点数 | toDouble("3.141592653589793") → 3.141592653589793 |
toBool(src) | 转换为布尔类型 | toBool("true") → true |
toDecimal(src) | 转换为高精度小数 | toDecimal("123.456789") → 123.456789 |
进制转换
| 函数名 | 说明 | 示例 |
|---|---|---|
hexStr(src) | 转换为十六进制字符串 | hexStr("hello") → "68656c6c6f" |
hexToInt(hex) | 十六进制转整数 | hexToInt("FF") → 255 |
intToHex(num) | 整数转十六进制 | intToHex(255) → "FF" |
binToInt(bin) | 二进制转整数 | binToInt("1010") → 10 |
intToBin(num) | 整数转二进制 | intToBin(10) → "1010" |
octToInt(oct) | 八进制转整数 | octToInt("12") → 10 |
intToOct(num) | 整数转八进制 | intToOct(10) → "12" |
📝 字符串处理函数
字符串操作
| 函数名 | 说明 | 示例 |
|---|---|---|
length(str) | 返回字符串长度 | length("hello") → 5 |
upper(str) | 转换为大写 | upper("hello") → "HELLO" |
lower(str) | 转换为小写 | lower("HELLO") → "hello" |
trim(str) | 去除首尾空格 | trim(" hello ") → "hello" |
ltrim(str) | 去除左侧空格 | ltrim(" hello") → "hello" |
rtrim(str) | 去除右侧空格 | rtrim("hello ") → "hello" |
reverse(str) | 反转字符串 | reverse("hello") → "olleh" |
repeat(str, count) | 重复字符串 | repeat("hi", 3) → "hihihi" |
字符串查找和替换
| 函数名 | 说明 | 示例 |
|---|---|---|
find(src, substr) | 从头查找子串 | find("hello world", "world") → "world" |
find(src, substr, "leading") | 从头查找第一个匹配 | find("abcabc", "abc", "leading") → "abcabc" |
find(src, substr, "trailing") | 从尾查找第一个匹配 | find("abcabc", "abc", "trailing") → "abc" |
indexOf(str, substr) | 返回子串位置 | indexOf("hello", "ell") → 1 |
lastIndexOf(str, substr) | 返回子串最后位置 | lastIndexOf("hello hello", "ello") → 7 |
replace(str, old, new) | 替换第一个匹配 | replace("hello", "l", "x") → "hexlo" |
replaceAll(str, old, new) | 替换所有匹配 | replaceAll("hello", "l", "x") → "hexxo" |
contains(str, substr) | 检查是否包含子串 | contains("hello", "ell") → true |
startsWith(str, prefix) | 检查是否以指定 前缀开始 | startsWith("hello", "he") → true |
endsWith(str, suffix) | 检查是否以指定后缀结束 | endsWith("hello", "lo") → true |
字符串分割和截取
| 函数名 | 说明 | 示例 |
|---|---|---|
split(src, delimiter) | 分割字符串 | split("a,b,c", ",") → ["a", "b", "c"] |
split(src, delimiter, "leading") | 从头分割一次 | split("a,b,c", ",", "leading") → ["a", "b,c"] |
split(src, delimiter, "trailing") | 从尾分割一次 | split("a,b,c", ",", "trailing") → ["a,b", "c"] |
substring(str, start) | 从指定位置截取到末尾 | substring("hello", 1) → "ello" |
substring(str, start, end) | 截取指定范围 | substring("hello", 1, 4) → "ell" |
left(str, length) | 从左截取指定长度 | left("hello", 3) → "hel" |
right(str, length) | 从右截取指定长度 | right("hello", 3) → "llo" |
charAt(str, index) | 获取指定位置字符 | charAt("hello", 1) → "e" |
字符串格式化
| 函数名 | 说明 | 示例 |
|---|---|---|
concat(str1, str2, ...) | 连接多个字符串 | concat("hello", " ", "world") → "hello world" |
format(template, args...) | 格式化字符串 | format("Hello {0}", "world") → "Hello world" |
padLeft(str, length, char) | 左填充到指定长度 | padLeft("5", 3, "0") → "005" |
padRight(str, length, char) | 右填充到指定长度 | padRight("5", 3, "0") → "500" |
⏰ 时间日期函数
时间获取
| 函数名 | 说明 | 示例 |
|---|---|---|
now() | 获取当前时间戳(毫秒) | now() → 1690599987495 |
currentDate() | 获取当前日期 | currentDate() → "2023-07-29" |
currentTime() | 获取当前时间 | currentTime() → "11:06:27" |
currentDateTime() | 获取当前日期时间 | currentDateTime() → "2023-07-29 11:06:27" |
时间格式化
| 函数名 | 说明 | 示例 |
|---|---|---|
date(timestamp) | 格式化为日期字符串 | date(1690599987495) → "2023-07-29" |
datetime(timestamp) | 格式化为日期时间字符串 | datetime(1690599987495) → "2023-07-29 11:06:27" |
time(timestamp) | 格式化为时间字符串 | time(1690599987495) → "11:06:27" |
formatDate(timestamp, format) | 自定义格式化日期 | formatDate(1690599987495, "yyyy/MM/dd") → "2023/07/29" |
时间转换
| 函数名 | 说明 | 示例 |
|---|---|---|
dateToTimestamp(dateStr) | 日期字符串转时间戳 | dateToTimestamp("2023-07-29") → 1690560000000 |
datetimeToTimestamp(datetimeStr) | 日期时间字符串转时间戳 | datetimeToTimestamp("2023-07-29 11:06:27") → 1690599987000 |
timeToTimestamp(timeStr) | 时间字符串转时间戳 | timeToTimestamp("11:06:27") → 39987000 |
时间计算
| 函数名 | 说明 | 示例 |
|---|---|---|
addYears(timestamp, years) | 增加年份 | addYears(1690599987495, 1) → 1722135987495 |
addMonths(timestamp, months) | 增加月份 | addMonths(1690599987495, 3) → 1698375987495 |
addDays(timestamp, days) | 增加天数 | addDays(1690599987495, 7) → 1691204787495 |
addHours(timestamp, hours) | 增加小时 | addHours(1690599987495, 2) → 1690607187495 |
addMinutes(timestamp, minutes) | 增加分钟 | addMinutes(1690599987495, 30) → 1690601787495 |
addSeconds(timestamp, seconds) | 增加秒数 | addSeconds(1690599987495, 60) → 1690600047495 |
时间提取
| 函数名 | 说明 | 示例 |
|---|---|---|
year(timestamp) | 提取年份 | year(1690599987495) → 2023 |
month(timestamp) | 提取月份 | month(1690599987495) → 7 |
day(timestamp) | 提取日期 | day(1690599987495) → 29 |
hour(timestamp) | 提取小时 | hour(1690599987495) → 11 |
minute(timestamp) | 提取分钟 | minute(1690599987495) → 6 |
second(timestamp) | 提取秒数 | second(1690599987495) → 27 |
weekday(timestamp) | 提取星期几 | weekday(1690599987495) → 6 |
weekOfYear(timestamp) | 提取年中第几周 | weekOfYear(1690599987495) → 30 |
dayOfYear(timestamp) | 提取年中第几天 | dayOfYear(1690599987495) → 210 |
🧮 数学计算函数
基础数学函数
| 函数名 | 说明 | 示例 |
|---|---|---|
abs(num) | 绝对值 | abs(-5) → 5 |
max(a, b, ...) | 最大值 | max(1, 5, 3) → 5 |
min(a, b, ...) | 最小值 | min(1, 5, 3) → 1 |
round(num) | 四舍五入 | round(3.6) → 4 |
round(num, digits) | 指定小数位四舍五入 | round(3.14159, 2) → 3.14 |
floor(num) | 向下取整 | floor(3.9) → 3 |
ceil(num) | 向上取整 | ceil(3.1) → 4 |
truncate(num) | 截断小数部分 | truncate(3.9) → 3 |
高级数学函数
| 函数名 | 说明 | 示例 |
|---|---|---|
sqrt(num) | 平方根 | sqrt(16) → 4 |
pow(base, exp) | 乘方 | pow(2, 3) → 8 |
exp(num) | e的幂 | exp(1) → 2.718281828459045 |
log(num) | 自然对数 | log(2.718281828459045) → 1 |
log10(num) | 以10为底的对数 | log10(100) → 2 |
sin(angle) | 正弦值(弧度) | sin(1.5708) → 1 |
cos(angle) | 余弦值(弧度) | cos(0) → 1 |
tan(angle) | 正切值(弧度) | tan(0.7854) → 1 |
random() | 生成0-1随机数 | random() → 0.7234567 |
randomInt(min, max) | 生成指定范围随机整数 | randomInt(1, 10) → 7 |
🔐 加密编码函数
哈希函数
| 函数名 | 说明 | 示例 |
|---|---|---|
md5(str) | MD5 哈希 | md5("hello") → "5d41402abc4b2a76b9719d911017c592" |
sha1(str) | SHA1 哈希 | sha1("hello") → "aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d" |
sha256(str) | SHA256 哈希 | sha256("hello") → "2cf24dba4f21d4288..." |
sha512(str) | SHA512 哈希 | sha512("hello") → "9b71d224bd62f378..." |
crc32(str) | CRC32 校验 | crc32("hello") → "907060870" |