io.confluent.kafka.serializers.AbstractKafkaAvroSerDeConfig Java Examples

The following examples show how to use io.confluent.kafka.serializers.AbstractKafkaAvroSerDeConfig. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RunningAverage.java    From kafka-tutorials with Apache License 2.0 6 votes vote down vote up
protected Properties buildStreamsProperties(Properties envProps) {
  Properties config = new Properties();
  config.putAll(envProps);

  config.put(APPLICATION_ID_CONFIG, envProps.getProperty("application.id"));
  config.put(BOOTSTRAP_SERVERS_CONFIG, envProps.getProperty("bootstrap.servers"));
  config.put(DEFAULT_KEY_SERDE_CLASS_CONFIG, Long().getClass());
  config.put(DEFAULT_VALUE_SERDE_CLASS_CONFIG, Double().getClass());
  config.put(AbstractKafkaAvroSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG, envProps.getProperty("schema.registry.url"));

  config.put(REPLICATION_FACTOR_CONFIG, envProps.getProperty("default.topic.replication.factor"));
  config.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, envProps.getProperty("offset.reset.policy"));

  config.put(StreamsConfig.CACHE_MAX_BYTES_BUFFERING_CONFIG, 0);

  return config;
}
 
Example #2
Source File: ConfluentRegistryCompatibleResourceTest.java    From registry with Apache License 2.0 6 votes vote down vote up
@Test
public void testConfluentSerDes() throws Exception {

    org.apache.avro.Schema schema = new org.apache.avro.Schema.Parser().parse(GENERIC_TEST_RECORD_SCHEMA);
    GenericRecord record = new GenericRecordBuilder(schema).set("field1", "some value").set("field2", "some other value").build();

    Map<String, Object> config = new HashMap<>();
    config.put(AbstractKafkaAvroSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG, rootTarget.getUri().toString());

    KafkaAvroSerializer kafkaAvroSerializer = new KafkaAvroSerializer();
    kafkaAvroSerializer.configure(config, false);
    byte[] bytes = kafkaAvroSerializer.serialize("topic", record);

    KafkaAvroDeserializer kafkaAvroDeserializer = new KafkaAvroDeserializer();
    kafkaAvroDeserializer.configure(config, false);

    GenericRecord result = (GenericRecord) kafkaAvroDeserializer.deserialize("topic", bytes);
    LOG.info(result.toString());
}
 
Example #3
Source File: KafkaAvroTest.java    From quarkus with Apache License 2.0 6 votes vote down vote up
public static KafkaConsumer<Integer, Pet> createConsumer() {
    String registry = System.getProperty("schema.url");

    Properties props = new Properties();
    props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:19092");
    props.put(ConsumerConfig.GROUP_ID_CONFIG, "test-avro");
    props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, IntegerDeserializer.class.getName());
    props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, KafkaAvroDeserializer.class.getName());
    props.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, "true");
    props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
    props.put(AbstractKafkaAvroSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG, registry);

    // Without you get GenericData.Record instead of `Pet`
    props.put(KafkaAvroDeserializerConfig.SPECIFIC_AVRO_READER_CONFIG, true);

    KafkaConsumer<Integer, Pet> consumer = new KafkaConsumer<>(props);
    consumer.subscribe(Collections.singletonList("test-avro-producer"));
    return consumer;
}
 
