Update a Kafka topic

Update a Kafka topic

Explore further

For detailed documentation that includes this code sample, see the following:

Code sample

Go

Before trying this sample, follow the Go setup instructions in the Managed Service for Apache Kafka quickstart using client libraries . For more information, see the Managed Service for Apache Kafka Go API reference documentation .

To authenticate to Managed Service for Apache Kafka, set up Application Default Credentials. For more information, see Set up authentication for a local development environment .

  import 
  
 ( 
  
 "context" 
  
 "fmt" 
  
 "io" 
  
 "cloud.google.com/go/managedkafka/apiv1/managedkafkapb" 
  
 "google.golang.org/api/option" 
  
 "google.golang.org/protobuf/types/known/fieldmaskpb" 
  
 managedkafka 
  
 "cloud.google.com/go/managedkafka/apiv1" 
 ) 
 func 
  
 updateTopic 
 ( 
 w 
  
 io 
 . 
 Writer 
 , 
  
 projectID 
 , 
  
 region 
 , 
  
 clusterID 
 , 
  
 topicID 
  
 string 
 , 
  
 partitionCount 
  
 int32 
 , 
  
 configs 
  
 map 
 [ 
 string 
 ] 
 string 
 , 
  
 opts 
  
 ... 
 option 
 . 
 ClientOption 
 ) 
  
 error 
  
 { 
  
 // projectID := "my-project-id" 
  
 // region := "us-central1" 
  
 // clusterID := "my-cluster" 
  
 // topicID := "my-topic" 
  
 // partitionCount := 20 
  
 // configs := map[string]string{"min.insync.replicas":"1"} 
  
 ctx 
  
 := 
  
 context 
 . 
 Background 
 () 
  
 client 
 , 
  
 err 
  
 := 
  
 managedkafka 
 . 
  NewClient 
 
 ( 
 ctx 
 , 
  
 opts 
 ... 
 ) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "managedkafka.NewClient got err: %w" 
 , 
  
 err 
 ) 
  
 } 
  
 defer 
  
 client 
 . 
 Close 
 () 
  
 clusterPath 
  
 := 
  
 fmt 
 . 
 Sprintf 
 ( 
 "projects/%s/locations/%s/clusters/%s" 
 , 
  
 projectID 
 , 
  
 region 
 , 
  
 clusterID 
 ) 
  
 topicPath 
  
 := 
  
 fmt 
 . 
 Sprintf 
 ( 
 "%s/topics/%s" 
 , 
  
 clusterPath 
 , 
  
 topicID 
 ) 
  
 TopicConfig 
  
 := 
  
 managedkafkapb 
 . 
 Topic 
 { 
  
 Name 
 : 
  
 topicPath 
 , 
  
 PartitionCount 
 : 
  
 partitionCount 
 , 
  
 Configs 
 : 
  
 configs 
 , 
  
 } 
  
 paths 
  
 := 
  
 [] 
 string 
 { 
 "partition_count" 
 , 
  
 "configs" 
 } 
  
 updateMask 
  
 := 
  
& fieldmaskpb 
 . 
 FieldMask 
 { 
  
 Paths 
 : 
  
 paths 
 , 
  
 } 
  
 req 
  
 := 
  
& managedkafkapb 
 . 
 UpdateTopicRequest 
 { 
  
 UpdateMask 
 : 
  
 updateMask 
 , 
  
 Topic 
 : 
  
& TopicConfig 
 , 
  
 } 
  
 topic 
 , 
  
 err 
  
 := 
  
 client 
 . 
 UpdateTopic 
 ( 
 ctx 
 , 
  
 req 
 ) 
  
 if 
  
 err 
  
 != 
  
 nil 
  
 { 
  
 return 
  
 fmt 
 . 
 Errorf 
 ( 
 "client.UpdateTopic got err: %w" 
 , 
  
 err 
 ) 
  
 } 
  
 fmt 
 . 
 Fprintf 
 ( 
 w 
 , 
  
 "Updated topic: %#v\n" 
 , 
  
 topic 
 ) 
  
 return 
  
 nil 
 } 
 

