当引入源文件采用 JSON 格式时,可使用 JSON 映射将传入数据映射到表内的列。

映射列表中的每个元素定义特定列的映射。 这些元素通过这三个属性进行构造: column datatype properties 。 有关详细信息,请参阅 数据映射概述

每个 JSON 映射元素必须包含以下可选属性之一:

string 如果值以 开头 $ ,则会将其解释为 JSON 文档中字段的 JSON 路径,该字段将成为表中列的内容。 表示整个文档的 JSON 路径为 $ 。 如果值不以 $ 开头,则将其解释为常量值。 包含特殊字符的 JSON 路径应转义为 ["属性名称"]。 有关详细信息,请参阅 JSONPath 语法 。 ConstValue string 要用于列而非 JSON 文件内某个值的常数值。 string 应通过 映射转换 应用于内容的转换。 {"Column": "event_timestamp", "Properties": {"Path": "$.Timestamp"}}, {"Column": "event_name", "Properties": {"Path": "$.Event.Name"}}, {"Column": "event_type", "Properties": {"Path": "$.Event.Type"}}, {"Column": "source_uri", "Properties": {"Transform": "SourceLocation"}}, {"Column": "source_line", "Properties": {"Transform": "SourceLineNumber"}}, {"Column": "event_time", "Properties": {"Path": "$.Timestamp", "Transform": "DateTimeFromUnixMilliseconds"}}, {"Column": "ingestion_time", "Properties": {"ConstValue": "2021-01-01T10:32:00"}}, {"Column": "full_record", "Properties": {"Path": "$"}}

当上述映射作为 .ingest 控制命令的一部分提供时,它将被序列化为 JSON 字符串。

.ingest into Table123 (@"source1", @"source2")
      format = "json",
      ingestionMapping =
        {"Column": "column_a", "Properties": {"Path": "$.Obj.Property"}},
        {"Column": "column_b", "Properties": {"Path": "$.Property"}},
        {"Column": "custom_column", "Properties": {"Path": "$.[\'Property name with space\']"}}

预先创建的映射

预先创建映射后,请在 .ingest 控制命令中按名称引用映射。

.ingest into Table123 (@"source1", @"source2")
        format="json",
        ingestionMappingReference = "Mapping_Name"

在引入期间使用 JSON 映射而不定义映射架构(请参阅标识映射)。

.ingest into Table123 (@"source1", @"source2")
        format="json"

复制 JSON 映射

可使用以下过程复制现有表的 JSON 映射,并使用相同的映射来创建新表:

  • 在要复制其映射的表上运行以下命令:

    .show table TABLENAME ingestion json mappings
    | extend formatted_mapping = strcat("'",replace_string(Mapping, "'", "\\'"),"'")
    | project formatted_mapping
    
  • 使用上述命令的输出创建具有相同映射的新表:

    .create table TABLENAME ingestion json mapping "TABLENAME_Mapping" RESULT_OF_ABOVE_CMD
    
  •