library(dplyr)
kafka_rest_proxy <- "http://localhost:8082"
+consumer <- 'my_json_consumer'
+consumer_instance <- 'my_consumer_instance'
+topic <- 'accesslogapache'
# Create consumer
# See https://docs.confluent.io/current/kafka-rest/docs/intro.html#produce-and-consume-avro-messages
-response <- POST(url=paste(kafka_rest_proxy, "consumers", "my_json_consumer", sep="/"),
- content_type("application/vnd.kafka.v2+json"),
- accept("application/vnd.kafka.v2+json"),
- body='{"name": "my_consumer_instance", "format": "json", "auto.offset.reset": "earliest"}')
-fromJSON(content(response, "text"))
-
-response <- POST(url=paste(kafka_rest_proxy,
- "consumers",
- "my_json_consumer",
- "instances",
- "my_consumer_instance",
+# Create consumer
+source("R/CreateKafkaConsumer.R")
+consumerDetails <- CreateKafkaConsumer(kafka.rest.proxy = kafka_rest_proxy, consumer = consumer, consumer_instance = consumer_instance)
+
+# Subscribe it to topic
+response <- POST(url=paste(consumerDetails$base_uri,
"subscription", sep="/"),
content_type("application/vnd.kafka.v2+json"),
- body = '{"topics":["accesslogapache"]}')
+ body = paste0('{"topics":["',
+ topic,
+ '"]}')
+ )
response
-# Obtain all messages on the topic
-messagesJSON <- GET(url = paste(kafka_rest_proxy,"consumers","my_json_consumer","instances","my_consumer_instance","records", sep = "/"),
+# Obtain all (or latest) messages on the topic
+messagesJSON <- GET(url = paste(consumerDetails$base_uri,"records", sep = "/"),
accept("application/vnd.kafka.json.v2+json"),
encode="json")
Sys.sleep(120)
-messagesJSON <- GET(url = paste(kafka_rest_proxy,"consumers","my_json_consumer","instances","my_consumer_instance","records", sep = "/"),
+# Obtain latest messages
+messagesJSON <- GET(url = paste(consumerDetails$base_uri,"records", sep = "/"),
accept("application/vnd.kafka.json.v2+json"),
encode="json")
createPlot(apachelog)
# Remove the consumer
-DELETE(url = paste(kafka_rest_proxy,"consumers","my_json_consumer","instances","my_consumer_instance", sep = "/"),
- content_type("application/vnd.kafka.v2+json"))
+source("R/DestroyKafkaConsumer.R")
+response <- DestroyKafkaConsumer(consumer.base.uri = consumerDetails$base_uri)