Break out CreateKafkaConsumer function.
authorFrederik Vanrenterghem <frederik@vanrenterghem.io>
Thu, 13 Sep 2018 13:13:42 +0000 (21:13 +0800)
committerFrederik Vanrenterghem <frederik@vanrenterghem.io>
Thu, 13 Sep 2018 13:13:42 +0000 (21:13 +0800)
R/CreateKafkaConsumer.R [new file with mode: 0644]
kafkaConsumer.R

diff --git a/R/CreateKafkaConsumer.R b/R/CreateKafkaConsumer.R
new file mode 100644 (file)
index 0000000..cf6edee
--- /dev/null
@@ -0,0 +1,11 @@
+CreateKafkaConsumer <- function(kafka.rest.proxy = "http://localhost:8082", consumer, consumer_instance) {
+  response <- POST(url=paste(kafka_rest_proxy, "consumers", consumer, sep="/"),
+                   content_type("application/vnd.kafka.v2+json"),
+                   accept("application/vnd.kafka.v2+json"),
+                   body=paste0('{"name": "',
+                               consumer_instance,
+                               '", "format": "json", "auto.offset.reset": "earliest"}')
+  )
+  consumerDetails <- fromJSON(content(response, "text"))
+  return(consumerDetails)
+}
\ No newline at end of file
index e7da6ed..6e8fe0b 100644 (file)
@@ -15,14 +15,8 @@ topic <- 'accesslogapache'
 # See https://docs.confluent.io/current/kafka-rest/docs/intro.html#produce-and-consume-avro-messages
 
 # Create consumer
-response <- POST(url=paste(kafka_rest_proxy, "consumers", consumer, sep="/"),
-     content_type("application/vnd.kafka.v2+json"),
-     accept("application/vnd.kafka.v2+json"),
-     body=paste0('{"name": "',
-                 consumer_instance,
-                 '", "format": "json", "auto.offset.reset": "earliest"}')
-     )
-consumerDetails <- fromJSON(content(response, "text"))
+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,