mirror of
https://gitee.com/rulego/streamsql.git
synced 2026-03-30 17:26:42 +00:00
chore:add readme
This commit is contained in:
@@ -237,152 +237,6 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
### 🔍 Pattern Matching Notes
|
||||
|
||||
**Note**: StreamSQL currently does not support standard SQL `LIKE` syntax, but provides more powerful alternatives:
|
||||
|
||||
- **Prefix matching**: `STARTSWITH(field, 'prefix')` replaces `field LIKE 'prefix%'`
|
||||
- **Suffix matching**: `ENDSWITH(field, 'suffix')` replaces `field LIKE '%suffix'`
|
||||
- **Contains matching**: `INDEXOF(field, 'substring') >= 0` replaces `field LIKE '%substring%'`
|
||||
- **Regex matching**: `REGEXP_MATCHES(field, '^pattern$')` supports complex pattern matching
|
||||
|
||||
**Examples**:
|
||||
```sql
|
||||
-- Replace: deviceId LIKE 'sensor%'
|
||||
WHERE STARTSWITH(deviceId, 'sensor')
|
||||
|
||||
-- Replace: message LIKE '%error%'
|
||||
WHERE INDEXOF(message, 'error') >= 0
|
||||
|
||||
-- Replace complex pattern: deviceId LIKE 'sensor[0-9]+'
|
||||
WHERE REGEXP_MATCHES(deviceId, '^sensor[0-9]+$')
|
||||
```
|
||||
|
||||
### Non-Aggregation Scenarios
|
||||
|
||||
StreamSQL supports real-time data transformation and filtering without aggregation operations. This mode provides immediate processing and output for each input record, making it ideal for data cleaning, enrichment, and real-time filtering.
|
||||
|
||||
```go
|
||||
// Real-time data transformation and filtering example
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
"github.com/rulego/streamsql"
|
||||
)
|
||||
|
||||
func main() {
|
||||
ssql := streamsql.New()
|
||||
defer ssql.Stop()
|
||||
|
||||
// Non-aggregation SQL - immediate data transformation
|
||||
rsql := `SELECT deviceId,
|
||||
upper(deviceType) as device_type,
|
||||
temperature * 1.8 + 32 as temp_fahrenheit,
|
||||
concat(location, '-', deviceId) as full_location,
|
||||
now() as processed_time
|
||||
FROM stream
|
||||
WHERE temperature > 0 AND deviceId LIKE 'sensor%'`
|
||||
|
||||
err := ssql.Execute(rsql)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// Handle real-time transformation results
|
||||
ssql.Stream().AddSink(func(result interface{}) {
|
||||
fmt.Printf("Transformed data: %+v\n", result)
|
||||
})
|
||||
|
||||
// Input raw data
|
||||
rawData := []map[string]interface{}{
|
||||
{
|
||||
"deviceId": "sensor001",
|
||||
"deviceType": "temperature",
|
||||
"temperature": 25.0,
|
||||
"humidity": 60,
|
||||
"location": "warehouse-A",
|
||||
},
|
||||
{
|
||||
"deviceId": "sensor002",
|
||||
"deviceType": "humidity",
|
||||
"temperature": 22.5,
|
||||
"humidity": 55,
|
||||
"location": "warehouse-B",
|
||||
},
|
||||
{
|
||||
"deviceId": "pump001", // Will be filtered out
|
||||
"deviceType": "actuator",
|
||||
"temperature": 30.0,
|
||||
"location": "factory",
|
||||
},
|
||||
}
|
||||
|
||||
// Each data record is processed immediately
|
||||
for _, data := range rawData {
|
||||
ssql.Stream().AddData(data)
|
||||
time.Sleep(100 * time.Millisecond) // Simulate real-time arrival
|
||||
}
|
||||
|
||||
time.Sleep(500 * time.Millisecond) // Wait for processing
|
||||
}
|
||||
```
|
||||
|
||||
#### Use Cases for Non-Aggregation Scenarios
|
||||
|
||||
**1. Real-time Data Cleaning and Validation**
|
||||
```sql
|
||||
-- Filter invalid records and normalize data formats
|
||||
SELECT deviceId,
|
||||
CAST(temperature AS FLOAT) as temperature,
|
||||
LOWER(status) as status,
|
||||
COALESCE(location, 'unknown') as location
|
||||
FROM stream
|
||||
WHERE temperature IS NOT NULL AND deviceId != ''
|
||||
```
|
||||
|
||||
**2. Data Enrichment and Transformation**
|
||||
```sql
|
||||
-- Add calculated fields and enrichment
|
||||
SELECT *,
|
||||
temperature * 1.8 + 32 as temp_fahrenheit,
|
||||
CASE WHEN temperature > 30 THEN 'hot'
|
||||
WHEN temperature < 10 THEN 'cold'
|
||||
ELSE 'normal' END as temp_category,
|
||||
FORMAT(humidity, 2) as formatted_humidity
|
||||
FROM stream
|
||||
```
|
||||
|
||||
**3. Real-time Alerting and Monitoring**
|
||||
```sql
|
||||
-- Filter critical events for immediate alerting
|
||||
SELECT deviceId, temperature, humidity, now() as alert_time
|
||||
FROM stream
|
||||
WHERE temperature > 50 OR humidity < 10
|
||||
```
|
||||
|
||||
**4. Data Format Conversion**
|
||||
```sql
|
||||
-- Convert data format for downstream systems
|
||||
SELECT TO_JSON(MAP(
|
||||
'id', deviceId,
|
||||
'metrics', MAP('temp', temperature, 'hum', humidity),
|
||||
'meta', MAP('location', location, 'type', deviceType)
|
||||
)) as json_output
|
||||
FROM stream
|
||||
```
|
||||
|
||||
**5. Real-time Data Routing**
|
||||
```sql
|
||||
-- Route data based on conditions
|
||||
SELECT *,
|
||||
CASE WHEN deviceType = 'sensor' THEN 'sensor_topic'
|
||||
WHEN deviceType = 'actuator' THEN 'actuator_topic'
|
||||
ELSE 'default_topic' END as routing_key
|
||||
FROM stream
|
||||
```
|
||||
|
||||
### Nested Field Access
|
||||
|
||||
StreamSQL supports querying nested structured data using dot notation (`.`) syntax to access nested fields:
|
||||
|
||||
@@ -253,39 +253,6 @@ func main() {
|
||||
}
|
||||
```
|
||||
|
||||
### 🔍 模式匹配功能
|
||||
|
||||
StreamSQL 支持标准 SQL 的 `LIKE` 语法进行模式匹配:
|
||||
|
||||
- **前缀匹配**: `field LIKE 'prefix%'` - 匹配以指定前缀开头的字符串
|
||||
- **后缀匹配**: `field LIKE '%suffix'` - 匹配以指定后缀结尾的字符串
|
||||
- **包含匹配**: `field LIKE '%substring%'` - 匹配包含指定子字符串的字符串
|
||||
- **单字符通配符**: `field LIKE 'patte_n'` - `_` 匹配任意单个字符
|
||||
- **复杂模式**: `field LIKE 'prefix%suffix'` - 组合前缀和后缀匹配
|
||||
|
||||
**示例**:
|
||||
```sql
|
||||
-- 前缀匹配:查找以'sensor'开头的设备ID
|
||||
WHERE deviceId LIKE 'sensor%'
|
||||
|
||||
-- 后缀匹配:查找以'error'结尾的消息
|
||||
WHERE message LIKE '%error'
|
||||
|
||||
-- 包含匹配:查找包含'alert'的日志
|
||||
WHERE logMessage LIKE '%alert%'
|
||||
|
||||
-- 单字符通配符:匹配三位数错误代码如E01, E02等
|
||||
WHERE errorCode LIKE 'E_0'
|
||||
|
||||
-- 复杂模式:匹配log_开头.log结尾的文件
|
||||
WHERE filename LIKE 'log_%.log'
|
||||
```
|
||||
|
||||
**兼容的字符串函数**:
|
||||
- `STARTSWITH(field, 'prefix')` - 等价于 `field LIKE 'prefix%'`
|
||||
- `ENDSWITH(field, 'suffix')` - 等价于 `field LIKE '%suffix'`
|
||||
- `REGEXP_MATCHES(field, '^pattern$')` - 支持更复杂的正则表达式匹配
|
||||
|
||||
### 嵌套字段访问
|
||||
|
||||
StreamSQL 还支持对嵌套结构数据进行查询,可以使用点号(`.`)语法访问嵌套字段:
|
||||
|
||||
Reference in New Issue
Block a user