Scala 对象映射器写入不正确的值
Scala Object Mapper write incorrect value
我有一个包含另一个对象的复杂对象。我想把它写成 yaml file.I am using scala with 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)
我想把它写入文件。我的主要案例 class 看起来像这样
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
并且进一步输入,输出都class也添加了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 中看到的,有几个字段具有空值。我不想写它们。我怎样才能做到这一点?我只是不想让那些字段出现在 Yaml 中。
Jackson 不知道如何序列化 Option
,因为它是一个 Java 库。
您可以添加 jackson-scala-module 库并在您的 ObjectMapper
上注册 DefaultScalaModule
以让它知道如何序列化常见的 Scala 类型.
我有一个包含另一个对象的复杂对象。我想把它写成 yaml file.I am using scala with 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)
我想把它写入文件。我的主要案例 class 看起来像这样
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
并且进一步输入,输出都class也添加了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 中看到的,有几个字段具有空值。我不想写它们。我怎样才能做到这一点?我只是不想让那些字段出现在 Yaml 中。
Jackson 不知道如何序列化 Option
,因为它是一个 Java 库。
您可以添加 jackson-scala-module 库并在您的 ObjectMapper
上注册 DefaultScalaModule
以让它知道如何序列化常见的 Scala 类型.