Skip to content

Commit

Permalink
add query demo
Browse files Browse the repository at this point in the history
  • Loading branch information
weilong.pwl committed Aug 15, 2023
1 parent 8c55f03 commit cdbf3df
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions example/consumer/query_demo/simple_demo_with_query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package main

import (
"fmt"
"os"
"os/signal"
"syscall"

sls "github.com/aliyun/aliyun-log-go-sdk"
consumerLibrary "github.com/aliyun/aliyun-log-go-sdk/consumer"
"github.com/go-kit/kit/log/level"
)

// README :
// This is a very simple example of pulling data from your logstore and printing it for consumption, including pre-handling for logs.

func main() {
option := consumerLibrary.LogHubConfig{
Endpoint: "",
AccessKeyID: "",
AccessKeySecret: "",
Project: "",
Logstore: "",
ConsumerGroupName: "",
ConsumerName: "",
// This options is used for initialization, will be ignored once consumer group is created and each shard has been started to be consumed.
// Could be "begin", "end", "specific time format in time stamp", it's log receiving time.
CursorPosition: consumerLibrary.BEGIN_CURSOR,
// Query is for log pre-handling before return to client, more info refer to https://help.aliyun.com/zh/sls/user-guide/rule-based-consumption
Query: "* | where cast(body_bytes_sent as bigint) > 14000",
}

consumerWorker := consumerLibrary.InitConsumerWorkerWithCheckpointTracker(option, process)
ch := make(chan os.Signal, 1)
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
consumerWorker.Start()
if _, ok := <-ch; ok {
level.Info(consumerWorker.Logger).Log("msg", "get stop signal, start to stop consumer worker", "consumer worker name", option.ConsumerName)
consumerWorker.StopAndWait()
}
}

// Fill in your consumption logic here, and be careful not to change the parameters of the function and the return value,
// otherwise you will report errors.
func process(shardId int, logGroupList *sls.LogGroupList, checkpointTracker consumerLibrary.CheckPointTracker) (string, error) {
fmt.Println(shardId, logGroupList)
checkpointTracker.SaveCheckPoint(false)
return "", nil
}

0 comments on commit cdbf3df

Please sign in to comment.