9.2. 解析和验证

GeoMesa提供了处理错误输入记录的选项,可以在您的转换器上进行配置。例如,您可能希望跳过某些输入源的错误记录,或者在其他情况下使整个文件失败。Converter框架提供了一些选项,可以帮助控制如何处理输入数据中的错误。

9.2.1. 验证器

转换器的核心是将输入流转换为SimpleFeature。验证器提供挂钩来在将这些SimpleFeature写入到GeoMesa之前验证它们的属性。例如,您可能希望验证是否存在几何图形字段以及该几何图形是否有效。

提供了四个用于GeoMesa转换器的验证器:

  • index -验证SimpleFeature的几何图形和日期是否在相关GeoMesa Z-Index实施的空间/时间范围内(即Z2、Z3、XZ2、XZ3)

  • has-geo -验证SimpleFeature是否具有非空几何图形

  • has-dtg -验证SimpleFeature是否具有非空日期

  • cql -验证SimpleFeature是否通过任意CQL筛选器

其他验证器可以通过Java SPI加载,方法是实现 org.locationtech.geomesa.convert2.validators.SimpleFeatureValidatorFactory 并包括专门的服务描述符文件。有关其他信息,请参阅下面的内容。

默认情况下, index 已启用验证器。这适用于大多数用例,因为它将根据SimpleFeatureType选择适当的验证器。要启用其他验证器,请在转换器定义的选项块中指定它们:

geomesa.converters.myconverter {
  options {
    validators = [ "has-dtg", "cql(bbox(geom,-75,-90,-45,90))" ]
  }
}

可以通过将其设置为空数组来禁用验证。

9.2.1.1. 自定义验证器

自定义验证器可以通过以下方式通过Java SPI加载 org.locationtech.geomesa.convert2.validators.SimpleFeatureValidatorFactory, shown below. Note that validators must be registered through a special service descriptor file

trait SimpleFeatureValidatorFactory {

  /**
    * Well-known name of this validator, for specifying the validator to use
    *
    * @return
    */
  def name: String

  /**
    * Create a validator for the given feature typ
    *
    * @param sft simple feature type
    * @param metrics metrics registry for reporting validation
    * @param config optional configuration string
    */
  def apply(sft: SimpleFeatureType, metrics: ConverterMetrics, config: Option[String]): SimpleFeatureValidator
}

验证器提供了一个 Dropwizard MetricRegistry ,它可用于注册自定义验证度量。看见 量度 ,见下文。

在转换器配置中指定验证器时, name 必须与工厂的 validators 弦乐。任何其他参数都可以在圆括号中指定,这些参数将传递给 validator 方法。例如::

geomesa.converters.myconverter {
  options {
    validators = [ "my-custom-validator(optionA,optionB)" ]
  }
}
import org.locationtech.geomesa.convert2.validators.SimpleFeatureValidatorFactory

class MyCustomValidator extends SimpleFeatureValidatorFactory {

  override val name: String = "my-custom-validator"

  override def apply(
      sft: SimpleFeatureType,
      metrics: ConverterMetrics,
      config: Option[String]): SimpleFeatureValidator = {
    if (config.exists(_.contains("optionA"))) {
      // handle option a
    } else {
      // handle other options
    }
  }
}

请参阅地理台地 unit tests 以获取样例实现。

有关实现服务提供商的更多详细信息,请参阅 Oracle Javadoc

9.2.2. 错误模式

有两种类型的模式用于处理错误:

  • skip-bad-records

  • raise-errors

raise-errors 如果基于解析或验证检测到错误数据,则模式将抛出IOException。在第一次开发和测试转换器定义时,这可能特别有用。 skip-bad-records 模式仍将提供调试级日志记录,但不会引发异常。要配置错误模式,请将以下选项添加到转换器的类型安全配置中:

geomesa.converters.myconverter {
  options {
    error-mode = "raise-errors"
  }
}

9.2.3. 解析模式

解析模式选项允许您控制文件在转换之前是增量解析还是完全解析。在进行验证时,这一点很重要。有两种模式可用:

  • incremental

  • batch

由于转换器提供SimpleFeature的迭代器,因此默认解析模式为 incremental 这提供了更好的性能和更少的内存开销。vbl.使用 incremental 解析模式意味着在引发错误之前,数据可能会被部分吸收到GeoMesa中。在大多数情况下,这是适当的,并且可以由客户端代码处理。

vbl.使用 batch 模式将在内存中缓冲整个输入流或文件,以验证性能较差且需要更多内存的数据。 batch 然而,模式确实会阻止部分接收的数据流。您不太可能需要使用 batch 数据经过适当清理的环境中的模式。

要配置解析模式,请使用在转换器的类型安全配置中添加一个选项:

geomesa.converters.myconverter {
  options {
    parse-mode = "incremental"
  }
}

9.2.4. 日志记录

若要查看验证日志,可以对包启用信息或调试级别日志记录 org.locationtech.geomesa.convertorg.locationtech.geomesa.convert2

当在信息级别启用日志记录时,它将只显示失败的字段。在调试级别启用时,它将显示整个记录以及堆栈跟踪。

9.2.5. 量度

转换器使用 Dropwizard Metrics 库来注册关于成功转换、失败转换、验证错误和处理速率的度量。指标可以通过转换器评估上下文访问,也可以通过在转换器选项中配置的报告器公开:

geomesa.converters.myconverter {
  options {
    reporters = [
      {
        type           = "slf4j"
        logger         = "com.example.MyConverter"
        level          = "INFO"
        rate-units     = "SECONDS"
        duration-units = "MILLISECONDS"
        interval       = "10 seconds"
      }
    ]
  }
}

看见 GeoMesa指标 有关配置不同的报告器的更多详细信息。

9.2.6. 交易注意事项

GeoMesa使用的大多数数据存储(Acumulo、HBase等)都不提供事务。因此,流入和流出转换器以及流入接收管道的数据不是事务性的。要模拟事务,可以使用批处理解析模式 raise-errors 错误模式,并使用 index 验证器。请注意,这可能会增加内存需求并影响性能:

geomesa.converters.myconverter {
  options {
    validators = [ "index" ]
    parse-mode = "batch"
    error-mode = "raise-errors"
  }
}

如果需要通知错误的输入数据,可以考虑使用错误模式 raise-errors 使用增量解析模式:

geomesa.converters.myconverter {
  options {
    validators = [ "index" ]
    parse-mode = "incremental"
    error-mode = "raise-errors"
  }
}

如果您使用的是GeoMesa Nifi处理器之类的框架,则文件仍将被路由到错误关系,但您可能会遇到部分数据被摄取的情况。看见 GeoMesa NiFi捆绑包 更多信息。

9.2.7. 使用系统属性管理解析和验证配置

对于推断的转换器,可以通过系统属性或来管理解析、行和验证模式 geomesa-site.xml 。对于下表中的每种模式,都给出了相应的属性名称。

系统属性

错误模式

geomesa.converter.error.mode.default

解析模式

geomesa.converter.parse.mode.default

线路模式

geomesa.converter.line.mode.default

验证器模式

geomesa.converter.validators