如何将 JsonObject 转换为 Avro 通用记录?

问题描述 投票:0回答:0

我还是这个领域的新手,目前我正在尝试使用 Apache Beam 中的

ParDo
DoFn
来反序列化镶木地板文件中的数据,对记录进行一些修改,然后更新
GenericRecord
.

最初,我开始迭代 GenericArray 类型的字段,然后将每个对象(GenericRecord)转换为 JSONObject,因为我必须遍历多个嵌套并更新各自的值。我能够做到这一点,但现在我正在努力将更新的 JSONObject 转换回 GenericRecord,以便整个 GenericArray 得到更新。

val attributes = dataNestedObjGroup.getAsGenericRecord("details")?.get("attributes") as GenericArray<GenericRecord>
attributes.forEach { attribute ->
    val jsonRootObject = JSONObject(attribute.toString())
    jsonRootObject.keySet().forEach { keyStr ->
        val value = jsonRootObject.get(keyStr)
        
        val innerObject = JSONObject(value?.toString())
        val valueOfKey = innerObject.getJSONArray("value")
        if(innerObject.get("name") == "customer_id" && valueOfKey != null){
            // do something
            innerObject.put("value",JsonArray())
        }        
        jsonRootObject.put(keyStr,innerObject)
    }
    // here i have to update the attribute which is a GenericRecord type with the above jsonRootObject (which is a JSONObject type) I've derived above
}

非常感谢任何帮助。

java json kotlin apache-beam avro
© www.soinside.com 2019 - 2024. All rights reserved.