【问题标题】:R library to publish a message to Google Cloud Pub/Sub topics用于将消息发布到 Google Cloud Pub/Sub 主题的 R 库
【发布时间】:2022-08-22 23:12:05
【问题描述】:

是否存在用于向 Google Cloud Pub/Sub 主题发布消息的 R 库?

论文 2 库不起作用:

googleCloudRunner::cr_plumber_pubsub(message, pub) (cf https://code.markedmondson.me/googleCloudRunner/articles/usecase-r-event-driven-pubsub.html) 从 pub/sub 触发 R 函数。这不是我想要的。

googleCloudStorageR::gcs_create_pubsub()(cf https://code.markedmondson.me/googleCloudStorageR/reference/gcs_create_pubsub.html) 为存储桶创建发布/订阅通知。

谢谢。

标签: r google-cloud-platform google-cloud-storage


【解决方案1】:

其实,是。您可以使用 googlePubsubR 库。

他们readme on Github的用法示例:

library(googlePubsubR)
library(base64enc)
library(jsonlite)

# Authenticate 
pubsub_auth()

# Create resources
topic_readme <- topics_create("readme-topic")
sub_readme <- subscriptions_create("readme-sub", topic_readme)

# Prepare the message
msg <- mtcars %>%
  toJSON(auto_unbox = TRUE) %>%
  # Pub/Sub expects a base64 encoded string
  msg_encode() %>% 
  PubsubMessage() 

# Publish the message!
topics_publish(msg, topic_readme)

# Pull the message from server
msgs_pull <- subscriptions_pull(sub_readme)

msg_decoded <- msgs_pull$receivedMessages$message$data %>%
  msg_decode() %>% 
  fromJSON()

head(msg_decoded)

# Prints
# mpg cyl disp  hp drat    wt  qsec vs am gear carb
# Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
# Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
# Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
# Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
# Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
# Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1

# We can acknowledge that the message has been consumed
subscriptions_ack(msgs_pull$receivedMessages$ackId, sub_readme)
# [1] TRUE

# A subsequent pull will return no messages from the server
subscriptions_pull(sub_readme)
# named list()

# Cleanup resources
topics_delete(topic_readme)
subscriptions_delete(sub_readme)

【讨论】:

    最近更新 更多