【问题标题】:Convert Java object to BigQuery TableRow将 Java 对象转换为 BigQuery TableRow
【发布时间】:2017-08-04 14:32:26
【问题描述】:

我正在探索 Google Cloud Dataflow。

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

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

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

将不胜感激任何信息!

问候

【问题讨论】:

    标签: dataflow


    【解决方案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());
    

    Item 类是 Order 对象的一部分:

    @JsonProperty("items")
    private List<Item> items = null;
    

    这是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);
    }
    }
    

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

    Item schema fields in BQ

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-08-17
      • 1970-01-01
      • 2017-04-29
      • 2012-08-01
      • 2012-08-29
      • 2012-05-29
      • 2016-05-14
      • 2020-09-01
      相关资源
      最近更新 更多