Skip to content

Commit dacb3b3

Browse files
authored
Merge pull request #136 from awesome-fc/update-eventstreaming-mode-event-type
update event streaming mode event type
2 parents 9d85318 + d365bcf commit dacb3b3

File tree

12 files changed

+115
-75
lines changed

12 files changed

+115
-75
lines changed

quick-start-sample-codes/quick-start-sample-codes-golang/kafka-trigger-golang/src/code/main.go

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package main
22

33
import (
44
"context"
5-
"encoding/json"
65
"fmt"
76

87
"github.com/aliyun/fc-runtime-go-sdk/fc"
@@ -35,27 +34,20 @@ type DataHeader struct {
3534
IsReadOnly bool
3635
}
3736

38-
func HandleRequest(ctx context.Context, event []string) (string, error) {
37+
func HandleRequest(ctx context.Context, events []StructEvent) (string, error) {
3938
fctx, ok := fccontext.FromContext(ctx)
4039
if !ok {
4140
return "Get fccontext fail.", nil
4241
}
4342
flog := fctx.GetLogger()
4443

45-
for _, eventString := range event {
46-
var evt StructEvent
47-
err := json.Unmarshal([]byte(eventString), &evt)
48-
if err != nil {
49-
return "Unmarshal event fail.", err
50-
}
51-
flog.Info("kafka event:", event)
52-
44+
for _, evt := range events {
5345
// The trigger event data is in the `Data` json object from the json array
5446
flog.Info("kafka topic:", evt.Data.Topic)
5547
flog.Info("kafka messgae:", evt.Data.Value)
5648
}
5749

58-
return fmt.Sprintf("Receive Kafka Trigger Event: %v", event), nil
50+
return fmt.Sprintf("Receive Kafka Trigger Event: %v", events), nil
5951
}
6052

6153
func main() {
Original file line numberDiff line numberDiff line change
@@ -1 +1,21 @@
1-
["{\"data\":{\"topic\":\"HelloTopic\",\"partition\":9,\"offset\":3,\"timestamp\":1659346376797,\"headers\":{\"headers\":[],\"isReadOnly\":false},\"value\":\"b\\u0027{\\\\n \\\"Test\\\": \\\"TestKafkaEventBridgetrigger\\\"\\\\n}\\u0027\"},\"id\":\"1cb591f9-987e-41d9-b974-0342e9acb90a\",\"source\":\"acs:alikafka\",\"specversion\":\"1.0\",\"type\":\"alikafka:Topic:Message\",\"datacontenttype\":\"application/json; charset\\u003dutf-8\",\"time\":\"2022-08-01T09:32:56.797Z\",\"subject\":\"acs:alikafka:alikafka_pre-cn-7pp2t2jwj001:topic:HelloTopic\",\"aliyunaccountid\":\"1938858730552836\"}"]
1+
[{
2+
"data": {
3+
"topic": "HelloTopic",
4+
"partition": 9,
5+
"offset": 3,
6+
"timestamp": 1659346376797,
7+
"headers": {
8+
"headers": [],
9+
"isReadOnly": false
10+
},
11+
"value": "b\u0027{\\n \"Test\": \"TestKafkaEventBridgetrigger\"\\n}\u0027"
12+
},
13+
"id": "1cb591f9-987e-41d9-b974-0342e9acb90a",
14+
"source": "acs:alikafka",
15+
"specversion": "1.0",
16+
"type": "alikafka:Topic:Message",
17+
"datacontenttype": "application/json; charset\u003dutf-8",
18+
"time": "2022-08-01T09:32:56.797Z",
19+
"subject": "acs:alikafka:alikafka_pre-cn-7pp2t2jwj001:topic:HelloTopic",
20+
"aliyunaccountid": "1938858730552836"
21+
}]

quick-start-sample-codes/quick-start-sample-codes-golang/rabbit-trigger-golang/publish.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Type: Application
22
Name: rabbitmq-trigger-fc-event-golang
33
Provider:
44
- 阿里云
5-
Version: 0.0.2
5+
Version: 0.0.3
66
Description: 快速部署一个具备 RabbitMQ 触发器 Event 类型的函数到阿里云函数计算
77
HomePage: https://github.com/devsapp/start-fc
88
Tags:

quick-start-sample-codes/quick-start-sample-codes-golang/rabbit-trigger-golang/src/code/main.go

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package main
22

33
import (
44
"context"
5-
"encoding/json"
65
"fmt"
76

87
"github.com/aliyun/fc-runtime-go-sdk/fc"
@@ -39,27 +38,20 @@ type RabbitProps struct {
3938
MessageId *string `json:"messageId"`
4039
}
4140

42-
func HandleRequest(ctx context.Context, event []string) (string, error) {
41+
func HandleRequest(ctx context.Context, events []StructEvent) (string, error) {
4342
fctx, ok := fccontext.FromContext(ctx)
4443
if !ok {
4544
return "Get fccontext fail.", nil
4645
}
4746
flog := fctx.GetLogger()
48-
flog.Info("event: %+v", event)
49-
50-
for _, eventString := range event {
51-
var evt *StructEvent
52-
err := json.Unmarshal([]byte(eventString), &evt)
53-
if err != nil {
54-
return "Unmarshal event fail.", err
55-
}
5647

48+
for _, evt := range events {
5749
// The trigger event data is in the `Data` json object from the json array
5850
flog.Info("event id:", *evt.Id)
5951
flog.Info("body of rabbitmq messageid :", *evt.Data.Body)
6052
}
6153

62-
return fmt.Sprintf("Receive RabbitMQ Trigger Event: %v", event), nil
54+
return fmt.Sprintf("Receive RabbitMQ Trigger Event: %v", events), nil
6355
}
6456

6557
func main() {
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
[
2-
"{
3-
\"data\":{
4-
\"envelope\":{
5-
\"deliveryTag\":2,
6-
\"redeliver\":false,
7-
\"exchange\":\"test-exchange\",
8-
\"routingKey\":\"test-binding\"
2+
{
3+
"data":{
4+
"envelope":{
5+
"deliveryTag":2,
6+
"redeliver":false,
7+
"exchange":"test-exchange",
8+
"routingKey":"test-binding"
99
},
10-
\"props\":{
11-
\"messageId\":\"d720c348-ed4b-471c-8c97-d3a3e6afdb3e\"
10+
"props":{
11+
"messageId":"d720c348-ed4b-471c-8c97-d3a3e6afdb3e"
1212
},
13-
\"body\":\"消息发送Body010\"
13+
"body":"消息发送Body010"
1414
},
15-
\"id\":\"d720c348-ed4b-471c-8c97-d3a3e6afdb3e\",
16-
\"source\":\"acs:amqp\",
17-
\"specversion\":\"1.0\",
18-
\"type\":\"amqp:Queue:SendMessage\",
19-
\"datacontenttype\":\"application/json; charset\u003dutf-8\",
20-
\"time\":\"2022-08-19T09:55:53.460Z\",
21-
\"subject\":\"acs:amqp:cn-qingdao:123:/instances/amqp-cn-xxx/vhosts/test-test/queues/test-test\",
22-
\"aliyunaccountid\":\"123\"
23-
}"
15+
"id":"d720c348-ed4b-471c-8c97-d3a3e6afdb3e",
16+
"source":"acs:amqp",
17+
"specversion":"1.0",
18+
"type":"amqp:Queue:SendMessage",
19+
"datacontenttype":"application/json; charset\u003dutf-8",
20+
"time":"2022-08-19T09:55:53.460Z",
21+
"subject":"acs:amqp:cn-qingdao:123:/instances/amqp-cn-xxx/vhosts/test-test/queues/test-test",
22+
"aliyunaccountid":"123"
23+
}
2424
]
Original file line numberDiff line numberDiff line change
@@ -1 +1,21 @@
1-
["{\"data\":{\"topic\":\"HelloTopic\",\"partition\":9,\"offset\":3,\"timestamp\":1659346376797,\"headers\":{\"headers\":[],\"isReadOnly\":false},\"value\":\"b\\u0027{\\\\n \\\"Test\\\": \\\"TestKafkaEBtrigger\\\"\\\\n}\\u0027\"},\"id\":\"1cb591f9-987e-41d9-b974-0342e9acb90a\",\"source\":\"acs:alikafka\",\"specversion\":\"1.0\",\"type\":\"alikafka:Topic:Message\",\"datacontenttype\":\"application/json; charset\\u003dutf-8\",\"time\":\"2022-08-01T09:32:56.797Z\",\"subject\":\"acs:alikafka:alikafka_pre-cn-7pp2t2jwj001:topic:HelloTopic\",\"aliyunaccountid\":\"1938858730552836\"}"]
1+
[{
2+
"data": {
3+
"topic": "HelloTopic",
4+
"partition": 9,
5+
"offset": 3,
6+
"timestamp": 1659346376797,
7+
"headers": {
8+
"headers": [],
9+
"isReadOnly": false
10+
},
11+
"value": "testValue"
12+
},
13+
"id": "1cb591f9-987e-41d9-b974-0342e9acb90a",
14+
"source": "acs:alikafka",
15+
"specversion": "1.0",
16+
"type": "alikafka:Topic:Message",
17+
"datacontenttype": "application/json; charset\u003dutf-8",
18+
"time": "2022-08-01T09:32:56.797Z",
19+
"subject": "acs:alikafka:alikafka_pre-cn-7pp2t2jwj001:topic:HelloTopic",
20+
"aliyunaccountid": "1938858730552836"
21+
}]

quick-start-sample-codes/quick-start-sample-codes-nodejs/kafka-trigger-nodejs/src/code/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ exports.handler = function (event, context, callback) {
66
var eventObj = JSON.parse(event.toString());
77

88
for (let i = 0; i < eventObj.length; ++i) {
9-
var evt = JSON.parse(eventObj[i]);
9+
var evt = eventObj[i];
1010
var messageTopic = evt['data']['topic']
1111
var message = evt['data']['value']
1212

quick-start-sample-codes/quick-start-sample-codes-python/kafka-trigger-python/src/code/index.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ def handler(event, context):
1010
eventObject = json.loads(event)
1111
# Get each json object from json array
1212
for evt in eventObject:
13-
# Parse the json inside
14-
evt = json.loads(evt)
15-
1613
logger.info('message topic:' + evt['data']['topic'])
1714
logger.info('message value:' + evt['data']['value'])
1815

Original file line numberDiff line numberDiff line change
@@ -1 +1,23 @@
1-
["{\"data\":{\"topic\":\"HelloTopic\",\"partition\":9,\"offset\":3,\"timestamp\":1659346376797,\"headers\":{\"headers\":[],\"isReadOnly\":false},\"value\":\"b\\u0027{\\\\n \\\"Test\\\": \\\"TestKafkaEBtrigger\\\"\\\\n}\\u0027\"},\"id\":\"1cb591f9-987e-41d9-b974-0342e9acb90a\",\"source\":\"acs:alikafka\",\"specversion\":\"1.0\",\"type\":\"alikafka:Topic:Message\",\"datacontenttype\":\"application/json; charset\\u003dutf-8\",\"time\":\"2022-08-01T09:32:56.797Z\",\"subject\":\"acs:alikafka:alikafka_pre-cn-7pp2t2jwj001:topic:HelloTopic\",\"aliyunaccountid\":\"1938858730552836\"}"]
1+
[
2+
{
3+
"data": {
4+
"topic": "HelloTopic",
5+
"partition": 9,
6+
"offset": 3,
7+
"timestamp": 1659346376797,
8+
"headers": {
9+
"headers": [],
10+
"isReadOnly": false
11+
},
12+
"value": "b\u0027{\\n \"Test\": \"TestKafkaEBtrigger\"\\n}\u0027"
13+
},
14+
"id": "1cb591f9-987e-41d9-b974-0342e9acb90a",
15+
"source": "acs:alikafka",
16+
"specversion": "1.0",
17+
"type": "alikafka:Topic:Message",
18+
"datacontenttype": "application/json; charset\u003dutf-8",
19+
"time": "2022-08-01T09:32:56.797Z",
20+
"subject": "acs:alikafka:alikafka_pre-cn-7pp2t2jwj001:topic:HelloTopic",
21+
"aliyunaccountid": "1938858730552836"
22+
}
23+
]

quick-start-sample-codes/quick-start-sample-codes-python/rabbitmq-trigger-python/publish.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Type: Application
22
Name: rabbitmq-trigger-fc-event-python
33
Provider:
44
- 阿里云
5-
Version: 0.0.1
5+
Version: 0.0.2
66
Description: 快速部署一个具备 RabbitMQ 触发器 Event 类型的函数到阿里云函数计算
77
HomePage: https://github.com/devsapp/start-fc
88
Tags:

0 commit comments

Comments
 (0)