【发布时间】:2021-08-22 20:31:26
【问题描述】:
我有一个包含另一个对象的复杂对象。我想把它写成 yaml 文件。我使用 scala 和 ObjectMapper 。我的最终对象是这样的
Configuration(Some(List(s3://etl/configuration/minioApp/23-08-2021/metrics.yaml)),Some(Map(inputDataFrame -> Input(None,Some(Kafka(List(uguyhi, ytvvt),Some(hgvgvugb),Some(ytfytvi),Some(yftug),None,None,None,Some( uyguytf)))))),None,Some(Output(None,Some(Kafka(null,Some(yrdryft),None)))),None,None,None,None,None,None,None,None,Some(minioApp),None,None,None,None)
我想将它写入文件。我的主要案例类是这样的
case class Configuration(@BeanProperty var metrics: Option[Seq[String]],
@BeanProperty var inputs: Option[Map[String, Input]],
@BeanProperty var variables: Option[Map[String, String]] = None,
@BeanProperty var output: Option[Output] = None,
@BeanProperty var outputs: Option[Map[String, Output]] = None,
@BeanProperty var cacheOnPreview: Option[Boolean] = None,
@BeanProperty var showQuery: Option[Boolean] = None,
@BeanProperty var streaming: Option[Streaming] = None,
@BeanProperty var periodic: Option[Periodic] = None,
@BeanProperty var logLevel: Option[String] = None,
@BeanProperty var showPreviewLines: Option[Int] = None,
@BeanProperty var explain: Option[Boolean] = None,
@BeanProperty var appName: Option[String] = None,
@BeanProperty var continueOnFailedStep: Option[Boolean] = None,
@BeanProperty var cacheCountOnOutput: Option[Boolean] = None,
@BeanProperty var ignoreDeequValidations: Option[Boolean] = None,
@BeanProperty var failedDFLocationPrefix: Option[String] = None) extends Conf
进一步的输入、输出所有类也都添加了BeanProperty。当我尝试使用以下片段编写此文件时。
val objectMapper = new ObjectMapper(new YAMLFactory)
objectMapper.writeValue(new File("/Users/ayush.goyal/Downloads/servingWrapper/src/main/resources/input1.yaml"),jobYamls)
如上所述,jobYaml 是我的对象。我得到以下文件。
metrics:
empty: false
defined: true
inputs:
empty: false
defined: true
variables:
empty: true
defined: false
output:
empty: false
defined: true
outputs:
empty: true
defined: false
cacheOnPreview:
empty: true
defined: false
showQuery:
empty: true
defined: false
streaming:
empty: true
defined: false
periodic:
empty: true
defined: false
logLevel:
empty: true
defined: false
showPreviewLines:
empty: true
defined: false
explain:
empty: true
defined: false
appName:
empty: false
defined: true
continueOnFailedStep:
empty: true
defined: false
cacheCountOnOutput:
empty: true
defined: false
ignoreDeequValidations:
empty: true
defined: false
failedDFLocationPrefix:
empty: true
defined: false
现在这个文件有 2 个问题
- 对象的值未填充
- 我不想填充具有空值的字段。
我该怎么做?
编辑:
根据建议,我可以低于 yaml
---
metrics:
- "s3://etl/configuration/minioApp/23-08-2021/metrics.yaml"
inputs:
inputDataFrame:
file: null
kafka:
servers:
- "uguyhi"
- "ytvvt"
topic: "hgvgvugb"
topicPattern: "ytfytvi"
consumerGroup: "yftug"
options: null
schemaRegistryUrl: "yrftg"
schemaSubject: null
schemaId: " uyguytf"
output:
file: null
kafka:
vservers: null
checkpointLocation: "yrdryft"
compressionType: null
streaming: null
appName: "minioApp"
正如您在上面的 yaml 中看到的,有几个字段具有 null 值。我不想写它们。我怎样才能做到这一点?我只是不希望这些字段出现在 Yaml 中。
【问题讨论】:
标签: scala jackson yaml objectmapper