Search in sources :

Example 1 with SerializableSerde

use of org.apache.samza.serializers.SerializableSerde in project samza by apache.

the class TestJobNodeConfigurationGenerator method validateAndGetDeserializedSerdes.

private Map<String, Serde> validateAndGetDeserializedSerdes(Config jobConfig, int numSerdes) {
    Config serializers = jobConfig.subset("serializers.registry.", true);
    // make sure that the serializers deserialize correctly
    SerializableSerde<Serde> serializableSerde = new SerializableSerde<>();
    assertEquals(numSerdes, serializers.size());
    return serializers.entrySet().stream().collect(Collectors.toMap(e -> e.getKey().replace(SerializerConfig.SERIALIZED_INSTANCE_SUFFIX, ""), e -> serializableSerde.fromBytes(Base64.getDecoder().decode(e.getValue().getBytes()))));
}
Also used : Serde(org.apache.samza.serializers.Serde) StringSerde(org.apache.samza.serializers.StringSerde) SerializableSerde(org.apache.samza.serializers.SerializableSerde) TimestampedValueSerde(org.apache.samza.operators.impl.store.TimestampedValueSerde) Arrays(java.util.Arrays) TaskApplicationDescriptorImpl(org.apache.samza.application.descriptors.TaskApplicationDescriptorImpl) StreamApplicationDescriptorImpl(org.apache.samza.application.descriptors.StreamApplicationDescriptorImpl) TableDescriptor(org.apache.samza.table.descriptors.TableDescriptor) JobConfig(org.apache.samza.config.JobConfig) Serde(org.apache.samza.serializers.Serde) HashMap(java.util.HashMap) GenericInputDescriptor(org.apache.samza.system.descriptors.GenericInputDescriptor) StringSerde(org.apache.samza.serializers.StringSerde) HashSet(java.util.HashSet) BaseTableDescriptor(org.apache.samza.table.descriptors.BaseTableDescriptor) Map(java.util.Map) MapConfig(org.apache.samza.config.MapConfig) KV(org.apache.samza.operators.KV) MockTableProviderFactory(org.apache.samza.table.descriptors.TestLocalTableDescriptor.MockTableProviderFactory) SerializableSerde(org.apache.samza.serializers.SerializableSerde) SerializerConfig(org.apache.samza.config.SerializerConfig) ImmutableMap(com.google.common.collect.ImmutableMap) TaskConfig(org.apache.samza.config.TaskConfig) ConfigRewriter(org.apache.samza.config.ConfigRewriter) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) MockLocalTableDescriptor(org.apache.samza.table.descriptors.TestLocalTableDescriptor.MockLocalTableDescriptor) StreamSpec(org.apache.samza.system.StreamSpec) Test(org.junit.Test) SideInputsProcessor(org.apache.samza.storage.SideInputsProcessor) Collectors(java.util.stream.Collectors) SamzaException(org.apache.samza.SamzaException) TimestampedValueSerde(org.apache.samza.operators.impl.store.TimestampedValueSerde) Mockito(org.mockito.Mockito) Base64(java.util.Base64) Config(org.apache.samza.config.Config) JsonSerdeV2(org.apache.samza.serializers.JsonSerdeV2) Assert.assertEquals(org.junit.Assert.assertEquals) Joiner(com.google.common.base.Joiner) JobConfig(org.apache.samza.config.JobConfig) MapConfig(org.apache.samza.config.MapConfig) SerializerConfig(org.apache.samza.config.SerializerConfig) TaskConfig(org.apache.samza.config.TaskConfig) Config(org.apache.samza.config.Config) SerializableSerde(org.apache.samza.serializers.SerializableSerde)

Example 2 with SerializableSerde

use of org.apache.samza.serializers.SerializableSerde in project samza by apache.

the class TableConfigGenerator method generateSerdeConfig.

/**
 * Generate serde configuration for provided tables
 *
 * @param tableDescriptors table descriptors, for which serde configuration to be generated
 * @return serde configuration for tables
 */