Java

Before trying this sample, follow the Java setup instructions in the Managed Service for Apache Kafka quickstart using client libraries . For more information, see the Managed Service for Apache Kafka Java API reference documentation .

To authenticate to Managed Service for Apache Kafka, set up Application Default Credentials. For more information, see Set up authentication for a local development environment .

  import 
  
 com.google.api.gax.rpc. ApiException 
 
 ; 
 import 
  
 com.google.cloud.managedkafka.v1. ManagedKafkaClient 
 
 ; 
 import 
  
 com.google.cloud.managedkafka.v1. Topic 
 
 ; 
 import 
  
 com.google.cloud.managedkafka.v1. TopicName 
 
 ; 
 import 
  
 com.google.cloud.managedkafka.v1. UpdateTopicRequest 
 
 ; 
 import 
  
 com.google.protobuf. FieldMask 
 
 ; 
 import 
  
 java.io.IOException 
 ; 
 import 
  
 java.util.Arrays 
 ; 
 import 
  
 java.util.HashMap 
 ; 
 import 
  
 java.util.Map 
 ; 
 public 
  
 class 
 UpdateTopic 
  
 { 
  
 public 
  
 static 
  
 void 
  
 main 
 ( 
 String 
 [] 
  
 args 
 ) 
  
 throws 
  
 Exception 
  
 { 
  
 // TODO(developer): Replace these variables before running the example. 
  
 String 
  
 projectId 
  
 = 
  
 "my-project-id" 
 ; 
  
 String 
  
 region 
  
 = 
  
 "my-region" 
 ; 
  
 // e.g. us-east1 
  
 String 
  
 clusterId 
  
 = 
  
 "my-cluster" 
 ; 
  
 String 
  
 topicId 
  
 = 
  
 "my-topic" 
 ; 
  
 int 
  
 partitionCount 
  
 = 
  
 200 
 ; 
  
 Map<String 
 , 
  
 String 
>  
 configs 
  
 = 
  
 new 
  
 HashMap<String 
 , 
  
 String 
> () 
  
 { 
  
 { 
  
 put 
 ( 
 "min.insync.replicas" 
 , 
  
 "1" 
 ); 
  
 } 
  
 }; 
  
 updateTopic 
 ( 
 projectId 
 , 
  
 region 
 , 
  
 clusterId 
 , 
  
 topicId 
 , 
  
 partitionCount 
 , 
  
 configs 
 ); 
  
 } 
  
 public 
  
 static 
  
 void 
  
 updateTopic 
 ( 
  
 String 
  
 projectId 
 , 
  
 String 
  
 region 
 , 
  
 String 
  
 clusterId 
 , 
  
 String 
  
 topicId 
 , 
  
 int 
  
 partitionCount 
 , 
  
 Map<String 
 , 
  
 String 
>  
 configs 
 ) 
  
 throws 
  
 Exception 
  
 { 
  
  Topic 
 
  
 topic 
  
 = 
  
  Topic 
 
 . 
 newBuilder 
 () 
  
 . 
 setName 
 ( 
  TopicName 
 
 . 
 of 
 ( 
 projectId 
 , 
  
 region 
 , 
  
 clusterId 
 , 
  
 topicId 
 ). 
 toString 
 ()) 
  
 . 
  setPartitionCount 
 
 ( 
 partitionCount 
 ) 
  
 . 
 putAllConfigs 
 ( 
 configs 
 ) 
  
 . 
 build 
 (); 
  
 String 
 [] 
  
 paths 
  
 = 
  
 { 
 "partition_count" 
 , 
  
 "configs" 
 }; 
  
  FieldMask 
 
  
 updateMask 
  
 = 
  
  FieldMask 
 
 . 
 newBuilder 
 (). 
  addAllPaths 
 
 ( 
 Arrays 
 . 
 asList 
 ( 
 paths 
 )). 
 build 
 (); 
  
 try 
  
 ( 
  ManagedKafkaClient 
 
  
 managedKafkaClient 
  
 = 
  
  ManagedKafkaClient 
 
 . 
 create 
 ()) 
  
 { 
  
  UpdateTopicRequest 
 
  
 request 
  
 = 
  
  UpdateTopicRequest 
 
 . 
 newBuilder 
 (). 
 setUpdateMask 
 ( 
 updateMask 
 ). 
 setTopic 
 ( 
 topic 
 ). 
 build 
 (); 
  
 // This operation is being handled synchronously. 
  
  Topic 
 
  
 response 
  
 = 
  
 managedKafkaClient 
 . 
 updateTopic 
 ( 
 request 
 ); 
  
 System 
 . 
 out 
 . 
 printf 
 ( 
 "Updated topic: %s\n" 
 , 
  
 response 
 . 
  getName 
 
 ()); 
  
 } 
  
 catch 
  
 ( 
 IOException 
  
 | 
  
  ApiException 
 
  
 e 
 ) 
  
 { 
  
 System 
 . 
 err 
 . 
 printf 
 ( 
 "managedKafkaClient.updateCluster got err: %s" 
 , 
  
 e 
 . 
 getMessage 
 ()); 
  
 } 
  
 } 
 } 
 

