根据 Google Cloud Pub Sub 消息格式在 R 中构建 JSON 内容
Build JSON content in R according Google Cloud Pub Sub message format
在 R 中,我想根据此 Google Cloud Pub Sub 消息格式构建 json 内容:https://cloud.google.com/pubsub/docs/reference/rest/v1/PubsubMessage
不得不尊重:
{
"data": string,
"attributes": {
string: string,
...
},
"messageId": string,
"publishTime": string,
"orderingKey": string
}
构建的消息将从这个Python代码中读取:
def pubsub_read(data, context):
'''This function is executed from a Cloud Pub/Sub'''
message = base64.b64decode(data['data']).decode('utf-8')
file_name = data['attributes']['file_name']
以下 R 代码构建 R 数据帧并将其转换为 json 内容:
library(jsonlite)
data="Hello World!"
df <- data.frame(data)
attributes <- data.frame(file_name=c('gfs_data_temp_FULL.csv'))
df$attributes <- attributes
msg <- df %>%
toJSON(auto_unbox = TRUE, dataframe = 'columns', pretty = T) %>%
# Pub/Sub expects a base64 encoded string
googlePubsubR::msg_encode() %>%
googlePubsubR::PubsubMessage()
看起来不错,但是当我用 json 编辑器可视化它时:
添加了索引。
另外还有留言内容:
我不确定它是否尊重 Google Cloud Pub Sub 消息格式...
不确定为什么,但用列表替换数据框似乎可行:
library(jsonlite)
df = list(data = "Hello World")
attributes <- list(file_name=c('toto.csv'))
df$attributes <- attributes
df %>%
toJSON(auto_unbox = TRUE, simplifyVector=TRUE, dataframe = 'columns', pretty = T)
输出:
{
"data": "Hello World",
"attributes": {
"file_name": "toto.csv"
}
}
在 R 中,我想根据此 Google Cloud Pub Sub 消息格式构建 json 内容:https://cloud.google.com/pubsub/docs/reference/rest/v1/PubsubMessage
不得不尊重:
{
"data": string,
"attributes": {
string: string,
...
},
"messageId": string,
"publishTime": string,
"orderingKey": string
}
构建的消息将从这个Python代码中读取:
def pubsub_read(data, context):
'''This function is executed from a Cloud Pub/Sub'''
message = base64.b64decode(data['data']).decode('utf-8')
file_name = data['attributes']['file_name']
以下 R 代码构建 R 数据帧并将其转换为 json 内容:
library(jsonlite)
data="Hello World!"
df <- data.frame(data)
attributes <- data.frame(file_name=c('gfs_data_temp_FULL.csv'))
df$attributes <- attributes
msg <- df %>%
toJSON(auto_unbox = TRUE, dataframe = 'columns', pretty = T) %>%
# Pub/Sub expects a base64 encoded string
googlePubsubR::msg_encode() %>%
googlePubsubR::PubsubMessage()
看起来不错,但是当我用 json 编辑器可视化它时:
添加了索引。
另外还有留言内容:
我不确定它是否尊重 Google Cloud Pub Sub 消息格式...
不确定为什么,但用列表替换数据框似乎可行:
library(jsonlite)
df = list(data = "Hello World")
attributes <- list(file_name=c('toto.csv'))
df$attributes <- attributes
df %>%
toJSON(auto_unbox = TRUE, simplifyVector=TRUE, dataframe = 'columns', pretty = T)
输出:
{
"data": "Hello World",
"attributes": {
"file_name": "toto.csv"
}
}