public static Map<String, String> generateSerdeConfig(List<TableDescriptor> tableDescriptors) {
    Map<String, String> serdeConfigs = new HashMap<>();
    // Collect key and msg serde instances for all the tables
    Map<String, Serde> tableKeySerdes = new HashMap<>();
    Map<String, Serde> tableValueSerdes = new HashMap<>();
    HashSet<Serde> serdes = new HashSet<>();
    tableDescriptors.stream().filter(d -> d instanceof LocalTableDescriptor).forEach(d -> {
        LocalTableDescriptor ld = (LocalTableDescriptor) d;
        tableKeySerdes.put(ld.getTableId(), ld.getSerde().getKeySerde());
        tableValueSerdes.put(ld.getTableId(), ld.getSerde().getValueSerde());
    });
    serdes.addAll(tableKeySerdes.values());
    serdes.addAll(tableValueSerdes.values());
    // Generate serde names
    SerializableSerde<Serde> serializableSerde = new SerializableSerde<>();
    Base64.Encoder base64Encoder = Base64.getEncoder();
    Map<Serde, String> serdeUUIDs = new HashMap<>();
    serdes.forEach(serde -> {
        String serdeName = serdeUUIDs.computeIfAbsent(serde, s -> serde.getClass().getSimpleName() + "-" + UUID.randomUUID().toString());
        serdeConfigs.putIfAbsent(String.format(SerializerConfig.SERDE_SERIALIZED_INSTANCE, serdeName), base64Encoder.encodeToString(serializableSerde.toBytes(serde)));
    });
    // Set key and msg serdes for tables to the serde names generated above
    tableKeySerdes.forEach((tableId, serde) -> {
        String keySerdeConfigKey = String.format(JavaTableConfig.STORE_KEY_SERDE, tableId);
        serdeConfigs.put(keySerdeConfigKey, serdeUUIDs.get(serde));
    });
    tableValueSerdes.forEach((tableId, serde) -> {
        String valueSerdeConfigKey = String.format(JavaTableConfig.STORE_MSG_SERDE, tableId);
        serdeConfigs.put(valueSerdeConfigKey, serdeUUIDs.get(serde));
    });
    return serdeConfigs;
}
Also used : Serde(org.apache.samza.serializers.Serde) SerializableSerde(org.apache.samza.serializers.SerializableSerde) Logger(org.slf4j.Logger) SerializerConfig(org.apache.samza.config.SerializerConfig) TableDescriptor(org.apache.samza.table.descriptors.TableDescriptor) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) Serde(org.apache.samza.serializers.Serde) LocalTableDescriptor(org.apache.samza.table.descriptors.LocalTableDescriptor) UUID(java.util.UUID) JavaTableConfig(org.apache.samza.config.JavaTableConfig) HashSet(java.util.HashSet) Base64(java.util.Base64) List(java.util.List) Map(java.util.Map) Config(org.apache.samza.config.Config) SerializableSerde(org.apache.samza.serializers.SerializableSerde) LocalTableDescriptor(org.apache.samza.table.descriptors.LocalTableDescriptor) Base64(java.util.Base64) HashMap(java.util.HashMap) SerializableSerde(org.apache.samza.serializers.SerializableSerde) HashSet(java.util.HashSet)

Aggregations

Base64 (java.util.Base64)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 Config (org.apache.samza.config.Config)2 SerializerConfig (org.apache.samza.config.SerializerConfig)2 Serde (org.apache.samza.serializers.Serde)2 SerializableSerde (org.apache.samza.serializers.SerializableSerde)2 TableDescriptor (org.apache.samza.table.descriptors.TableDescriptor)2 Joiner (com.google.common.base.Joiner)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Set (java.util.Set)1 UUID (java.util.UUID)1 Collectors (java.util.stream.Collectors)1 SamzaException (org.apache.samza.SamzaException)1 StreamApplicationDescriptorImpl (org.apache.samza.application.descriptors.StreamApplicationDescriptorImpl)1 TaskApplicationDescriptorImpl (org.apache.samza.application.descriptors.TaskApplicationDescriptorImpl)1 ConfigRewriter (org.apache.samza.config.ConfigRewriter)1