Kafka 0.8
Java Code
Topic Creation
Partitioning
Replication

Kafka 0.8, is it possible to create topic with partition and replication using java code?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Yes, in Kafka 0.8 you can create a topic programmatically from Java and specify both the partition count and replication factor. The important historical detail is that Kafka 0.8 administration was ZooKeeper-based, so the code path is different from newer Kafka versions that use AdminClient.

Kafka 0.8 Admin Operations Go Through ZooKeeper

In the 0.8 era, topic metadata was managed through ZooKeeper, and Java admin code typically used ZkClient together with Kafka admin utilities. That means older examples often look surprising if you are used to modern Kafka APIs.

A typical flow is:

  1. connect to ZooKeeper
  2. build any topic properties
  3. call the admin utility to create the topic

A representative Java example looks like this:

java
1import java.util.Properties;
2import kafka.admin.AdminUtils;
3import org.I0Itec.zkclient.ZkClient;
4import kafka.utils.ZKStringSerializer$;
5
6public class CreateTopicExample {
7    public static void main(String[] args) {
8        String zkConnect = "localhost:2181";
9        int sessionTimeoutMs = 10_000;
10        int connectionTimeoutMs = 10_000;
11
12        ZkClient zkClient = new ZkClient(
13            zkConnect,
14            sessionTimeoutMs,
15            connectionTimeoutMs,
16            ZKStringSerializer$.MODULE$
17        );
18
19        String topic = "orders";
20        int partitions = 3;
21        int replicationFactor = 2;
22        Properties config = new Properties();
23
24        AdminUtils.createTopic(
25            zkClient,
26            topic,
27            partitions,
28            replicationFactor,
29            config
30        );
31
32        System.out.println("Created topic: " + topic);
33        zkClient.close();
34    }
35}

The key point is that the partition count and replication factor are both passed directly into the topic-creation call.

What Those Numbers Mean

The partition count controls how many partitions the topic is split into. More partitions can increase parallelism, but they also affect consumer-group behavior, ordering guarantees, and operational overhead.

The replication factor controls how many brokers store copies of each partition. In Kafka 0.8, that means the factor must not exceed the number of eligible brokers in the cluster.

Example:

  • 'partitions = 3'
  • 'replicationFactor = 2'

This creates three partitions, each stored on two brokers.

Topic Configuration Properties

You can also pass topic-level configuration in the Properties object:

java
Properties config = new Properties();
config.put("retention.ms", "86400000");
config.put("segment.bytes", "1073741824");

Then use the same createTopic call. That is helpful when you want retention or segment settings to be part of creation instead of a later manual change.

Why Newer Examples Look Different

If you search today, many examples show AdminClient.createTopics(...). That is correct for modern Kafka, but it does not answer the Kafka 0.8 question because Kafka 0.8 predates that API style.

So the short answer is:

  • for Kafka 0.8, use the ZooKeeper-based admin utilities
  • for modern Kafka, use AdminClient

Mixing those generations of examples leads to dependency errors or code that simply will not compile against 0.8-era libraries.

Operational Constraints

Programmatic creation only works if the cluster can satisfy the request.

For example, this will fail operationally:

  • replication factor is larger than the number of available brokers
  • ZooKeeper is unreachable
  • the topic already exists

A simple defensive pattern is to check existence first if the admin utility version you use supports it, or to catch and report the duplicate-topic failure cleanly.

Common Pitfalls

The biggest mistake is copying a modern AdminClient example into a Kafka 0.8 project. The admin stack changed significantly after the ZooKeeper-centric versions.

Another issue is choosing a replication factor that the cluster cannot satisfy. If you only have one broker, asking for replication factor 2 cannot work.

Developers also sometimes rely on auto-topic creation and then wonder why the partition and replication settings are wrong. If you care about those values, create the topic explicitly.

Finally, remember that this is old infrastructure. Dependency versions, Scala binary versions, and ZooKeeper client classes must line up correctly or the example will fail before topic creation even starts.

Summary

  • Kafka 0.8 can create topics programmatically from Java.
  • In that version, topic creation is ZooKeeper-based rather than AdminClient-based.
  • You pass the partition count and replication factor directly to the creation call.
  • The replication factor must not exceed the available broker count.
  • Be careful not to mix modern Kafka admin examples with Kafka 0.8 dependencies.

Course illustration
Course illustration

All Rights Reserved.