Kafka (Message queue service)
Back to home
On this page
Apache Kafka is an open-source stream-processing software platform.
It is a framework for storing, reading and analyzing streaming data. See the Kafka documentation for more information.
Supported versions
You can select the major and minor version.
Patch versions are applied periodically for bug fixes and the like. When you deploy your app, you always get the latest available patches.
- 3.7
Deprecated versions
The following versions are deprecated. They’re available, but they don’t receive security updates from upstream and aren’t guaranteed to work. They’ll be removed in the future – consider migrating to a supported version.
- 3.6
- 3.4
- 3.2
- 2.7
- 2.6
- 2.5
- 2.4
- 2.3
- 2.2
- 2.1
Relationship reference
For each service defined via a relationship to your application,
Upsun automatically generates corresponding environment variables within your application container,
in the $<RELATIONSHIP-NAME>_<SERVICE-PROPERTY> format.
Here is example information available through the service environment variables themselves,
or through the PLATFORM_RELATIONSHIPS environment variable.
You can obtain the complete list of available service environment variables in your app container by running upsun ssh env.
Note that the information about the relationship can change when an app is redeployed or restarted or the relationship is changed. So your apps should only rely on the service environment variables directly rather than hard coding any values.
KAFKA_SERVICE=kafka
KAFKA_IP=123.456.78.90
KAFKA_HOSTNAME=azertyuiopqsdfghjklm.kafka.service._.eu-1.platformsh.site
KAFKA_CLUSTER=azertyuiop-main-7rqtwti
KAFKA_HOST=kafka.internal
KAFKA_REL=kafka
KAFKA_SCHEME=kafka
KAFKA_TYPE=kafka:3.7
KAFKA_PORT=9092For some advanced use cases, you can use the PLATFORM_RELATIONSHIPS environment variable.
The structure of the PLATFORM_RELATIONSHIPS environment variable can be obtained by running upsun relationships in your terminal:
{
"service": "kafka",
"ip": "123.456.78.90",
"hostname": "azertyuiopqsdfghjklm.kafka.service._.eu-1.platformsh.site",
"cluster": "azertyuiop-main-7rqtwti",
"host": "kafka.internal",
"rel": "kafka",
"scheme": "kafka",
"type": "kafka:3.7",
"port": 9092
}Here is an example of how to gather PLATFORM_RELATIONSHIPS environment variable information in a .environment file:
# Decode the built-in credentials object variable.
export RELATIONSHIPS_JSON="$(echo "$PLATFORM_RELATIONSHIPS" | base64 --decode)"
# Set environment variables for individual credentials.
export APP_KAFKA_HOST="$(echo "$RELATIONSHIPS_JSON" | jq -r '.kafka[0].host')" Usage example
1. Configure the service
To define the service, use the kafka type:
services:
# The name of the service container. Must be unique within a project.
<SERVICE_NAME>:
type: kafka:<VERSION>Note that changing the name of the service replaces it with a brand new service and all existing data is lost. Back up your data before changing the service.
2. Define the relationship
To define the relationship, use the following configuration:
applications:
# The name of the app container. Must be unique within a project.
<APP_NAME>:
# Relationships enable access from this app to a given service.
# The example below shows simplified configuration leveraging a default service
# (identified from the relationship name) and a default endpoint.
# See the Application reference for all options for defining relationships and endpoints.
relationships:
<SERVICE_NAME>:You can define <SERVICE_NAME> as you like, so long as it’s unique between all defined services
and matches in both the application and services configuration.
The example above leverages default endpoint configuration for relationships. That is, it uses default endpoints behind the scenes, providing a relationship (the network address a service is accessible from) that is identical to the name of that service.
Depending on your needs, instead of default endpoint configuration, you can use explicit endpoint configuration.
With the above definition, the application container (<APP_NAME>) now has access to the service via the relationship <SERVICE_NAME> and its corresponding service environment variables
applications:
# The name of the app container. Must be unique within a project.
<APP_NAME>:
# Relationships enable access from this app to a given service.
# The example below shows configuration with an explicitly set service name and endpoint.
# See the Application reference for all options for defining relationships and endpoints.
relationships:
<RELATIONSHIP_NAME>:
service: <SERVICE_NAME>
endpoint: kafkaYou can define <SERVICE_NAME> and <RELATIONSHIP_NAME> as you like, so long as it’s unique between all defined services and relationships
and matches in both the application and services configuration.
The example above leverages explicit endpoint configuration for relationships.
Depending on your needs, instead of explicit endpoint configuration, you can use default endpoint configuration.
With the above definition, the application container now has access to the service via the relationship <RELATIONSHIP_NAME> and its corresponding service environment variables.
Example configuration
applications:
# The name of the app container. Must be unique within a project.
myapp:
# Relationships enable access from this app to a given service.
# The example below shows simplified configuration leveraging a default service
# (identified from the relationship name) and a default endpoint.
# See the Application reference for all options for defining relationships and endpoints.
relationships:
kafka:
services:
# The name of the service container. Must be unique within a project.
kafka:
type: kafka:3.7applications:
# The name of the app container. Must be unique within a project.
myapp:
# Relationships enable access from this app to a given service.
# The example below shows configuration with an explicitly set service name and endpoint.
# See the Application reference for all options for defining relationships and endpoints.
relationships:
kafka:
service: kafka
endpoint: kafka
services:
# The name of the service container. Must be unique within a project.
kafka:
type: kafka:3.7 Use in app
To use the configured service in your app, add a configuration file similar to the following to your project.
package sh.platform.languages.sample;
import org.apache.kafka.clients.consumer.Consumer;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.RecordMetadata;
import sh.platform.config.Config;
import sh.platform.config.Kafka;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;
public class KafkaSample implements Supplier<String> {
@Override
public String get() {
StringBuilder logger = new StringBuilder();
// Create a new config object to ease reading the Platform.sh environment variables.
// You can alternatively use getenv() yourself.
Config config = new Config();
try {
// Get the credentials to connect to the Kafka service.
final Kafka kafka = config.getCredential("kafka", Kafka::new);
Map<String, Object> configProducer = new HashMap<>();
configProducer.putIfAbsent(ProducerConfig.CLIENT_ID_CONFIG, "animals");
final Producer<Long, String> producer = kafka.getProducer(configProducer);
logger.append("<ul>");
// Sending data into the stream.
RecordMetadata metadata = producer.send(new ProducerRecord<>("animals", "lion")).get();
logger.append("<li>Record sent with to partition <code>").append(metadata.partition())
.append("</code> with offset <code>").append(metadata.offset()).append("</code></li>");
metadata = producer.send(new ProducerRecord<>("animals", "dog")).get();
logger.append("<li>Record sent with to partition <code>").append(metadata.partition())
.append("</code> with offset <code>").append(metadata.offset()).append("</code></li>");
metadata = producer.send(new ProducerRecord<>("animals", "cat")).get();
logger.append("<li>Record sent with to partition <code>").append(metadata.partition())
.append("</code> with offset <code>").append(metadata.offset()).append("</code></li>");
logger.append("</ul>");
// Consumer, read data from the stream.
final HashMap<String, Object> configConsumer = new HashMap<>();
configConsumer.put(ConsumerConfig.GROUP_ID_CONFIG, "consumerGroup1");
configConsumer.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
Consumer<Long, String> consumer = kafka.getConsumer(configConsumer, "animals");
ConsumerRecords<Long, String> consumerRecords = consumer.poll(Duration.ofSeconds(3));
logger.append("<ul>");
// Print each record.
consumerRecords.forEach(record -> {
logger.append("<li>Record: Key <code>" + record.key());
logger.append("</code> value <code>" + record.value());
logger.append("</code> partition <code>" + record.partition());
logger.append("</code> offset <code>" + record.offset()).append("</code></li>");
});
logger.append("</ul>");
// Commits the offset of record to broker.
consumer.commitSync();
return logger.toString();
} catch (Exception exp) {
throw new RuntimeException("An error when execute Kafka", exp);
}
}
}<?php
/*
with rdkafka PHP extension installed, you can use the RdKafka\Producer class to produce messages to a Kafka topic:
applications:
app:
type: php:8.5
runtime:
extensions:
- rdkafka
*/
declare(strict_types=1);
try {
$host = getenv('KAFKA_HOST') ?: getenv('KAFKA_IP');
$port = getenv('KAFKA_PORT');
if (!$host || !$port) {
throw new RuntimeException('Kafka environment variables not available.');
}
$broker = sprintf('%s:%s', $host, $port);
$conf = new RdKafka\Conf();
$conf->set('bootstrap.servers', $broker); // <-- important
$producer = new RdKafka\Producer($conf);
$topic = $producer->newTopic('test');
$topic->produce(
RD_KAFKA_PARTITION_UA, // let Kafka choose the partition
0,
'Hello, World!'
);
// Serve delivery reports / internal queue
$producer->poll(0);
// Wait for delivery
$timeoutMs = 10000;
$start = microtime(true);
while ($producer->getOutQLen() > 0) {
$producer->poll(100);
if ((microtime(true) - $start) * 1000 > $timeoutMs) {
break;
}
}
$result = $producer->flush($timeoutMs);
if ($result !== RD_KAFKA_RESP_ERR_NO_ERROR) {
throw new RuntimeException('Unable to flush messages (not delivered within timeout).');
}
echo "Message delivered.\n";
} catch (\Throwable $e) {
print $e->getMessage();
}
from json import dumps
from json import loads
from kafka import KafkaConsumer, KafkaProducer
from platformshconfig import Config
def usage_example():
# Create a new Config object to ease reading the Platform.sh environment variables.
# You can alternatively use os.environ yourself.
config = Config()
# Get the credentials to connect to the Kafka service.
credentials = config.credentials('kafka')
try:
kafka_server = '{}:{}'.format(credentials['host'], credentials['port'])
# Producer
producer = KafkaProducer(
bootstrap_servers=[kafka_server],
value_serializer=lambda x: dumps(x).encode('utf-8')
)
for e in range(10):
data = {'number' : e}
producer.send('numtest', value=data)
# Consumer
consumer = KafkaConsumer(
bootstrap_servers=[kafka_server],
auto_offset_reset='earliest'
)
consumer.subscribe(['numtest'])
output = ''
# For demonstration purposes so it doesn't block.
for e in range(10):
message = next(consumer)
output += str(loads(message.value.decode('UTF-8'))["number"]) + ', '
# What a real implementation would do instead.
# for message in consumer:
# output += loads(message.value.decode('UTF-8'))["number"]
return output
except Exception as e:
return e
## With the ruby-kafka gem
# Producer
require "kafka"
kafka = Kafka.new(["kafka.internal:9092"], client_id: "my-application")
kafka.deliver_message("Hello, World!", topic: "greetings")
# Consumer
kafka.each_message(topic: "greetings") do |message|
puts message.offset, message.key, message.value
end