Leo*_*oli 6 google-bigquery google-cloud-pubsub google-cloud-dataflow
我想使用Google Cloud Dataflow将来自主题的PubSub消息数据插入到BigQuery表中.一切都很好,但在BigQuery表中我可以看到像"߈ "这样难以理解的字符串.这是我的管道:
p.apply(PubsubIO.Read.named("ReadFromPubsub").topic("projects/project-name/topics/topic-name"))
.apply(ParDo.named("Transformation").of(new StringToRowConverter()))
.apply(BigQueryIO.Write.named("Write into BigQuery").to("project-name:dataset-name.table")
.withSchema(schema)
.withCreateDisposition(BigQueryIO.Write.CreateDisposition.CREATE_IF_NEEDED))
Run Code Online (Sandbox Code Playgroud)
我的简单StringToRowConverter函数是:
class StringToRowConverter extends DoFn<String, TableRow> {
private static final long serialVersionUID = 0;
@Override
public void processElement(ProcessContext c) {
for (String word : c.element().split(",")) {
if (!word.isEmpty()) {
System.out.println(word);
c.output(new TableRow().set("data", word));
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我通过POST请求发送的消息:
POST https://pubsub.googleapis.com/v1/projects/project-name/topics/topic-name:publish
{
"messages": [
{
"attributes":{
"key": "tablet, smartphone, desktop",
"value": "eng"
},
"data": "34gf5ert"
}
]
}
Run Code Online (Sandbox Code Playgroud)
我错过了什么?谢谢!
根据https://cloud.google.com/pubsub/reference/rest/v1/PubsubMessage,pubsub消息的JSON有效负载是base64编码的.默认情况下,Dataflow中的PubsubIO使用String UTF8编码器.您提供的示例字符串"34gf5ert",当base64解码然后被解释为UTF-8字符串时,恰好给出"߈ ".