Documentation Index
Fetch the complete documentation index at: https://docs.conduktor.io/llms.txt
Use this file to discover all available pages before exploring further.
Set up a Maven project with Kafka dependencies in 10 minutes
Before writing Kafka producers and consumers, you need a Java project with the Kafka client library and logging dependencies configured.
What you’ll learn:
- How to create a new Maven project in IntelliJ
- How to add Kafka client dependencies to pom.xml
- How to configure logging with SLF4J
- How to verify your setup works
Create a Maven project with pom.xml and set up dependencies
In IntelliJ IDEA, create a new Java maven project (File > New > Project)
Then add your Maven project attributes
The build tool Maven contains a **pom.xml** file. The pom.xml is a default XML file that carries all the information regarding the GroupID, ArtifactID, as well as the Version values. The user needs to define all the necessary project dependencies in the pom.xml file. Go to the pom.xml file.
Define the Kafka Dependencies. Create a **<dependencies>...</dependencies>** block within which we will define the required dependencies.
Add a dependency for Kafka client as shown below
<project>
...
<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.kafka/kafka-clients -->
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>2.8.1</version>
</dependency>
</dependencies>
</project>
If the version number appears red in color, it means the user missed to enable the ‘Auto-Import’ option. If so, go to View > Tool Windows > Maven. A Maven Projects Window will appear on the right side of the screen. Click on the ‘Refresh’ button appearing right there. This will enable the missed Auto-Import Maven Projects. If the color changes to black, it means the missed dependency is downloaded.
Add another dependency for logging. This will enable us to print diagnostic logs while our application runs.
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.32</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.32</version>
</dependency>
Now, we have set all the required dependencies. Let’s try the Simple Hello World example.
Create your first class
Create a java package say, io.conduktor.demos.kafka.HelloWorld
While creating the java package, follow the package naming conventions. Finally, create the sample application program as shown below.
package io.conduktor.demos.kafka;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class HelloWorld {
private static final Logger log = LoggerFactory.getLogger(HelloWorld.class);
public static void main(String[] args) {
log.info("Hello World");
}
}
Run the application (the play green button on line 9 in the screenshot below) and verify that it runs and prints the message, and exits with code 0. This means that your Java application has run successfully.
Expand the ‘External Libraries’ on the Project panel and verify that it displays the dependencies that we added for the project in pom.xml.
All good!
We have created a sample Java project that includes all the needed dependencies. This will form the basis for creating Java producers and consumers next.
See it in practice with ConduktorOnce your project is set up, use Conduktor Console to create topics, inspect messages, and test your Kafka applications during development.
Next steps