Example #4
Source File: KafkaClients.java    From apicurio-registry with Apache License 2.0 6 votes vote down vote up
public static Producer<Object, ?> createProducer(Properties props, String keySerializer,
        String valueSerializer, String topicName, String artifactIdStrategy) {
    props.putIfAbsent(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers());
    props.putIfAbsent(ProducerConfig.CLIENT_ID_CONFIG, "Producer-" + topicName);
    props.putIfAbsent(ProducerConfig.ACKS_CONFIG, "all");
    props.putIfAbsent(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, keySerializer);
    props.putIfAbsent(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, valueSerializer);
    // Schema Registry location.
    if (valueSerializer.contains("confluent")) {
        props.putIfAbsent(KafkaAvroSerializerConfig.SCHEMA_REGISTRY_URL_CONFIG, TestUtils.getRegistryUrl() + "/ccompat");
        props.putIfAbsent(AbstractKafkaAvroSerDeConfig.AUTO_REGISTER_SCHEMAS, "false");
        props.putIfAbsent(KafkaAvroSerializerConfig.VALUE_SUBJECT_NAME_STRATEGY, artifactIdStrategy);
    } else {
        props.putIfAbsent(AbstractKafkaSerDe.REGISTRY_URL_CONFIG_PARAM, TestUtils.getRegistryUrl());
        props.putIfAbsent(AbstractKafkaSerializer.REGISTRY_ARTIFACT_ID_STRATEGY_CONFIG_PARAM, artifactIdStrategy);
    }

    return new KafkaProducer<>(props);
}
 
Example #5
Source File: TumblingWindow.java    From kafka-tutorials with Apache License 2.0 6 votes vote down vote up
public Properties buildStreamsProperties(Properties envProps) {
    Properties props = new Properties();

    props.put(StreamsConfig.APPLICATION_ID_CONFIG, envProps.getProperty("application.id"));
    props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, envProps.getProperty("bootstrap.servers"));
    props.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass());
    props.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, SpecificAvroSerde.class);
    props.put(AbstractKafkaAvroSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG, envProps.getProperty("schema.registry.url"));
    props.put(StreamsConfig.DEFAULT_TIMESTAMP_EXTRACTOR_CLASS_CONFIG, RatingTimestampExtractor.class.getName());
    props.put(StreamsConfig.CACHE_MAX_BYTES_BUFFERING_CONFIG, 0);
    try {
        props.put(StreamsConfig.STATE_DIR_CONFIG,
                  Files.createTempDirectory("tumbling-windows").toAbsolutePath().toString());
    }
    catch(IOException e) {
        // If we can't have our own temporary directory, we can leave it with the default. We create a custom
        // one because running the app outside of Docker multiple times in quick succession will find the
        // previous state still hanging around in /tmp somewhere, which is not the expected result.
    }
    return props;
}
 
Example #6
Source File: KsqlGenericRowAvroDeserializerTest.java    From ksql-fork-with-deep-learning-function with Apache License 2.0 6 votes vote down vote up
private byte[] getSerializedRow(String topicName, SchemaRegistryClient schemaRegistryClient,
                                Schema rowAvroSchema,
                                GenericRow
    genericRow) {
  Map map = new HashMap();
  // Automatically register the schema in the Schema Registry if it has not been registered.
  map.put(AbstractKafkaAvroSerDeConfig.AUTO_REGISTER_SCHEMAS, true);
  map.put(AbstractKafkaAvroSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG, "");
  KafkaAvroSerializer kafkaAvroSerializer = new KafkaAvroSerializer(schemaRegistryClient, map);
  GenericRecord avroRecord = new GenericData.Record(rowAvroSchema);
  List<Schema.Field> fields = rowAvroSchema.getFields();
  for (int i = 0; i < genericRow.getColumns().size(); i++) {
    if (fields.get(i).schema().getType() == Schema.Type.ARRAY) {
      avroRecord.put(fields.get(i).name(), Arrays.asList((Object[]) genericRow.getColumns().get(i)));
    } else {
      avroRecord.put(fields.get(i).name(), genericRow.getColumns().get(i));
    }
  }

  return kafkaAvroSerializer.serialize(topicName, avroRecord);
}
 
