-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
573 additions
and
270 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
...-channel-rocketmq/src/main/java/org/apache/rocketmq/streams/schema/AvroSchemaWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package org.apache.rocketmq.streams.schema; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
import org.apache.rocketmq.common.message.MessageExt; | ||
import org.apache.rocketmq.schema.registry.client.SchemaRegistryClient; | ||
import org.apache.rocketmq.schema.registry.client.SchemaRegistryClientFactory; | ||
import org.apache.rocketmq.schema.registry.client.config.AvroSerializerConfig; | ||
import org.apache.rocketmq.schema.registry.client.serde.avro.SpecificAvroSerde; | ||
|
||
public class AvroSchemaWrapper implements SchemaWrapper { | ||
|
||
private static final Log LOG = LogFactory.getLog(AvroSchemaWrapper.class); | ||
|
||
private SpecificAvroSerde avroSerde; | ||
|
||
private SchemaConfig schemaConfig; | ||
|
||
public AvroSchemaWrapper(SchemaConfig schemaConfig) { | ||
this.schemaConfig = schemaConfig; | ||
try { | ||
if (schemaConfig.getSchemaRegistryUrl() == null) { | ||
avroSerde = new SpecificAvroSerde(); | ||
Map<String, Object> configs = new HashMap<>(); | ||
configs.put(AvroSerializerConfig.DESERIALIZE_TARGET_TYPE, Class.forName(schemaConfig.getClassName())); | ||
configs.put(AvroSerializerConfig.SKIP_SCHEMA_REGISTRY, true); | ||
avroSerde.configure(configs); | ||
} else { | ||
SchemaRegistryClient schemaRegistryClient = | ||
SchemaRegistryClientFactory.newClient(schemaConfig.getSchemaRegistryUrl(), null); | ||
avroSerde = new SpecificAvroSerde(schemaRegistryClient); | ||
Map<String, Object> configs = new HashMap<>(); | ||
configs.put(AvroSerializerConfig.DESERIALIZE_TARGET_TYPE, Class.forName(schemaConfig.getClassName())); | ||
avroSerde.configure(configs); | ||
} | ||
} catch (Exception e) { | ||
LOG.error("init AvroSchemaWrapper failed, " + schemaConfig.toString(), e); | ||
throw new RuntimeException("init AvroSchemaWrapper failed"); | ||
} | ||
} | ||
|
||
@Override | ||
public Object deserialize(MessageExt messageExt) { | ||
String subject = messageExt.getTopic(); | ||
byte[] msgBody = messageExt.getBody(); | ||
return avroSerde.deserializer().deserialize(subject, msgBody); | ||
} | ||
|
||
@Override | ||
public SchemaConfig getConfig() { | ||
return schemaConfig; | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
...-channel-rocketmq/src/main/java/org/apache/rocketmq/streams/schema/JsonSchemaWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package org.apache.rocketmq.streams.schema; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
import org.apache.rocketmq.common.message.MessageExt; | ||
import org.apache.rocketmq.schema.registry.client.SchemaRegistryClient; | ||
import org.apache.rocketmq.schema.registry.client.SchemaRegistryClientFactory; | ||
import org.apache.rocketmq.schema.registry.client.config.JsonSerializerConfig; | ||
import org.apache.rocketmq.schema.registry.client.serde.json.JsonSerde; | ||
|
||
/** | ||
* @author huitong | ||
*/ | ||
public class JsonSchemaWrapper implements SchemaWrapper { | ||
|
||
private static final Log LOG = LogFactory.getLog(JsonSchemaWrapper.class); | ||
|
||
private JsonSerde jsonSerde; | ||
|
||
private SchemaConfig schemaConfig; | ||
|
||
public JsonSchemaWrapper(SchemaConfig schemaConfig) { | ||
this.schemaConfig = schemaConfig; | ||
try { | ||
if (schemaConfig.getSchemaRegistryUrl() == null) { | ||
jsonSerde = new JsonSerde(); | ||
Map<String, Object> configs = new HashMap<>(); | ||
configs.put(JsonSerializerConfig.DESERIALIZE_TARGET_TYPE, Class.forName(schemaConfig.getClassName())); | ||
configs.put(JsonSerializerConfig.SKIP_SCHEMA_REGISTRY, true); | ||
jsonSerde.configure(configs); | ||
} else { | ||
SchemaRegistryClient schemaRegistryClient = | ||
SchemaRegistryClientFactory.newClient(schemaConfig.getSchemaRegistryUrl(), null); | ||
jsonSerde = new JsonSerde(schemaRegistryClient); | ||
Map<String, Object> configs = new HashMap<>(); | ||
configs.put(JsonSerializerConfig.DESERIALIZE_TARGET_TYPE, Class.forName(schemaConfig.getClassName())); | ||
jsonSerde.configure(configs); | ||
} | ||
} catch (Exception e) { | ||
LOG.error("init AvroSchemaWrapper failed, " + schemaConfig.toString(), e); | ||
throw new RuntimeException("init AvroSchemaWrapper failed"); | ||
} | ||
} | ||
|
||
@Override | ||
public Object deserialize(MessageExt messageExt) { | ||
|
||
String subject = messageExt.getTopic(); | ||
byte[] msgBody = messageExt.getBody(); | ||
return jsonSerde.deserializer().deserialize(subject, msgBody); | ||
} | ||
|
||
@Override | ||
public SchemaConfig getConfig() { | ||
return schemaConfig; | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
...reams-channel-rocketmq/src/main/java/org/apache/rocketmq/streams/schema/SchemaConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package org.apache.rocketmq.streams.schema; | ||
|
||
import java.io.Serializable; | ||
import org.apache.commons.lang3.StringUtils; | ||
|
||
public class SchemaConfig implements Serializable { | ||
|
||
/** | ||
* json, avro, protobuf, kyro, thrift, etc. | ||
*/ | ||
private String schemaType; | ||
|
||
/** | ||
* allowed to be null, if null means skip schema registry | ||
*/ | ||
private String schemaRegistryUrl; | ||
|
||
/** | ||
* deserialize target class | ||
*/ | ||
private String className; | ||
|
||
public SchemaConfig() { | ||
} | ||
|
||
public SchemaConfig(SchemaType schemaType, Class targetClass) { | ||
this.schemaType = schemaType.name(); | ||
this.className = targetClass.getName(); | ||
} | ||
|
||
public SchemaConfig(SchemaType schemaType, Class targetClass, String schemaRegistryUrl) { | ||
this.schemaType = schemaType.name(); | ||
this.schemaRegistryUrl = schemaRegistryUrl; | ||
this.className = targetClass.getName(); | ||
} | ||
|
||
public String getSchemaType() { | ||
return schemaType; | ||
} | ||
|
||
public void setSchemaType(String schemaType) { | ||
this.schemaType = schemaType; | ||
} | ||
|
||
public String getSchemaRegistryUrl() { | ||
return schemaRegistryUrl; | ||
} | ||
|
||
public void setSchemaRegistryUrl(String schemaRegistryUrl) { | ||
this.schemaRegistryUrl = schemaRegistryUrl; | ||
} | ||
|
||
public String getClassName() { | ||
return className; | ||
} | ||
|
||
public void setClassName(String className) { | ||
this.className = className; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "SchemaConfig{" + | ||
"schemaType='" + schemaType + '\'' + | ||
", schemaRegistryUrl='" + schemaRegistryUrl + '\'' + | ||
", className='" + className + '\'' + | ||
'}'; | ||
} | ||
|
||
public boolean equals(SchemaConfig configToCompare) { | ||
if (!StringUtils.equals(getSchemaType(), configToCompare.getSchemaType())) { | ||
return false; | ||
} | ||
if (!StringUtils.equals(getClassName(), configToCompare.getClassName())) { | ||
return false; | ||
} | ||
if (!StringUtils.equals(getSchemaRegistryUrl(), configToCompare.getSchemaRegistryUrl())) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...streams-channel-rocketmq/src/main/java/org/apache/rocketmq/streams/schema/SchemaType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package org.apache.rocketmq.streams.schema; | ||
|
||
public enum SchemaType { | ||
|
||
/** | ||
* Avro type | ||
*/ | ||
AVRO("AVRO"), | ||
/** | ||
* Protobuf type | ||
*/ | ||
PROTOBUF("PROTOBUF"), | ||
/** | ||
* Thrift type | ||
*/ | ||
THRIFT("THRIFT"), | ||
/** | ||
* Json type | ||
*/ | ||
JSON("JSON"), | ||
/** | ||
* Text type for reserved | ||
*/ | ||
TEXT("TEXT"), | ||
/** | ||
* Binlog type for reserved | ||
*/ | ||
BINLOG("BINLOG"); | ||
|
||
private final String value; | ||
|
||
SchemaType(final String value) { | ||
this.value = value; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...eams-channel-rocketmq/src/main/java/org/apache/rocketmq/streams/schema/SchemaWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package org.apache.rocketmq.streams.schema; | ||
|
||
import org.apache.rocketmq.common.message.MessageExt; | ||
|
||
public interface SchemaWrapper { | ||
|
||
Object deserialize(MessageExt messageExt); | ||
|
||
SchemaConfig getConfig(); | ||
} |
31 changes: 31 additions & 0 deletions
31
...annel-rocketmq/src/main/java/org/apache/rocketmq/streams/schema/SchemaWrapperFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package org.apache.rocketmq.streams.schema; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class SchemaWrapperFactory { | ||
|
||
private static Map<String /*topic*/, SchemaWrapper> schemaWrapperCache = new HashMap<>(); | ||
|
||
public static SchemaWrapper createIfAbsent(String topic, SchemaConfig schemaConfig) { | ||
SchemaWrapper schemaWrapper = schemaWrapperCache.get(topic); | ||
if (schemaWrapper != null) { | ||
// if change config, also need to create a new one | ||
if (schemaConfig.equals(schemaWrapper.getConfig())) { | ||
return schemaWrapper; | ||
} | ||
} | ||
if (SchemaType.JSON.name().equals(schemaConfig.getSchemaType())) { | ||
JsonSchemaWrapper jsonSchemaWrapper = new JsonSchemaWrapper(schemaConfig); | ||
schemaWrapperCache.put(topic, jsonSchemaWrapper); | ||
return jsonSchemaWrapper; | ||
} else if (SchemaType.AVRO.name().equals(schemaConfig.getSchemaType())) { | ||
AvroSchemaWrapper avroSchemaWrapper = new AvroSchemaWrapper(schemaConfig); | ||
schemaWrapperCache.put(topic, avroSchemaWrapper); | ||
return avroSchemaWrapper; | ||
} else { | ||
throw new RuntimeException("scheme type " + schemaConfig.getSchemaType() + " not supported"); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.