Complete Guide: Installing Java, Setting up Environment Variables, and Redpanda Protobuf Consumer Program
1. Installing Java
To install the latest version of Java (Java 17 or later), follow these steps:
1. **Download the Latest JDK**: Visit the AdoptOpenJDK or OpenJDK website and download the latest version (Java 17 or later).
2. **Install Java**: Follow installation steps based on your OS (Windows, Linux, etc.).
3. **Verify Installation**: Run `java -version` in the terminal or Command Prompt to check the Java version.
2. Setting Up Environment Variables
After installing Java, you need to set up the environment variables for easy access to Java commands.
### For Windows
1. Right-click "This PC" > "Properties" > "Advanced system settings" > "Environment Variables".
2. Under "System Variables", click "New" to create a new variable:
- **Variable Name**: `JAVA_HOME`
- **Variable Value**: `C:\Program Files\AdoptOpenJDK\jdk-17.0.1` (or your installation path)
3. Find "Path" variable, click "Edit" and add the following path:
- `C:\Program Files\AdoptOpenJDK\jdk-17.0.1\bin` (or your installation path)
4. Click "OK" to save the changes.
### For Linux
1. Open `.bashrc` or `.bash_profile` in a text editor:
```bash
nano ~/.bashrc
```
2. Add the following lines to the file:
```bash
export JAVA_HOME=/usr/lib/jvm/java-17-openjdk
export PATH=$JAVA_HOME/bin:$PATH
```
3. Save the file and run `source ~/.bashrc` to reload the file.
4. Verify by running `echo $JAVA_HOME` and `java -version`.
3. Gradle Setup and Dependencies
Now let's set up your project using Gradle and add the necessary dependencies.
1. **Create Project Structure**:
Your project structure should look like this:
```
<project-root>
├── build.gradle
└── src
└── main
└── java
└── com
└── nnnn
└── pp
└── RedpandaProtobufConsumer.java
```
2. **Add Dependencies in build.gradle**:
Below is the Gradle configuration file to include Kafka and Protobuf dependencies.
```gradle
plugins {
id 'java'
}
group = 'com.nnnn'
version = '1.0-SNAPSHOT'
sourceCompatibility = '17'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.apache.kafka:kafka-clients:3.0.0'
implementation 'com.google.protobuf:protobuf-java:3.19.1'
implementation 'org.apache.kafka:kafka-protobuf-serializer:3.0.0'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
}
test {
useJUnitPlatform()
}
```
3. **Install Gradle**:
Follow the [Gradle Installation Guide](https://gradle.org/install/) to set up Gradle on your system.
4. **Build the Project**: After adding dependencies, run the following command to build your project:
```bash
gradle build
```
4. Protobuf Model Setup
Create a `.proto` file for your Protobuf model, such as `Ticket.proto`:
```protobuf
syntax = "proto3";
package com.nnnn.pp;
message Ticket {
string ticketId = 1;
string ticketType = 2;
int32 amount = 3;
}
```
After generating the Java classes or writing them manually, the `Ticket` class might look like this:
```java
package com.nnnn.pp;
public class Ticket {
private String ticketId;
private String ticketType;
private int amount;
// Getters and setters
}
```
5. Redpanda Protobuf Consumer Program
Here is the implementation of the consumer program that connects to Redpanda and fetches Protobuf messages:
```java
package com.nnnn.pp;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.serialization.ByteArrayDeserializer;
import com.google.protobuf.InvalidProtocolBufferException;
import org.apache.kafka.common.serialization.Deserializer;
public class RedpandaProtobufConsumer {
public static void main(String[] args) {
String bootstrapServers = "localhost:9092";
String groupId = "example-group";
String topic = "your-topic";
Properties properties = new Properties();
properties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
properties.put(ConsumerConfig.GROUP_ID_CONFIG, groupId);
properties.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, ByteArrayDeserializer.class);
properties.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, ProtobufDeserializer.class);
KafkaConsumer<byte[], Ticket> consumer = new KafkaConsumer<>(properties);
consumer.subscribe(Collections.singletonList(topic));
while (true) {
var records = consumer.poll(1000);
records.forEach(record -> {
try {
Ticket ticket = Ticket.parseFrom(record.value());
System.out.println("Ticket ID: " + ticket.getTicketId());
System.out.println("Ticket Type: " + ticket.getTicketType());
System.out.println("Ticket Amount: " + ticket.getAmount());
} catch (InvalidProtocolBufferException e) {
e.printStackTrace();
}
});
}
}
static class ProtobufDeserializer implements Deserializer<byte[]> {
@Override
public byte[] deserialize(String topic, byte[] data) {
return data;
}
}
}
```
6. Running the Program
1. **Build the Project**: Run `gradle build` to compile the project.
2. **Run the Program**: Run `gradle run` to start consuming messages.
No comments:
Post a Comment