【发布时间】:2016-08-28 16:00:54
【问题描述】:
我有以下 DoFN 函数可以做到这一点,但没有我能找到有关它的问题的文档。
- 问题 1 是如何自动转换键,以便它们在 BigQuery 中以与导入表单数据存储备份文件时 BigQuery 相同的方式构建?
- 问题二是如何处理时间戳?下面的代码通过以下消息打破了管道:
为非记录字段指定的 JSON 对象:时间戳
这是我写的代码:
public class SensorObservationEntityToRowFn extends DoFn<Entity, TableRow> {
/**
* In this example, put the whole string into single BigQuery field.
*/
@Override
public void processElement(ProcessContext c) {
Map<String, Value> props = getPropertyMap(c.element());
TableRow row = new TableRow();
row.set("id", c.element().getKey().getPathElement(c.element().getKey().getPathElementCount()-1).getId());
if (
props.get("property1") != null &&
props.get("property2") != null
) {
// Map data from the source Entity to the destination TableRow
row.set("property1", props.get("property1").getStringValue());
row.set("property2", props.get("property2").getStringValue());
}
row.set("source_type", props.get("source_type").getStringValue());
DateTime dateTime = new DateTime(props.get("timestamp").getTimestampMicrosecondsValue()/1000L);
row.set("timestamp", dateTime);
// Output new TableRow only if all data is present in the source
c.output(row);
}
}
【问题讨论】:
-
我刚刚找到了时间戳问题的解决方案:
DateTime dateTime = new DateTime(props.get("timestamp").getTimestampMicrosecondsValue()/1000L); row.set("timestamp", ISODateTimeFormat.dateTime().print(dateTime));不知道应该如何构造时间戳以避免使用未知库。 -
如果这解决了您的问题,请您将其发布为答案吗?
标签: java google-app-engine google-cloud-dataflow