Skip to content

Commit 0b64945

Browse files
committed
wip add serializer and deserializer for avro
Signed-off-by: Ning Sun <[email protected]>
1 parent 662032e commit 0b64945

File tree

3 files changed

+154
-0
lines changed

3 files changed

+154
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2018-Present The CloudEvents Authors
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package io.cloudevents.avro;
19+
20+
import io.cloudevents.CloudEvent;
21+
import io.cloudevents.SpecVersion;
22+
import io.cloudevents.AvroCloudEvent;
23+
import io.cloudevents.AvroCloudEventData;
24+
import io.cloudevents.core.builder.CloudEventBuilder;
25+
26+
public class AvroDeserializer {
27+
28+
public static CloudEvent fromAvro(AvroCloudEvent avroCloudEvent) {
29+
30+
}
31+
32+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 2018-Present The CloudEvents Authors
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package io.cloudevents.avro;
19+
20+
import java.io.ByteArrayInputStream;
21+
import java.io.ByteArrayOutputStream;
22+
import java.io.IOException;
23+
24+
import io.cloudevents.CloudEvent;
25+
import io.cloudevents.CloudEventData;
26+
import io.cloudevents.AvroCloudEvent;
27+
import io.cloudevents.core.format.EventDeserializationException;
28+
import io.cloudevents.core.format.EventFormat;
29+
import io.cloudevents.core.format.EventSerializationException;
30+
import io.cloudevents.rw.CloudEventDataMapper;
31+
32+
public class AvroFormat implements EventFormat {
33+
34+
public static final String AVRO_CONTENT_TYPE = "application/avro";
35+
36+
@Override
37+
public byte[] serialize(CloudEvent event) throws EventSerializationException {
38+
AvroCloudEvent avroCloudEvent = AvroSerializer.toAvro(event);
39+
ByteArrayOutputStream output = new ByteArrayOutputStream();
40+
41+
try {
42+
AvroCloudEvent.getEncoder().encode(avroCloudEvent, output);
43+
} catch (IOException e) {
44+
throw new EventSerializationException(e);
45+
}
46+
47+
return output.toByteArray();
48+
}
49+
50+
@Override
51+
public CloudEvent deserialize(byte[] bytes, CloudEventDataMapper<? extends CloudEventData> mapper)
52+
throws EventDeserializationException {
53+
ByteArrayInputStream input = new ByteArrayInputStream(bytes);
54+
55+
try {
56+
AvroCloudEvent avroCloudEvent = AvroCloudEvent.getDecoder().decode(input);
57+
58+
return AvroDeserializer.fromAvro(avroCloudEvent);
59+
} catch (IOException e) {
60+
throw new EventDeserializationException(e);
61+
}
62+
}
63+
64+
@Override
65+
public String serializedContentType() {
66+
return AVRO_CONTENT_TYPE;
67+
}
68+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2018-Present The CloudEvents Authors
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package io.cloudevents.avro;
19+
20+
import java.util.Map;
21+
import java.util.HashMap;
22+
23+
import io.cloudevents.CloudEvent;
24+
import io.cloudevents.CloudEventData;
25+
import io.cloudevents.AvroCloudEvent;
26+
import io.cloudevents.AvroCloudEventData;
27+
28+
public class AvroSerializer {
29+
30+
public static final AvroCloudEvent toAvro(CloudEvent e) {
31+
AvroCloudEvent avroCloudEvent = new AvroCloudEvent();
32+
33+
Map<CharSequence, Object> attrs = new HashMap<>();
34+
35+
attrs.put("type", e.getType());
36+
attrs.put("specversion", e.getSpecVersion().toString());
37+
attrs.put("id", e.getId());
38+
attrs.put("source", e.getSource());
39+
attrs.put("time", e.getTime());
40+
attrs.put("dataschema", e.getDataSchema());
41+
attrs.put("contenttype", AvroFormat.AVRO_CONTENT_TYPE);
42+
attrs.put("datacontenttype", e.getDataContentType());
43+
44+
avroCloudEvent.setAttribute(attrs);
45+
46+
// check datacontenttype
47+
CloudEventData cloudEventData = e.getData();
48+
if (cloudEventData != null) {
49+
avroCloudEvent.setData(cloudEventData.toBytes());
50+
}
51+
52+
return avroCloudEvent;
53+
}
54+
}

0 commit comments

Comments
 (0)