122 lines
3.5 KiB
Go
122 lines
3.5 KiB
Go
// 演示 Pipeline Database 的基础使用
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
|
|
"code.tczkiot.com/wlw/pipelinedb"
|
|
"code.tczkiot.com/wlw/pipelinedb/examples/common"
|
|
)
|
|
|
|
func main() {
|
|
// 创建临时数据库文件
|
|
dbFile := "basic_example.db"
|
|
defer os.Remove(dbFile) // 清理临时文件
|
|
|
|
// 确保文件可以创建
|
|
if _, err := os.Create(dbFile); err != nil {
|
|
log.Fatalf("创建数据库文件失败: %v", err)
|
|
}
|
|
|
|
fmt.Println("🚀 Pipeline Database 基础使用示例")
|
|
fmt.Println("================================")
|
|
|
|
// 1. 打开数据库
|
|
fmt.Println("\n📂 步骤1: 打开数据库")
|
|
config := &pipelinedb.Config{
|
|
CacheSize: 100, // 缓存100个页面
|
|
}
|
|
|
|
// 创建处理器
|
|
handler := common.NewLoggingHandler()
|
|
|
|
pdb, err := pipelinedb.Open(pipelinedb.Options{
|
|
Filename: dbFile,
|
|
Config: config,
|
|
Handler: handler,
|
|
})
|
|
if err != nil {
|
|
log.Fatalf("打开数据库失败: %v", err)
|
|
}
|
|
defer pdb.Stop()
|
|
|
|
fmt.Printf("✅ 数据库已打开: %s\n", dbFile)
|
|
|
|
// 2. 接收数据
|
|
fmt.Println("\n📥 步骤2: 接收数据")
|
|
testData := []struct {
|
|
group string
|
|
data []byte
|
|
metadata string
|
|
}{
|
|
{"用户行为", []byte("用户点击了首页按钮"), `{"timestamp": "2024-01-01T10:00:00Z", "user_id": "user123"}`},
|
|
{"用户行为", []byte("用户浏览了产品页面"), `{"timestamp": "2024-01-01T10:01:00Z", "user_id": "user123"}`},
|
|
{"系统日志", []byte("系统启动完成"), `{"timestamp": "2024-01-01T09:00:00Z", "level": "info"}`},
|
|
{"系统日志", []byte("数据库连接建立"), `{"timestamp": "2024-01-01T09:01:00Z", "level": "info"}`},
|
|
{"错误日志", []byte("网络连接超时"), `{"timestamp": "2024-01-01T10:30:00Z", "level": "error"}`},
|
|
}
|
|
|
|
var recordIDs []int64
|
|
for _, item := range testData {
|
|
recordID, err := pdb.AcceptData(item.group, item.data, item.metadata)
|
|
if err != nil {
|
|
log.Fatalf("接收数据失败: %v", err)
|
|
}
|
|
recordIDs = append(recordIDs, recordID)
|
|
fmt.Printf(" ✅ 记录 %d: [%s] %s\n", recordID, item.group, string(item.data))
|
|
}
|
|
|
|
// 3. 查询数据
|
|
fmt.Println("\n🔍 步骤3: 查询数据")
|
|
|
|
// 按组查询
|
|
groups := []string{"用户行为", "系统日志", "错误日志"}
|
|
for _, group := range groups {
|
|
fmt.Printf("\n📋 查询组: %s\n", group)
|
|
|
|
pageReq := &pipelinedb.PageRequest{
|
|
Page: 1,
|
|
PageSize: 10,
|
|
}
|
|
|
|
response, err := pdb.GetRecordsByGroup(group, pageReq)
|
|
if err != nil {
|
|
fmt.Printf(" ❌ 查询失败: %v\n", err)
|
|
continue
|
|
}
|
|
|
|
fmt.Printf(" 📊 总记录数: %d, 当前页: %d/%d\n",
|
|
response.TotalCount, response.Page, response.TotalPages)
|
|
|
|
for _, record := range response.Records {
|
|
fmt.Printf(" 📄 ID:%d [%s] %s (创建时间: %s)\n",
|
|
record.ID, record.Status, string(record.Data),
|
|
record.CreatedAt.Format("15:04:05"))
|
|
}
|
|
}
|
|
|
|
// 4. 获取统计信息
|
|
fmt.Println("\n📊 步骤4: 获取统计信息")
|
|
stats, err := pdb.GetStats()
|
|
if err != nil {
|
|
log.Fatalf("获取统计信息失败: %v", err)
|
|
}
|
|
|
|
fmt.Printf("📈 数据库统计信息:\n")
|
|
fmt.Printf(" 总记录数: %d\n", stats.TotalRecords)
|
|
fmt.Printf(" 总组数: %d\n", len(stats.GroupStats))
|
|
|
|
fmt.Println("\n📋 各组统计:")
|
|
for group, groupStats := range stats.GroupStats {
|
|
fmt.Printf(" %s:\n", group)
|
|
fmt.Printf(" 热数据: %d\n", groupStats.HotRecords)
|
|
fmt.Printf(" 温数据: %d\n", groupStats.WarmRecords)
|
|
fmt.Printf(" 冷数据: %d\n", groupStats.ColdRecords)
|
|
fmt.Printf(" 总计: %d\n", groupStats.TotalRecords)
|
|
}
|
|
|
|
fmt.Println("\n🎉 基础使用示例完成!")
|
|
}
|