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 consumerDetails <- fromJSON(content(response, "text"))
27 # Subscribe it to topic
28 response <- POST(url=paste(consumerDetails$base_uri,
29 "subscription", sep="/"),
30 content_type("application/vnd.kafka.v2+json"),
31 body = paste0('{"topics":["',
37 # Obtain all (or latest) messages on the topic
38 messagesJSON <- GET(url = paste(consumerDetails$base_uri,"records", sep = "/"),
39 accept("application/vnd.kafka.json.v2+json"),
42 messages <- fromJSON(content(messagesJSON,"text"))
44 createPlot(messages$value)
48 # Obtain latest messages
49 messagesJSON <- GET(url = paste(consumerDetails$base_uri,"records", sep = "/"),
50 accept("application/vnd.kafka.json.v2+json"),
53 messages2 <- fromJSON(content(messagesJSON,"text"))
55 apachelog <- rbind(messages$value,messages2$value)
60 DELETE(url = consumerDetails$base_uri,
61 content_type("application/vnd.kafka.v2+json"))