将 Java 对象转换为 BigQuery TableRow

use*_*010 6 dataflow

我正在探索 Google Cloud Dataflow。

我想知道是否可以在java对象或JSON到TableRow之间自动转换。

就像我们可以自动将JSON解析为POJO类一样。

我找不到相关信息。希望不要重复问题。

如有任何信息,将不胜感激!

问候

小智 1

我一直在寻找同样的例子,但没有运气。我创建了一个 POJO 类,它几乎与 bigquery 表的架构匹配,并且与作为管道输入的 JSON 对象的结构匹配。最后,当我必须将这些对象转换为 TableRow 时,对于嵌套和重复的值,我做了如下所示的操作,并且转换是由 API 进行的

    TableRow row = new TableRow()
            .set("items", c.element().getItems())
            .set("orderDate", c.element().getOrderDate())
            .set("orderNumber", c.element().getOrderNumber());
Run Code Online (Sandbox Code Playgroud)

其中 Item 类是 Order 对象的一部分:

@JsonProperty("items")
private List<Item> items = null;
Run Code Online (Sandbox Code Playgroud)

这是 Item 类的代码:

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
    "id",
    "code",
    "detail",
    "name",
    "shortName",
    "description",
    "sku",
    "quantity",
    "category",
    "products"
})
public class Item implements Serializable
{

    @JsonProperty("id")
    private Integer id;
    @JsonProperty("code")
    private String code;
    @JsonProperty("detail")
    private String detail;
    @JsonProperty("name")
    private String name;
    @JsonProperty("shortName")
    private String shortName;
    @JsonProperty("description")
    private String description;
    @JsonProperty("sku")
    private String sku;
    @JsonProperty("quantity")
    private Integer quantity;
    @JsonProperty("category")
    private Category category;
    @JsonProperty("products")
    private List<Product> products = null;
    @JsonIgnore
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();
    private final static long serialVersionUID = -5644586446669059821L;

    @JsonProperty("id")
    public Integer getId() {
        return id;
    }

    @JsonProperty("id")
    public void setId(Integer id) {
        this.id = id;
    }

    @JsonProperty("code")
    public String getCode() {
        return code;
    }

    @JsonProperty("code")
    public void setCode(String code) {
        this.code = code;
    }

    @JsonProperty("detail")
    public String getDetail() {
        return detail;
    }

    @JsonProperty("detail")
    public void setDetail(String detail) {
        this.detail = detail;
    }

    @JsonProperty("name")
    public String getName() {
        return name;
    }

    @JsonProperty("name")
    public void setName(String name) {
        this.name = name;
    }

    @JsonProperty("shortName")
    public String getShortName() {
        return shortName;
    }

    @JsonProperty("shortName")
    public void setShortName(String shortName) {
        this.shortName = shortName;
    }

    @JsonProperty("description")
    public String getDescription() {
        return description;
    }

    @JsonProperty("description")
    public void setDescription(String description) {
        this.description = description;
    }

    @JsonProperty("sku")
    public String getSku() {
        return sku;
    }

    @JsonProperty("sku")
    public void setSku(String sku) {
        this.sku = sku;
    }

    @JsonProperty("quantity")
    public Integer getQuantity() {
        return quantity;
    }

    @JsonProperty("quantity")
    public void setQuantity(Integer quantity) {
        this.quantity = quantity;
    }

    @JsonProperty("category")
    public Category getCategory() {
        return category;
    }

    @JsonProperty("category")
    public void setCategory(Category category) {
        this.category = category;
    }

    @JsonProperty("products")
    public List<Product> getProducts() {
        return products;
    }

@JsonProperty("products")
public void setProducts(List<Product> products) {
    this.products = products;
}

@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
    return this.additionalProperties;
}

@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
    this.additionalProperties.put(name, value);
}
}
Run Code Online (Sandbox Code Playgroud)

这是 BigQuery 表关于 Items 的架构,其中 Item 是 RECORD 和 REPEATED 字段,还包含嵌套的 RECORD 和 REPEATED 字段:产品。查看架构的屏幕截图

BQ 中的项目架构字段