-
Notifications
You must be signed in to change notification settings - Fork 81
/
KafkaEventData.cs
120 lines (101 loc) · 3.76 KB
/
KafkaEventData.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using Confluent.Kafka;
using System;
using System.Linq;
namespace Microsoft.Azure.WebJobs.Extensions.Kafka
{
public class KafkaEventData<TKey, TValue> : IKafkaEventData
{
public TKey Key { get; set; }
public long Offset { get; set; }
public int Partition { get; set; }
public string Topic { get; set; }
public IKafkaEventDataHeaders Headers { get; }
public DateTime Timestamp { get; set; }
public TValue Value { get; set; }
object IKafkaEventData.Value => this.Value;
object IKafkaEventData.Key => this.Key;
public KafkaEventData()
{
this.Headers = new KafkaEventDataHeaders(false);
}
public KafkaEventData(TKey key, TValue value) : this()
{
this.Key = key;
this.Value = value;
}
public KafkaEventData(ConsumeResult<TKey, TValue> consumeResult)
{
this.Key = consumeResult.Key;
this.Value = consumeResult.Value;
this.Offset = consumeResult.Offset;
this.Partition = consumeResult.Partition;
this.Timestamp = consumeResult.Message.Timestamp.UtcDateTime;
this.Topic = consumeResult.Topic;
if (consumeResult.Headers?.Count > 0)
{
this.Headers = new KafkaEventDataHeaders(consumeResult.Message.Headers);
}
else
{
this.Headers = KafkaEventDataHeaders.EmptyReadOnly;
}
}
}
public class KafkaEventData<TValue> : IKafkaEventData
{
public long Offset { get; set; }
public int Partition { get; set; }
public string Topic { get; set; }
public DateTime Timestamp { get; set; }
public TValue Value { get; set; }
public object Key { get; set; }
object IKafkaEventData.Value => this.Value;
object IKafkaEventData.Key => this.Key;
public IKafkaEventDataHeaders Headers { get; private set; }
public KafkaEventData()
{
this.Headers = new KafkaEventDataHeaders(false);
}
public KafkaEventData(TValue value) : this()
{
this.Value = value;
}
internal KafkaEventData(KafkaEventDataHeaders headers)
{
this.Headers = headers;
}
internal static KafkaEventData<TValue> CreateFrom<TKey>(ConsumeResult<TKey, TValue> consumeResult)
{
KafkaEventDataHeaders headers;
if (consumeResult.Headers?.Count > 0)
{
headers = new KafkaEventDataHeaders(consumeResult.Message.Headers);
}
else
{
headers = KafkaEventDataHeaders.EmptyReadOnly;
}
var result = new KafkaEventData<TValue>(headers)
{
Value = consumeResult.Value,
Offset = consumeResult.Offset,
Partition = consumeResult.Partition,
Timestamp = consumeResult.Timestamp.UtcDateTime,
Topic = consumeResult.Topic,
Key = consumeResult.Key
};
return result;
}
public KafkaEventData(IKafkaEventData src)
{
this.Value = (TValue)src.Value;
this.Offset = src.Offset;
this.Partition = src.Partition;
this.Timestamp = src.Timestamp;
this.Topic = src.Topic;
this.Headers = new KafkaEventDataHeaders(src.Headers.Select(x => new KafkaEventDataHeader(x.Key, x.Value)), (src.Headers as KafkaEventDataHeaders)?.IsReadOnly == true);
}
}
}