private void CreatePartition() { if (PartitionOptions != null) { TopicPartitionCache = new ConcurrentDictionary <string, TopicPartition>(); if (PartitionOptions.Partition > 1) { using (var adminClient = new AdminClient(Producer.Handle)) { try { adminClient.CreateTopicsAsync(new TopicSpecification[] { new TopicSpecification { Name = TopicName, NumPartitions = 1, ReplicationFactor = 1 } }).Wait(); } catch (AggregateException ex) { //{Confluent.Kafka.Admin.CreateTopicsException: An error occurred creating topics: [jt809]: [Topic 'jt809' already exists.].} if (ex.InnerException is Confluent.Kafka.Admin.CreateTopicsException exception) { } else { //记录日志 //throw ex.InnerException; } } try { //topic IncreaseTo 只增不减 adminClient.CreatePartitionsAsync( new List <PartitionsSpecification> { new PartitionsSpecification { IncreaseTo = PartitionOptions.Partition, Topic = TopicName } } ).Wait(); } catch (AggregateException ex) { //记录日志 // throw ex.InnerException; } } } } }
public JT808_MsgId_Producer( IOptions <ProducerConfig> producerConfigAccessor) { producer = new Producer <string, byte[]>(producerConfigAccessor.Value); this.jT808ProducerPartitionFactory = new JT808MsgIdProducerPartitionFactoryImpl(); using (var adminClient = new AdminClient(producer.Handle)) { try { adminClient.CreateTopicsAsync(new TopicSpecification[] { new TopicSpecification { Name = TopicName, NumPartitions = 1, ReplicationFactor = 1 } }).Wait(); } catch (AggregateException ex) { //{Confluent.Kafka.Admin.CreateTopicsException: An error occurred creating topics: [jt808]: [Topic 'jt808' already exists.].} if (ex.InnerException is Confluent.Kafka.Admin.CreateTopicsException exception) { } else { //记录日志 //throw ex.InnerException; } } try { //topic IncreaseTo 只增不减 adminClient.CreatePartitionsAsync( new List <PartitionsSpecification> { new PartitionsSpecification { IncreaseTo = 8, Topic = TopicName } } ).Wait(); } catch (AggregateException ex) { //记录日志 // throw ex.InnerException; } } }
private static void CreateTopic() { using (var producer = new Producer <Null, string>(GetConfig())) { using (var adminClient = new AdminClient(producer.Handle)) { adminClient.CreateTopicsAsync(new TopicSpecification[] { new TopicSpecification { Name = TOPIC, NumPartitions = 5, ReplicationFactor = 1 } }).Wait(); adminClient.CreatePartitionsAsync(new List <PartitionsSpecification> { new PartitionsSpecification { Topic = TOPIC, IncreaseTo = 6 } }).Wait(); } } }
public static void AdminClient_CreatePartitions(string bootstrapServers, string singlePartitionTopic, string partitionedTopic) { LogToFile("start AdminClient_CreatePartitions"); var topicName1 = Guid.NewGuid().ToString(); var topicName2 = Guid.NewGuid().ToString(); var topicName3 = Guid.NewGuid().ToString(); var topicName4 = Guid.NewGuid().ToString(); var topicName5 = Guid.NewGuid().ToString(); var topicName6 = Guid.NewGuid().ToString(); // test creating a new partition works. using (var producer = new Producer <Null, Null>(new ProducerConfig { BootstrapServers = bootstrapServers })) using (var adminClient = new AdminClient(producer.Handle)) { adminClient.CreateTopicsAsync(new TopicSpecification[] { new TopicSpecification { Name = topicName1, NumPartitions = 1, ReplicationFactor = 1 } }).Wait(); adminClient.CreatePartitionsAsync(new List <PartitionsSpecification> { new PartitionsSpecification { Topic = topicName1, IncreaseTo = 2 } }).Wait(); var dr1 = producer.ProduceAsync(new TopicPartition(topicName1, 0), new Message <Null, Null> { }).Result; var dr2 = producer.ProduceAsync(new TopicPartition(topicName1, 1), new Message <Null, Null> { }).Result; try { producer.ProduceAsync(new TopicPartition(topicName1, 2), new Message <Null, Null> { }).Wait(); Assert.True(false, "expecting exception"); } catch (KafkaException ex) { Assert.True(ex.Error.IsError); } } // check validate only works. using (var producer = new Producer <Null, Null>(new ProducerConfig { BootstrapServers = bootstrapServers })) using (var adminClient = new AdminClient(producer.Handle)) { adminClient.CreateTopicsAsync(new TopicSpecification[] { new TopicSpecification { Name = topicName2, NumPartitions = 1, ReplicationFactor = 1 } }).Wait(); adminClient.CreatePartitionsAsync(new List <PartitionsSpecification> { new PartitionsSpecification { Topic = topicName2, IncreaseTo = 10 } }, new CreatePartitionsOptions { ValidateOnly = true }).Wait(); // forces a metadata request. var dr1 = producer.ProduceAsync(new TopicPartition(topicName2, 0), new Message <Null, Null> { }).Result; try { // since we have metadata, this throws immediately (i.e. not wrapped in AggregateException) var dr2 = producer.ProduceAsync(new TopicPartition(topicName2, 1), new Message <Null, Null> { }).Result; Assert.True(false, "expecting exception"); } catch (KafkaException ex) { Assert.True(ex.Error.IsError); } } // check valid Assignments property value works. using (var producer = new Producer <Null, Null>(new ProducerConfig { BootstrapServers = bootstrapServers })) using (var adminClient = new AdminClient(producer.Handle)) { adminClient.CreateTopicsAsync(new TopicSpecification[] { new TopicSpecification { Name = topicName3, NumPartitions = 1, ReplicationFactor = 1 } }).Wait(); adminClient.CreatePartitionsAsync( new List <PartitionsSpecification> { new PartitionsSpecification { Topic = topicName2, IncreaseTo = 2, ReplicaAssignments = new List <List <int> > { new List <int> { 0 } } } }, new CreatePartitionsOptions { ValidateOnly = true } ).Wait(); } // check invalid Assignments property value works. using (var producer = new Producer <Null, Null>(new ProducerConfig { BootstrapServers = bootstrapServers })) using (var adminClient = new AdminClient(producer.Handle)) { adminClient.CreateTopicsAsync(new TopicSpecification[] { new TopicSpecification { Name = topicName4, NumPartitions = 1, ReplicationFactor = 1 } }).Wait(); try { adminClient.CreatePartitionsAsync( new List <PartitionsSpecification> { new PartitionsSpecification { Topic = topicName2, IncreaseTo = 2, ReplicaAssignments = new List <List <int> > { new List <int> { 42 } } } }, new CreatePartitionsOptions { ValidateOnly = true } ).Wait(); Assert.True(false, "Expecting exception"); } catch (AggregateException ex) { Assert.True(ex.InnerException.GetType() == typeof(CreatePartitionsException)); var cpe = (CreatePartitionsException)ex.InnerException; Assert.Single(cpe.Results); Assert.True(cpe.Results.First().Error.IsError); } } // more than one. using (var adminClient = new AdminClient(new AdminClientConfig { BootstrapServers = bootstrapServers })) { adminClient.CreateTopicsAsync(new TopicSpecification[] { new TopicSpecification { Name = topicName5, NumPartitions = 1, ReplicationFactor = 1 }, new TopicSpecification { Name = topicName6, NumPartitions = 1, ReplicationFactor = 1 } } ).Wait(); Thread.Sleep(TimeSpan.FromSeconds(1)); // just a simple check there wasn't an exception. adminClient.CreatePartitionsAsync( new List <PartitionsSpecification> { new PartitionsSpecification { Topic = topicName5, IncreaseTo = 2 }, new PartitionsSpecification { Topic = topicName6, IncreaseTo = 3 } } ).Wait(); } Assert.Equal(0, Library.HandleCount); LogToFile("end AdminClient_CreatePartitions"); }