Example #7
Source File: KsqlGenericRowAvroSerializer.java    From ksql-fork-with-deep-learning-function with Apache License 2.0 6 votes vote down vote up
public KsqlGenericRowAvroSerializer(
    org.apache.kafka.connect.data.Schema schema,
    SchemaRegistryClient schemaRegistryClient, KsqlConfig
    ksqlConfig
) {
  String avroSchemaStr = SchemaUtil.buildAvroSchema(schema, "avro_schema");
  
  Schema.Parser parser = new Schema.Parser();
  avroSchema = parser.parse(avroSchemaStr);
  fields = avroSchema.getFields();

  Map<String, Object> map = new HashMap<>();

  // Automatically register the schema in the Schema Registry if it has not been registered.
  map.put(AbstractKafkaAvroSerDeConfig.AUTO_REGISTER_SCHEMAS, true);
  map.put(
      AbstractKafkaAvroSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG,
      ksqlConfig.getString(KsqlConfig.SCHEMA_REGISTRY_URL_PROPERTY)
  );
  kafkaAvroSerializer = new KafkaAvroSerializer(schemaRegistryClient, map);

}
 
Example #8
Source File: Producer.java    From snowflake-kafka-connector with Apache License 2.0 6 votes vote down vote up
Properties getProperties(String valueSerializer)
{
  Properties props = new Properties();
  props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, URL);
  props.put(ProducerConfig.CLIENT_ID_CONFIG, CLIENT_ID);
  props.put(
    ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,
    org.apache.kafka.common.serialization.StringSerializer.class.getCanonicalName()
  );
  props.put(
    ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, valueSerializer
  );

  if(useSchemaRegistry())
  {
    props.put(AbstractKafkaAvroSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG, "http://localhost:8081");
  }
  return props;
}
 
Example #9
Source File: PosSimulator.java    From Kafka-Streams-Real-time-Stream-Processing with The Unlicense 5 votes vote down vote up
public static void main(String[] args) {
    if (args.length < 3) {
        System.out.println("Please provide command line arguments: topicName noOfProducers produceSpeed");
        System.exit(-1);
    }
    String topicName = args[0];
    int noOfProducers = new Integer(args[1]);
    int produceSpeed = new Integer(args[2]);
    Properties properties = new Properties();
    properties.put(ProducerConfig.CLIENT_ID_CONFIG, "StockSimulator");
    properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092,localhost:9093");
    properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
    properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, KafkaAvroSerializer.class.getName());
    properties.put(AbstractKafkaAvroSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG, "http://localhost:8081");

    KafkaProducer<String, PosInvoice> kafkaProducer = new KafkaProducer<>(properties);
    ExecutorService executor = Executors.newFixedThreadPool(3);
    final List<RunnableProducer> runnableProducers = new ArrayList<>();
    for (int i = 0; i < noOfProducers; i++) {
        RunnableProducer runnableProducer = new RunnableProducer(i, kafkaProducer, topicName, produceSpeed);
        runnableProducers.add(runnableProducer);
        executor.submit(runnableProducer);
    }

    Runtime.getRuntime().addShutdownHook(new Thread(() -> {
        for (RunnableProducer p : runnableProducers)
            p.shutdown();
        executor.shutdown();
        logger.info("Closing Executor Service");
        try {
            executor.awaitTermination(produceSpeed * 2, TimeUnit.MILLISECONDS);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }));

}
 
Example #10
Source File: StreamsIngest.java    From kafka-tutorials with Apache License 2.0 5 votes vote down vote up
public Properties buildStreamsProperties(Properties envProps) {
  Properties props = new Properties();

  //props.put(StreamsConfig.APPLICATION_ID_CONFIG, envProps.getProperty("application.id"));
  props.put(StreamsConfig.APPLICATION_ID_CONFIG, "foo2");
  props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
  props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, envProps.getProperty("bootstrap.servers"));
  props.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass());
  props.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes.String().getClass());
  props.put(AbstractKafkaAvroSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG, envProps.getProperty("schema.registry.url"));
  props.put(StreamsConfig.CACHE_MAX_BYTES_BUFFERING_CONFIG, 0);

  return props;
}
 
Example #11
Source File: StreamsIngest.java    From kafka-tutorials with Apache License 2.0 5 votes vote down vote up
private SpecificAvroSerde<City> citySerde(final Properties envProps) {
  final SpecificAvroSerde<City> serde = new SpecificAvroSerde<>();
  Map<String, String> config = new HashMap<>();
  config.put(AbstractKafkaAvroSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG, envProps.getProperty("schema.registry.url"));
  serde.configure(config, false);
  return serde;
}
 
Example #12
Source File: DynamicOutputTopic.java    From kafka-tutorials with Apache License 2.0 5 votes vote down vote up
public Properties buildStreamsProperties(Properties envProps) {
    Properties props = new Properties();

    props.put(StreamsConfig.APPLICATION_ID_CONFIG, envProps.getProperty("application.id"));
    props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, envProps.getProperty("bootstrap.servers"));
    props.put(StreamsConfig.STATE_DIR_CONFIG, TestUtils.tempDirectory().getPath());
    props.put(AbstractKafkaAvroSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG, envProps.getProperty("schema.registry.url"));

    return props;
}
 
Example #13
Source File: DynamicOutputTopic.java    From kafka-tutorials with Apache License 2.0 5 votes vote down vote up
static <T extends SpecificRecord> SpecificAvroSerde<T> getSpecificAvroSerde(final Properties envProps) {
    final SpecificAvroSerde<T> specificAvroSerde = new SpecificAvroSerde<>();

    final HashMap<String, String> serdeConfig = new HashMap<>();
    serdeConfig.put(AbstractKafkaAvroSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG,
            envProps.getProperty("schema.registry.url"));

    specificAvroSerde.configure(serdeConfig, false);
    return specificAvroSerde;
}
 
Example #14
Source File: FilterEvents.java    From kafka-tutorials with Apache License 2.0 5 votes vote down vote up
public Properties buildStreamsProperties(Properties envProps) {
  Properties props = new Properties();

  props.put(StreamsConfig.APPLICATION_ID_CONFIG, envProps.getProperty("application.id"));
  props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, envProps.getProperty("bootstrap.servers"));
  props.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass());
  props.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes.String().getClass());
  props.put(AbstractKafkaAvroSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG, envProps.getProperty("schema.registry.url"));
  props.put(StreamsConfig.CACHE_MAX_BYTES_BUFFERING_CONFIG, 0);

  return props;
}
 
Example #15
Source File: FilterEvents.java    From kafka-tutorials with Apache License 2.0 5 votes vote down vote up
private SpecificAvroSerde<Publication> publicationSerde(final Properties envProps) {
  final SpecificAvroSerde<Publication> serde = new SpecificAvroSerde<>();
  Map<String, String> config = new HashMap<>();
  config.put(AbstractKafkaAvroSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG, envProps.getProperty("schema.registry.url"));
  serde.configure(config, false);
  return serde;
}
 
Example #16
Source File: SchemaRegistryTransfer.java    From schema-registry-transfer-smt with Apache License 2.0 5 votes vote down vote up
@Override
public void configure(Map<String, ?> props) {
    SimpleConfig config = new SimpleConfig(CONFIG_DEF, props);

    List<String> sourceUrls = config.getList(ConfigName.SRC_SCHEMA_REGISTRY_URL);
    final Map<String, String> sourceProps = new HashMap<>();
    sourceProps.put(AbstractKafkaAvroSerDeConfig.BASIC_AUTH_CREDENTIALS_SOURCE,
        "SRC_" + config.getString(ConfigName.SRC_BASIC_AUTH_CREDENTIALS_SOURCE));
    sourceProps.put(AbstractKafkaAvroSerDeConfig.USER_INFO_CONFIG,
        config.getPassword(ConfigName.SRC_USER_INFO)
            .value());

    List<String> destUrls = config.getList(ConfigName.DEST_SCHEMA_REGISTRY_URL);
    final Map<String, String> destProps = new HashMap<>();
    destProps.put(AbstractKafkaAvroSerDeConfig.BASIC_AUTH_CREDENTIALS_SOURCE,
        "DEST_" + config.getString(ConfigName.DEST_BASIC_AUTH_CREDENTIALS_SOURCE));
    destProps.put(AbstractKafkaAvroSerDeConfig.USER_INFO_CONFIG,
        config.getPassword(ConfigName.DEST_USER_INFO)
            .value());

    Integer schemaCapacity = config.getInt(ConfigName.SCHEMA_CAPACITY);

    this.schemaCache = new SynchronizedCache<>(new LRUCache<>(schemaCapacity));
    this.sourceSchemaRegistryClient = new CachedSchemaRegistryClient(sourceUrls, schemaCapacity, sourceProps);
    this.destSchemaRegistryClient = new CachedSchemaRegistryClient(destUrls, schemaCapacity, destProps);

    this.transferKeys = config.getBoolean(ConfigName.TRANSFER_KEYS);
    this.includeHeaders = config.getBoolean(ConfigName.INCLUDE_HEADERS);

    // TODO: Make the Strategy configurable, may be different for src and dest
    // Strategy for the -key and -value subjects
    this.subjectNameStrategy = new TopicNameStrategy();
}
 
Example #17
Source File: DynamicOutputTopic.java    From kafka-tutorials with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
static <T> Serde<T> getPrimitiveAvroSerde(final Properties envProps, boolean isKey) {
    final KafkaAvroDeserializer deserializer = new KafkaAvroDeserializer();
    final KafkaAvroSerializer serializer = new KafkaAvroSerializer();
    final Map<String, String> config = new HashMap<>();
    config.put(AbstractKafkaAvroSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG,
            envProps.getProperty("schema.registry.url"));
    deserializer.configure(config, isKey);
    serializer.configure(config, isKey);
    return (Serde<T>)Serdes.serdeFrom(serializer, deserializer);
}
 
Example #18
Source File: AvroConsumer.java    From Kafka-Streams-Real-time-Stream-Processing with The Unlicense 5 votes vote down vote up
/**
 * Application entry point
 *
 * @param args topicName and groupName
 */
@SuppressWarnings("InfiniteLoopStatement")
public static void main(String[] args) {

    if (args.length < 2) {
        System.out.println("Please provide command line arguments: topicName groupName");
        System.exit(-1);
    }
    String topicName = args[0];
    String groupName = args[1];

    Properties properties = new Properties();
    try {
        InputStream kafkaConfigStream = ClassLoader.class.getResourceAsStream(kafkaConfig);
        properties.load(kafkaConfigStream);
        properties.put(ConsumerConfig.GROUP_ID_CONFIG, groupName);
        //Set autocommit to false so you can execute it again for the same set of messages
        properties.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, "false");
        properties.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
        properties.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
        properties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, KafkaAvroDeserializer.class);
        properties.put(KafkaAvroDeserializerConfig.SPECIFIC_AVRO_READER_CONFIG, true);
        properties.put(AbstractKafkaAvroSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG, "http://localhost:8081");

    } catch (IOException e) {
        logger.error(e.getMessage());
        throw new RuntimeException(e);
    }

    final KafkaConsumer<String, StockData> consumer = new KafkaConsumer<>(properties);
    consumer.subscribe(Collections.singletonList(topicName));
    while (true) {
        ConsumerRecords<String, StockData> records = consumer.poll(Duration.ofMillis(100));
        for (ConsumerRecord<String, StockData> record : records) {
            System.out.println(record.value());
        }
    }
}
 
Example #19
Source File: AvroGenericUtils.java    From simplesource with Apache License 2.0 5 votes vote down vote up
private static Map<String, Object> avroSchemaRegistryConfig(String schemaRegistryUrl, SchemaNameStrategy schemaNameStrategy) {
    final Map<String, Object> configMap = new HashMap<>();
    configMap.put(AbstractKafkaAvroSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG, schemaRegistryUrl);
    if (schemaNameStrategy == SchemaNameStrategy.TOPIC_RECORD_NAME) {
        configMap.put(AbstractKafkaAvroSerDeConfig.KEY_SUBJECT_NAME_STRATEGY, TopicRecordNameStrategy.class);
        configMap.put(AbstractKafkaAvroSerDeConfig.VALUE_SUBJECT_NAME_STRATEGY, TopicRecordNameStrategy.class);
    }
    return configMap;
}
 
Example #20
Source File: AvroEndpoint.java    From quarkus with Apache License 2.0 5 votes vote down vote up
public static KafkaConsumer<Integer, Pet> createConsumer(String registry) {
    Properties props = new Properties();
    props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:19092");
    props.put(ConsumerConfig.GROUP_ID_CONFIG, "test-avro-consumer");
    props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, IntegerDeserializer.class.getName());
    props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, KafkaAvroDeserializer.class.getName());
    props.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, "true");
    props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
    props.put(AbstractKafkaAvroSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG, registry);
    props.put(KafkaAvroDeserializerConfig.SPECIFIC_AVRO_READER_CONFIG, true);
    KafkaConsumer<Integer, Pet> consumer = new KafkaConsumer<>(props);
    consumer.subscribe(Collections.singletonList("test-avro-consumer"));
    return consumer;
}
 
Example #21
Source File: AvroEndpoint.java    From quarkus with Apache License 2.0 5 votes vote down vote up
public static KafkaProducer<Integer, Pet> createProducer(String registry) {
    Properties props = new Properties();
    props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:19092");
    props.put(ProducerConfig.CLIENT_ID_CONFIG, "test-avro");
    props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, IntegerSerializer.class.getName());
    props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, KafkaAvroSerializer.class.getName());
    props.put(AbstractKafkaAvroSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG, registry);
    return new KafkaProducer<>(props);
}
 
Example #22
Source File: KafkaAvroTest.java    From quarkus with Apache License 2.0 5 votes vote down vote up
public static KafkaProducer<Integer, Pet> createProducer() {
    String registry = System.getProperty("schema.url");

    Properties props = new Properties();
    props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:19092");
    props.put(ProducerConfig.CLIENT_ID_CONFIG, "test-avro");
    props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, IntegerSerializer.class.getName());
    props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, KafkaAvroSerializer.class.getName());
    props.put(AbstractKafkaAvroSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG, registry);
    return new KafkaProducer<>(props);
}
 
Example #23
Source File: CountVersionApplication.java    From spring-cloud-stream-samples with Apache License 2.0 5 votes vote down vote up
@Bean
public Function<KStream<Object, Sensor>, KStream<String, Long>> process() {

	//The following Serde definitions are not needed in the topoloyy below
	//as we are not using it. However, if your topoloyg explicitly uses this
	//Serde, you need to configure this with the schema registry url as below.

	final Map<String, String> serdeConfig = Collections.singletonMap(
			AbstractKafkaAvroSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG, "http://localhost:8081");

	final SpecificAvroSerde<Sensor> sensorSerde = new SpecificAvroSerde<>();
	sensorSerde.configure(serdeConfig, false);

	return input -> input
			.map((k, value) -> {
				String newKey = "v1";
				if (value.getId().toString().endsWith("v2")) {
					newKey = "v2";
				}
				return new KeyValue<>(newKey, value);
			})
			.groupByKey()
			.count(Materialized.<String, Long, KeyValueStore<Bytes, byte[]>>as(STORE_NAME)
					.withKeySerde(Serdes.String())
					.withValueSerde(Serdes.Long()))
			.toStream();

}
 
Example #24
Source File: ConfluentSchemaRegistryDeserializerProvider.java    From beam with Apache License 2.0 5 votes vote down vote up
@Override
public Deserializer<T> getDeserializer(Map<String, ?> configs, boolean isKey) {
  ImmutableMap<String, Object> csrConfig =
      ImmutableMap.<String, Object>builder()
          .putAll(configs)
          .put(AbstractKafkaAvroSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG, schemaRegistryUrl)
          .build();
  Deserializer<T> deserializer =
      (Deserializer<T>) new KafkaAvroDeserializer(getSchemaRegistryClient());
  deserializer.configure(csrConfig, isKey);
  return deserializer;
}
 
Example #25
Source File: AvroMessageDeserializer.java    From Kafdrop with Apache License 2.0 5 votes vote down vote up
private KafkaAvroDeserializer getDeserializer() {
   Map<String, Object> config = new HashMap<>();
   config.put(AbstractKafkaAvroSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG, schemaRegistryUrl);
   KafkaAvroDeserializer kafkaAvroDeserializer = new KafkaAvroDeserializer();
   kafkaAvroDeserializer.configure(config, false);
   return kafkaAvroDeserializer;
}
 
Example #26
Source File: AggregatingMinMax.java    From kafka-tutorials with Apache License 2.0 5 votes vote down vote up
public static SpecificAvroSerde<YearlyMovieFigures> movieFiguresSerde(final Properties envProps) {
  final SpecificAvroSerde<YearlyMovieFigures> serde = new SpecificAvroSerde<>();
  serde.configure(Collections.singletonMap(
    AbstractKafkaAvroSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG, envProps.getProperty("schema.registry.url")
  ), false);
  return serde;
}
 
Example #27
Source File: TestTopology.java    From fluent-kafka-streams-tests with MIT License 5 votes vote down vote up
public void start() {
    this.schemaRegistry.start();
    this.properties
            .setProperty(AbstractKafkaAvroSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG, this.getSchemaRegistryUrl());
    try {
        this.stateDirectory = Files.createTempDirectory("fluent-kafka-streams");
    } catch (final IOException e) {
        throw new UncheckedIOException("Cannot create temporary state directory", e);
    }
    this.properties.setProperty(StreamsConfig.STATE_DIR_CONFIG, this.stateDirectory.toAbsolutePath().toString());
    final Topology topology = this.topologyFactory.apply(this.properties);
    this.testDriver = new TopologyTestDriver(topology, this.properties);

    this.inputTopics.clear();
    this.outputTopics.clear();

    for (final TopologyDescription.Subtopology subtopology : topology.describe().subtopologies()) {
        for (final TopologyDescription.Node node : subtopology.nodes()) {
            if (node instanceof TopologyDescription.Source) {
                for (final String topic : ((Source) node).topicSet()) {
                    addExternalTopics(this.inputTopics, topic);
                }
            } else if (node instanceof TopologyDescription.Sink) {
                addExternalTopics(this.outputTopics, ((TopologyDescription.Sink) node).topic());
            }
        }
    }

    for (final GlobalStore store : topology.describe().globalStores()) {
        store.source().topicSet().forEach(name -> addExternalTopics(this.inputTopics, name));
    }
}
 
Example #28
Source File: CountInhabitantsWithAvro.java    From fluent-kafka-streams-tests with MIT License 5 votes vote down vote up
public Properties getKafkaProperties() {
    final String brokers = "localhost:9092";
    final Properties kafkaConfig = new Properties();
    kafkaConfig.put(StreamsConfig.APPLICATION_ID_CONFIG, "inhabitants-per-city");
    kafkaConfig.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, brokers);
    kafkaConfig.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass());
    kafkaConfig.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, SpecificAvroSerde.class);
    kafkaConfig.put(AbstractKafkaAvroSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG, this.schemaRegistryUrl);
    return kafkaConfig;
}
 
Example #29
Source File: SplitStream.java    From kafka-tutorials with Apache License 2.0 5 votes vote down vote up
public Properties buildStreamsProperties(Properties envProps) {
    Properties props = new Properties();

    props.put(StreamsConfig.APPLICATION_ID_CONFIG, envProps.getProperty("application.id"));
    props.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, envProps.getProperty("bootstrap.servers"));
    props.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.String().getClass());
    props.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, SpecificAvroSerde.class);
    props.put(AbstractKafkaAvroSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG, envProps.getProperty("schema.registry.url"));

    return props;
}
 
Example #30
Source File: FindDistinctEvents.java    From kafka-tutorials with Apache License 2.0 5 votes vote down vote up
private SpecificAvroSerde<Click> buildClicksSerde(final Properties envProps) {
    final SpecificAvroSerde<Click> serde = new SpecificAvroSerde<>();
    Map<String, String> config = new HashMap<>();
    config.put(AbstractKafkaAvroSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG, envProps.getProperty("schema.registry.url"));
    serde.configure(config, false);
    return serde;
}