I know the post is old but I had to do this recently with Confluent Platform, see code below:
String todaysDatePostFix = getDate(); //gets date as yyyy-MM-dd-HH-mm-ss
Properties properties = new Properties();
properties.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
String replicaPlacementJson = readResourceFile("/stretched.json"); //reads from JSON file in resources
String prefix = "stretched-";
HashMap<String,String> topicProperties = new HashMap<String,String>();
topicProperties.put("min.insync.replicas", "3");
topicProperties.put("confluent.placement.constraints", replicaPlacementJson);
String topicName = "replica-placement-" + prefix + todaysDatePostFix;
// Create the AdminClient
try (AdminClient adminClient = AdminClient.create(properties)) {
NewTopic newTopic = new NewTopic(topicName,3,(short) -1); //we are using replica placement so as per docs set replicatonFactor to -1
newTopic.configs(topicProperties);
CreateTopicsResult createTopicsResult = adminClient.createTopics(Collections.singletonList(newTopic));
createTopicsResult.all().get();
System.out.println("Topic created successfully with custom replica placement. Topic " + topicName);
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
The property is “confluent.placement.constraints” also ensure replication factor is set to -1