Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2859891

Browse files
committed
Consumer: add custom deserializer product for kafka
1 parenta86a8da commit2859891

File tree

4 files changed

+190
-0
lines changed

4 files changed

+190
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2019 wuriyanto.com
3+
*
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+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
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+
packagecom.telkomdev.consumer.deserializer;
18+
19+
importcom.google.gson.Gson;
20+
importcom.google.gson.GsonBuilder;
21+
22+
publicclassJsonParser<T> {
23+
24+
privatefinalGsongson =newGsonBuilder().setPrettyPrinting().create();
25+
privateTt;
26+
privateClass<T>clazz;
27+
28+
publicJsonParser(Tt,Class<T>clazz) {
29+
this.t =t;
30+
this.clazz =clazz;
31+
}
32+
33+
publicJsonParser(Class<T>clazz) {
34+
this.clazz =clazz;
35+
}
36+
37+
publicbyte[]serialize() {
38+
returngson.toJson(t).getBytes();
39+
}
40+
41+
publicTdeserialize(byte[]in) {
42+
returngson.fromJson(newString(in),clazz);
43+
}
44+
45+
publicvoidsetEntity(Tt) {
46+
this.t =t;
47+
}
48+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2019 wuriyanto.com
3+
*
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+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
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+
packagecom.telkomdev.consumer.deserializer;
18+
19+
importcom.telkomdev.consumer.model.Product;
20+
importorg.apache.kafka.common.errors.SerializationException;
21+
importorg.apache.kafka.common.serialization.Deserializer;
22+
23+
importjava.io.IOException;
24+
importjava.util.Map;
25+
26+
publicclassProductAvroDeserializerimplementsDeserializer<Product> {
27+
28+
publicProductAvroDeserializer() {
29+
}
30+
31+
@Override
32+
publicvoidconfigure(Map<String, ?>configs,booleanisKey) {
33+
34+
}
35+
36+
@Override
37+
publicProductdeserialize(Strings,byte[]data) {
38+
try {
39+
returndata ==null ?null :Product.fromAvro(data);
40+
}catch (IOExceptione) {
41+
thrownewSerializationException("Error when serializing");
42+
}
43+
}
44+
45+
@Override
46+
publicvoidclose() {
47+
48+
}
49+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2019 wuriyanto.com
3+
*
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+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
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+
packagecom.telkomdev.consumer.deserializer;
18+
19+
importcom.telkomdev.consumer.model.Product;
20+
importorg.apache.kafka.common.serialization.Deserializer;
21+
22+
importjava.util.Map;
23+
24+
publicclassProductJsonDeserializerimplementsDeserializer<Product> {
25+
26+
publicProductJsonDeserializer() {
27+
}
28+
29+
@Override
30+
publicvoidconfigure(Map<String, ?>configs,booleanisKey) {
31+
32+
}
33+
34+
@Override
35+
publicProductdeserialize(Strings,byte[]data) {
36+
returndata ==null ?null :Product.fromJson(data);
37+
}
38+
39+
@Override
40+
publicvoidclose() {
41+
42+
}
43+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2019 wuriyanto.com
3+
*
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+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
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+
packagecom.telkomdev.consumer.deserializer;
18+
19+
importcom.google.protobuf.InvalidProtocolBufferException;
20+
importcom.telkomdev.consumer.model.Product;
21+
importorg.apache.kafka.common.errors.SerializationException;
22+
importorg.apache.kafka.common.serialization.Deserializer;
23+
24+
importjava.util.Map;
25+
26+
publicclassProductProtobufDeserializerimplementsDeserializer<Product> {
27+
28+
29+
publicProductProtobufDeserializer() {
30+
}
31+
32+
@Override
33+
publicvoidconfigure(Map<String, ?>configs,booleanisKey) {
34+
35+
}
36+
37+
@Override
38+
publicProductdeserialize(Strings,byte[]data) {
39+
try {
40+
returndata ==null ?null :Product.fromProto(data);
41+
}catch (InvalidProtocolBufferExceptionex) {
42+
thrownewSerializationException("Error when serializing");
43+
}
44+
}
45+
46+
@Override
47+
publicvoidclose() {
48+
49+
}
50+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp