内置 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) |