From: Frederik Vanrenterghem Date: Fri, 14 Sep 2018 12:04:58 +0000 (+0800) Subject: Break out SubscribeKafkaTopic function. X-Git-Url: http://git.vanrenterghem.biz/R/project-using-kafka-in-R.git/commitdiff_plain/5b2ed10b2cd8f0326a38e9658a3d5cecf2849c6e?ds=sidebyside;hp=0ce8ac9c84fc8a3f79d46469a9c3c37232bb62f5 Break out SubscribeKafkaTopic function. --- diff --git a/R/SubscribeKafkaTopic.R b/R/SubscribeKafkaTopic.R new file mode 100644 index 0000000..5e6976b --- /dev/null +++ b/R/SubscribeKafkaTopic.R @@ -0,0 +1,12 @@ +SubscribeKafkaTopic <- function(consumer.base.uri, topic) { + response <- POST(url=paste(consumer.base.uri, + "subscription", sep="/"), + content_type("application/vnd.kafka.v2+json"), + body = paste0('{"topics":["', + topic, + '"]}') + ) + if(response$status_code == 204) { + return(response) + } else stop(response) +} \ No newline at end of file diff --git a/kafkaConsumer.R b/kafkaConsumer.R index d6b092b..969fabd 100644 --- a/kafkaConsumer.R +++ b/kafkaConsumer.R @@ -19,14 +19,8 @@ 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 = paste0('{"topics":["', - topic, - '"]}') - ) -response +source("R/SubscribeKafkaTopic.R") +response <- SubscribeKafkaTopic(consumerDetails$base_uri,topic = topic) # Obtain all (or latest) messages on the topic source("R/ConsumeKafkaMessages.R")