Support
Quality
Security
License
Reuse
kandi has reviewed LogiKM and discovered the below as its top functions. This is intended to give you an instant insight into LogiKM implemented functionality, and help decide if they suit your requirements.
一站式Apache Kafka集群指标监控与运维管控平台
No Code Snippets are available at this moment for LogiKM.
QUESTION
Build JSON content in R according Google Cloud Pub Sub message format
Asked 2022-Apr-16 at 09:59In R, I want to build json content according this Google Cloud Pub Sub message format: https://cloud.google.com/pubsub/docs/reference/rest/v1/PubsubMessage
It have to respect :
{
"data": string,
"attributes": {
string: string,
...
},
"messageId": string,
"publishTime": string,
"orderingKey": string
}
The message built will be readed from this Python code:
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']
This following R code builds a R dataframe and converts it to json content:
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()
It seems good but when I visualise it with a json editor :
indexes are added.
Additionally there is the message content:
I dont'sure it respects Google Cloud Pub Sub message format...
ANSWER
Answered 2022-Apr-16 at 09:59Not sure why, but replacing the dataframe by a list seems to work:
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)
Output:
{
"data": "Hello World",
"attributes": {
"file_name": "toto.csv"
}
}
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
No vulnerabilities reported
Save this library and start creating your kit
Save this library and start creating your kit