1 # Read via REST API from Kafka topic
2 # Prerequisite: set up kafka with topic accesslogapache
9 kafka_rest_proxy <- "http://localhost:8082"
10 consumer <- 'my_json_consumer'
11 consumer_instance <- 'my_consumer_instance'
12 topic <- 'accesslogapache'
15 # See https://docs.confluent.io/current/kafka-rest/docs/intro.html#produce-and-consume-avro-messages
18 response <- POST(url=paste(kafka_rest_proxy, "consumers", consumer, sep="/"),
19 content_type("application/vnd.kafka.v2+json"),
20 accept("application/vnd.kafka.v2+json"),
21 body=paste0('{"name": "',
23 '", "format": "json", "auto.offset.reset": "earliest"}')
25 fromJSON(content(response, "text"))
27 # Subscribe it to topic
28 response <- POST(url=paste(kafka_rest_proxy,
33 "subscription", sep="/"),
34 content_type("application/vnd.kafka.v2+json"),
35 body = paste0('{"topics":["',
41 # Obtain all (or latest) messages on the topic
42 messagesJSON <- GET(url = paste(kafka_rest_proxy,"consumers",consumer,"instances",consumer_instance,"records", sep = "/"),
43 accept("application/vnd.kafka.json.v2+json"),
46 messages <- fromJSON(content(messagesJSON,"text"))
48 createPlot(messages$value)
52 # Obtain latest messages
53 messagesJSON <- GET(url = paste(kafka_rest_proxy,"consumers",consumer,"instances",consumer_instance,"records", sep = "/"),
54 accept("application/vnd.kafka.json.v2+json"),
57 messages2 <- fromJSON(content(messagesJSON,"text"))
59 apachelog <- rbind(messages$value,messages2$value)
64 DELETE(url = paste(kafka_rest_proxy,"consumers",consumer,"instances",consumer_instance, sep = "/"),
65 content_type("application/vnd.kafka.v2+json"))