9.6. 分隔文本转换器

分隔文本转换器处理纯分隔文本文件,如CSV或TSV。若要使用分隔文本转换器,请指定 type = "delimited-text" 在您的转换器定义中。

9.6.1. 配置

分隔文件的格式必须使用 format element. GeoMesa uses Apache Commons CSV 用于解析。可用的格式包括 org.apache.commons.csv.CSVFormat

  • DEFAULTCSVCSVFormat.DEFAULT

  • TDFTSVCSVFormat.TDF

  • QUOTEDCSVFormat.DEFAULT.withQuoteMode(QuoteMode.ALL)

  • QUOTE_ESCAPECSVFormat.DEFAULT.withEscape('"')

  • QUOTED_WITH_QUOTE_ESCAPECSVFormat.DEFAULT.withEscape('"').withQuoteMode(QuoteMode.ALL)

  • EXCELCSVFormat.EXCEL

  • MYSQLCSVFormat.MYSQL

  • RFC4180CSVFormat.RFC4180

此外,GeoMesa还支持自定义引号、转义符和分隔符,可用于修改基本格式。可以通过以下方式指定 options.quoteoptions.escapeoptions.delimiter 。通过将该选项设置为空字符串,可以禁用引号和转义。

如果输入文件有标题行,则可以通过使用指定要跳过的行数来跳过它们 options.skip-lines ,例如 options.skip-lines = 1

9.6.2. 变换函数

这个 transform 元素支持使用列号引用记录中的每个字段 $$0 是指整行,那么第一列是 $1 每一列最初都是一个字符串,因此可能需要进一步转换才能创建正确的类型。看见 变换函数概述 了解更多详细信息。

9.6.3. 用法示例

假设您有一个 SimpleFeatureType 具有以下架构:

phrase:String,dtg:Date,*geom:Point:srid=4326

并且您有以下逗号分隔的数据:

first,hello,2015-01-01T00:00:00.000Z,45.0,45.0
second,world,2015-01-01T00:00:00.000Z,45.0,45.0

我们希望将前两个字段连接在一起以形成短语,将第三个字段解析为日期,并使用最后两个字段作为 Point 几何图形。下面的配置定义了一个适当的转换器,用于获取此CSV数据并将其转换为我们的 SimpleFeatureType

geomesa.converters.example = {
  type     = "delimited-text",
  format   = "CSV",
  id-field = "md5(stringToBytes($0))",
  fields = [
    { name = "phrase", transform = "concatenate($1, $2)" },
    { name = "dtg",    transform = "dateHourMinuteSecondMillis($3)" },
    { name = "lat",    transform = "$4::double" },
    { name = "lon",    transform = "$5::double" },
    { name = "geom",   transform = "point($lon, $lat)" }
  ]
  user-data = {
    // note: keys will be treated as strings and should not be quoted
    my.user.key = "$phrase"
  }
}

这个 idSimpleFeature 由整个记录的MD5散列组成 ($0 是原始数据)。简单要素属性是通过 fields 带有适当转换的列表(请注意中间字段‘lat’和‘lon’的使用)。如果需要,可以通过引用字段来设置该功能的用户数据。这可用于设置数据可见性约束等(请参见 数据安全 )。