Python

Before trying this sample, follow the Python setup instructions in the Managed Service for Apache Kafka quickstart using client libraries . For more information, see the Managed Service for Apache Kafka Python API reference documentation .

To authenticate to Managed Service for Apache Kafka, set up Application Default Credentials. For more information, see Set up authentication for a local development environment .

  from 
  
 google.api_core.exceptions 
  
 import 
 NotFound 
 from 
  
 google.cloud 
  
 import 
  managedkafka_v1 
 
 from 
  
 google.protobuf 
  
 import 
 field_mask_pb2 
 # TODO(developer) 
 # project_id = "my-project-id" 
 # region = "us-central1" 
 # cluster_id = "my-cluster" 
 # topic_id = "my-topic" 
 # partition_count = 20 
 # configs = {"min.insync.replicas": "1"} 
 client 
 = 
  managedkafka_v1 
 
 . 
  ManagedKafkaClient 
 
 () 
 topic 
 = 
  managedkafka_v1 
 
 . 
  Topic 
 
 () 
 topic 
 . 
 name 
 = 
 client 
 . 
  topic_path 
 
 ( 
 project_id 
 , 
 region 
 , 
 cluster_id 
 , 
 topic_id 
 ) 
 topic 
 . 
 partition_count 
 = 
 partition_count 
 topic 
 . 
 configs 
 = 
 configs 
 update_mask 
 = 
 field_mask_pb2 
 . 
 FieldMask 
 () 
 update_mask 
 . 
 paths 
 . 
 extend 
 ([ 
 "partition_count" 
 , 
 "configs" 
 ]) 
 # For a list of editable fields, one can check https://cloud.google.com/managed-kafka/docs/create-topic#properties. 
 request 
 = 
  managedkafka_v1 
 
 . 
  UpdateTopicRequest 
 
 ( 
 update_mask 
 = 
 update_mask 
 , 
 topic 
 = 
 topic 
 , 
 ) 
 try 
 : 
 response 
 = 
 client 
 . 
  update_topic 
 
 ( 
 request 
 = 
 request 
 ) 
 print 
 ( 
 "Updated topic:" 
 , 
 response 
 ) 
 except 
 NotFound 
 as 
 e 
 : 
 print 
 ( 
 f 
 "Failed to update topic 
 { 
 topic_id 
 } 
 with error: 
 { 
 e 
 . 
 message 
 } 
 " 
 ) 
 

What's next

To search and filter code samples for other Google Cloud products, see the Google Cloud sample browser .

Design a Mobile Site
View Site in Mobile | Classic
Share by: