]> git.vanrenterghem.biz Git - R/project-using-kafka-in-R.git/commitdiff
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 e7da6ed25a27319c4369494650da48d1b0dfcec4..6e8fe0bc80dfe76052a2ee87114f5ce1ca6281a5 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
 # 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,
 
 # Subscribe it to topic
 response <- POST(url=paste(consumerDetails$base_uri,