编写摄取规范
本教程将引导读者完成定义摄取规范的过程,并指出其中的关键注意事项和指导原则。
本教程假设你已经按照单机快速入门中的说明下载了 Apache® Druid, 并让它在你的本地机器上运行。
如果你已经完成了教程:加载文件、教程:查询数据和教程:Rollup,将会对理解本教程有所帮助。
示例数据
假设我们有以下网络流数据:
srcIP:发送方的 IP 地址srcPort:发送方的端口dstIP:接收方的 IP 地址dstPort:接收方的端口protocol:IP 协议号packets:传输的数据包数bytes:传输的字节数cost:发送该流量的成本
{"ts":"2018-01-01T01:01:35Z","srcIP":"1.1.1.1", "dstIP":"2.2.2.2", "srcPort":2000, "dstPort":3000, "protocol": 6, "packets":10, "bytes":1000, "cost": 1.4}
{"ts":"2018-01-01T01:01:51Z","srcIP":"1.1.1.1", "dstIP":"2.2.2.2", "srcPort":2000, "dstPort":3000, "protocol": 6, "packets":20, "bytes":2000, "cost": 3.1}
{"ts":"2018-01-01T01:01:59Z","srcIP":"1.1.1.1", "dstIP":"2.2.2.2", "srcPort":2000, "dstPort":3000, "protocol": 6, "packets":30, "bytes":3000, "cost": 0.4}
{"ts":"2018-01-01T01:02:14Z","srcIP":"1.1.1.1", "dstIP":"2.2.2.2", "srcPort":5000, "dstPort":7000, "protocol": 6, "packets":40, "bytes":4000, "cost": 7.9}
{"ts":"2018-01-01T01:02:29Z","srcIP":"1.1.1.1", "dstIP":"2.2.2.2", "srcPort":5000, "dstPort":7000, "protocol": 6, "packets":50, "bytes":5000, "cost": 10.2}
{"ts":"2018-01-01T01:03:29Z","srcIP":"1.1.1.1", "dstIP":"2.2.2.2", "srcPort":5000, "dstPort":7000, "protocol": 6, "packets":60, "bytes":6000, "cost": 4.3}
{"ts":"2018-01-01T02:33:14Z","srcIP":"7.7.7.7", "dstIP":"8.8.8.8", "srcPort":4000, "dstPort":5000, "protocol": 17, "packets":100, "bytes":10000, "cost": 22.4}
{"ts":"2018-01-01T02:33:45Z","srcIP":"7.7.7.7", "dstIP":"8.8.8.8", "srcPort":4000, "dstPort":5000, "protocol": 17, "packets":200, "bytes":20000, "cost": 34.5}
{"ts":"2018-01-01T02:35:45Z","srcIP":"7.7.7.7", "dstIP":"8.8.8.8", "srcPort":4000, "dstPort":5000, "protocol": 17, "packets":300, "bytes":30000, "cost": 46.3}将上面的 JSON 内容保存到 quickstart/ 目录下名为 ingestion-tutorial-data.json 的文件中。
接下来,我们一步步定义一个能够加载这些数据的摄取规范。
本教程使用原生批量索引任务。使用其他任务类型时,摄取规范的某些方面会有所不同,本教程会指出这些地方。
定义 schema
Druid 摄取规范的核心元素是 dataSchema。dataSchema 定义了如何将输入数据解析为一组存储在 Druid 中的列。
我们从一个空的 dataSchema 开始,随着教程的推进逐步向其中添加字段。
在 quickstart/ 目录下新建一个名为 ingestion-tutorial-index.json 的文件,内容如下:
"dataSchema" : {}随着教程的推进,我们会不断修改这个摄取规范。
数据源名称
数据源名称由 dataSchema 中的 dataSource 参数指定。
"dataSchema" : {
"dataSource" : "ingestion-tutorial",
}我们将本教程的数据源命名为 ingestion-tutorial。
时间列
dataSchema 需要知道如何从输入数据中提取主时间戳字段。
输入数据中的时间戳列名为“ts”,包含 ISO 8601 格式的时间戳,因此我们向 dataSchema 添加一个包含这些信息的 timestampSpec:
"dataSchema" : {
"dataSource" : "ingestion-tutorial",
"timestampSpec" : {
"format" : "iso",
"column" : "ts"
}
}列类型
定义好时间列之后,我们来看看其他列的定义。
Druid 支持以下列类型:String、Long、Float、Double。我们将在后面的小节中看到如何使用它们。
在介绍如何定义其他非时间列之前,我们先来讨论一下 rollup。
Rollup
摄取数据时,我们必须考虑是否要使用 rollup。
如果启用 rollup,就需要把输入列分为两类:“维度”和“指标”。“维度”是 rollup 时用于分组的列,而“指标”是要被聚合的列。
如果禁用 rollup,所有列都被视为“维度”,不会进行预聚合。
在本教程中,我们启用 rollup。这通过 dataSchema 中的 granularitySpec 来指定。
"dataSchema" : {
"dataSource" : "ingestion-tutorial",
"timestampSpec" : {
"format" : "iso",
"column" : "ts"
},
"granularitySpec" : {
"rollup" : true
}
}选择维度和指标
对于这个示例数据集,下面是一种合理的“维度”和“指标”划分方式:
- 维度:srcIP、srcPort、dstIP、dstPort、protocol
- 指标:packets、bytes、cost
这里的维度是一组用于标识单向 IP 流量的属性,而指标则表示由某个维度组合所确定的 IP 流量的各项事实数据。
我们来看看如何在摄取规范中定义这些维度和指标。
维度
维度通过 dataSchema 中的 dimensionsSpec 来指定。
"dataSchema" : {
"dataSource" : "ingestion-tutorial",
"timestampSpec" : {
"format" : "iso",
"column" : "ts"
},
"dimensionsSpec" : {
"dimensions": [
"srcIP",
{ "name" : "srcPort", "type" : "long" },
{ "name" : "dstIP", "type" : "string" },
{ "name" : "dstPort", "type" : "long" },
{ "name" : "protocol", "type" : "string" }
]
},
"granularitySpec" : {
"rollup" : true
}
}每个维度都有一个 name 和一个 type,其中 type 可以是“long”、“float”、“double”或“string”。
注意,srcIP 是一个“string”维度。对于字符串维度,只需指定维度名称即可,因为“string”是默认的维度类型。
另外注意,protocol 在输入数据中是数值,但我们将其作为“string”列摄取;Druid 会在摄取时把输入的 long 值强制转换为字符串。
字符串与数值
数值类型的输入应当作为数值维度摄取,还是作为字符串维度摄取?
与字符串维度相比,数值维度有以下优缺点:
- 优点:数值表示可以让列在磁盘上占用更少空间,并降低从列中读取值时的处理开销
- 缺点:数值维度没有索引,因此对其进行过滤通常比对等价的字符串维度(具有 位图索引)进行过滤更慢
指标
指标通过 dataSchema 中的 metricsSpec 来指定:
"dataSchema" : {
"dataSource" : "ingestion-tutorial",
"timestampSpec" : {
"format" : "iso",
"column" : "ts"
},
"dimensionsSpec" : {
"dimensions": [
"srcIP",
{ "name" : "srcPort", "type" : "long" },
{ "name" : "dstIP", "type" : "string" },
{ "name" : "dstPort", "type" : "long" },
{ "name" : "protocol", "type" : "string" }
]
},
"metricsSpec" : [
{ "type" : "count", "name" : "count" },
{ "type" : "longSum", "name" : "packets", "fieldName" : "packets" },
{ "type" : "longSum", "name" : "bytes", "fieldName" : "bytes" },
{ "type" : "doubleSum", "name" : "cost", "fieldName" : "cost" }
],
"granularitySpec" : {
"rollup" : true
}
}定义指标时,需要指定 rollup 期间对该列执行哪种类型的聚合。
这里我们对 packets 和 bytes 这两个 long 类型的指标列定义了 long sum 聚合,对 cost 列定义了 double sum 聚合。
注意,我们还定义了一个 count 聚合器。count 聚合器会记录最终摄取的数据中每一个 rollup 后的行由原始输入数据中的多少行汇总而来。
不使用 rollup
如果不使用 rollup,所有列都会在 dimensionsSpec 中指定,例如:
"dimensionsSpec" : {
"dimensions": [
"srcIP",
{ "name" : "srcPort", "type" : "long" },
{ "name" : "dstIP", "type" : "string" },
{ "name" : "dstPort", "type" : "long" },
{ "name" : "protocol", "type" : "string" },
{ "name" : "packets", "type" : "long" },
{ "name" : "bytes", "type" : "long" },
{ "name" : "srcPort", "type" : "double" }
]
},定义粒度
至此,我们已经完成了 dataSchema 中 dimensionsSpec 和 metricsSpec 的定义,摄取规范也差不多写完了。
我们还需要在 granularitySpec 中设置一些其他属性:
- granularitySpec 的类型:
uniform粒度规范定义的段具有统一的时间间隔大小,例如所有段都覆盖一小时的数据。 - 段粒度:单个段应当包含多大时间间隔的数据?例如
DAY、WEEK - 时间列中时间戳的分桶粒度(称为
queryGranularity)
段粒度
段粒度通过 granularitySpec 中的 segmentGranularity 属性配置。在本教程中,我们将按小时创建段:
"dataSchema" : {
"dataSource" : "ingestion-tutorial",
"timestampSpec" : {
"format" : "iso",
"column" : "ts"
},
"dimensionsSpec" : {
"dimensions": [
"srcIP",
{ "name" : "srcPort", "type" : "long" },
{ "name" : "dstIP", "type" : "string" },
{ "name" : "dstPort", "type" : "long" },
{ "name" : "protocol", "type" : "string" }
]
},
"metricsSpec" : [
{ "type" : "count", "name" : "count" },
{ "type" : "longSum", "name" : "packets", "fieldName" : "packets" },
{ "type" : "longSum", "name" : "bytes", "fieldName" : "bytes" },
{ "type" : "doubleSum", "name" : "cost", "fieldName" : "cost" }
],
"granularitySpec" : {
"type" : "uniform",
"segmentGranularity" : "HOUR",
"rollup" : true
}
}我们的输入数据包含来自两个不同小时的事件,因此该任务会生成两个段。
查询粒度
查询粒度通过 granularitySpec 中的 queryGranularity 属性配置。在本教程中,我们使用分钟粒度:
"dataSchema" : {
"dataSource" : "ingestion-tutorial",
"timestampSpec" : {
"format" : "iso",
"column" : "ts"
},
"dimensionsSpec" : {
"dimensions": [
"srcIP",
{ "name" : "srcPort", "type" : "long" },
{ "name" : "dstIP", "type" : "string" },
{ "name" : "dstPort", "type" : "long" },
{ "name" : "protocol", "type" : "string" }
]
},
"metricsSpec" : [
{ "type" : "count", "name" : "count" },
{ "type" : "longSum", "name" : "packets", "fieldName" : "packets" },
{ "type" : "longSum", "name" : "bytes", "fieldName" : "bytes" },
{ "type" : "doubleSum", "name" : "cost", "fieldName" : "cost" }
],
"granularitySpec" : {
"type" : "uniform",
"segmentGranularity" : "HOUR",
"queryGranularity" : "MINUTE",
"rollup" : true
}
}为了了解查询粒度的效果,我们来看原始输入数据中的这一行:
{"ts":"2018-01-01T01:03:29Z","srcIP":"1.1.1.1", "dstIP":"2.2.2.2", "srcPort":5000, "dstPort":7000, "protocol": 6, "packets":60, "bytes":6000, "cost": 4.3}当以分钟级 queryGranularity 摄取这一行时,Druid 会把该行的时间戳向下取整到分钟桶:
{"ts":"2018-01-01T01:03:00Z","srcIP":"1.1.1.1", "dstIP":"2.2.2.2", "srcPort":5000, "dstPort":7000, "protocol": 6, "packets":60, "bytes":6000, "cost": 4.3}定义时间间隔(仅限批量)
对于批量任务,需要定义一个时间间隔。时间戳落在该时间间隔之外的输入行不会被摄取。
时间间隔同样在 granularitySpec 中指定:
"dataSchema" : {
"dataSource" : "ingestion-tutorial",
"timestampSpec" : {
"format" : "iso",
"column" : "ts"
},
"dimensionsSpec" : {
"dimensions": [
"srcIP",
{ "name" : "srcPort", "type" : "long" },
{ "name" : "dstIP", "type" : "string" },
{ "name" : "dstPort", "type" : "long" },
{ "name" : "protocol", "type" : "string" }
]
},
"metricsSpec" : [
{ "type" : "count", "name" : "count" },
{ "type" : "longSum", "name" : "packets", "fieldName" : "packets" },
{ "type" : "longSum", "name" : "bytes", "fieldName" : "bytes" },
{ "type" : "doubleSum", "name" : "cost", "fieldName" : "cost" }
],
"granularitySpec" : {
"type" : "uniform",
"segmentGranularity" : "HOUR",
"queryGranularity" : "MINUTE",
"intervals" : ["2018-01-01/2018-01-02"],
"rollup" : true
}
}定义任务类型
现在我们已经完成了 dataSchema 的定义。剩下的步骤是把创建好的 dataSchema 放入摄取任务规范中,并指定输入源。
所有任务类型都共用 dataSchema,但每种任务类型都有自己的规范格式。在本教程中,我们使用原生批量摄取任务:
{
"type" : "index_parallel",
"spec" : {
"dataSchema" : {
"dataSource" : "ingestion-tutorial",
"timestampSpec" : {
"format" : "iso",
"column" : "ts"
},
"dimensionsSpec" : {
"dimensions": [
"srcIP",
{ "name" : "srcPort", "type" : "long" },
{ "name" : "dstIP", "type" : "string" },
{ "name" : "dstPort", "type" : "long" },
{ "name" : "protocol", "type" : "string" }
]
},
"metricsSpec" : [
{ "type" : "count", "name" : "count" },
{ "type" : "longSum", "name" : "packets", "fieldName" : "packets" },
{ "type" : "longSum", "name" : "bytes", "fieldName" : "bytes" },
{ "type" : "doubleSum", "name" : "cost", "fieldName" : "cost" }
],
"granularitySpec" : {
"type" : "uniform",
"segmentGranularity" : "HOUR",
"queryGranularity" : "MINUTE",
"intervals" : ["2018-01-01/2018-01-02"],
"rollup" : true
}
}
}
}定义输入源
现在我们来定义输入源,它在 ioConfig 对象中指定。每种任务类型都有自己的 ioConfig 类型。要读取输入数据,需要指定一个 inputSource。前面保存的 netflow 示例数据需要从本地文件读取,配置如下:
"ioConfig" : {
"type" : "index_parallel",
"inputSource" : {
"type" : "local",
"baseDir" : "quickstart/",
"filter" : "ingestion-tutorial-data.json"
}
}定义数据格式
由于输入数据以 JSON 字符串表示,我们将 inputFormat 设为 json 格式:
"ioConfig" : {
"type" : "index_parallel",
"inputSource" : {
"type" : "local",
"baseDir" : "quickstart/",
"filter" : "ingestion-tutorial-data.json"
},
"inputFormat" : {
"type" : "json"
}
}{
"type" : "index_parallel",
"spec" : {
"dataSchema" : {
"dataSource" : "ingestion-tutorial",
"timestampSpec" : {
"format" : "iso",
"column" : "ts"
},
"dimensionsSpec" : {
"dimensions": [
"srcIP",
{ "name" : "srcPort", "type" : "long" },
{ "name" : "dstIP", "type" : "string" },
{ "name" : "dstPort", "type" : "long" },
{ "name" : "protocol", "type" : "string" }
]
},
"metricsSpec" : [
{ "type" : "count", "name" : "count" },
{ "type" : "longSum", "name" : "packets", "fieldName" : "packets" },
{ "type" : "longSum", "name" : "bytes", "fieldName" : "bytes" },
{ "type" : "doubleSum", "name" : "cost", "fieldName" : "cost" }
],
"granularitySpec" : {
"type" : "uniform",
"segmentGranularity" : "HOUR",
"queryGranularity" : "MINUTE",
"intervals" : ["2018-01-01/2018-01-02"],
"rollup" : true
}
},
"ioConfig" : {
"type" : "index_parallel",
"inputSource" : {
"type" : "local",
"baseDir" : "quickstart/",
"filter" : "ingestion-tutorial-data.json"
},
"inputFormat" : {
"type" : "json"
}
}
}
}其他调优
每个摄取任务都有一个 tuningConfig 部分,用户可以通过它调优各种摄取参数。
例如,我们为原生批量摄取任务添加一个设置目标段大小的 tuningConfig:
"tuningConfig" : {
"type" : "index_parallel",
"partitionsSpec": {
"type": "dynamic",
"maxRowsPerSegment" : 5000000
}
}注意,每种摄取任务都有自己的 tuningConfig 类型。
最终规范
我们已经完成了摄取规范的定义,它现在应该如下所示:
{
"type" : "index_parallel",
"spec" : {
"dataSchema" : {
"dataSource" : "ingestion-tutorial",
"timestampSpec" : {
"format" : "iso",
"column" : "ts"
},
"dimensionsSpec" : {
"dimensions": [
"srcIP",
{ "name" : "srcPort", "type" : "long" },
{ "name" : "dstIP", "type" : "string" },
{ "name" : "dstPort", "type" : "long" },
{ "name" : "protocol", "type" : "string" }
]
},
"metricsSpec" : [
{ "type" : "count", "name" : "count" },
{ "type" : "longSum", "name" : "packets", "fieldName" : "packets" },
{ "type" : "longSum", "name" : "bytes", "fieldName" : "bytes" },
{ "type" : "doubleSum", "name" : "cost", "fieldName" : "cost" }
],
"granularitySpec" : {
"type" : "uniform",
"segmentGranularity" : "HOUR",
"queryGranularity" : "MINUTE",
"intervals" : ["2018-01-01/2018-01-02"],
"rollup" : true
}
},
"ioConfig" : {
"type" : "index_parallel",
"inputSource" : {
"type" : "local",
"baseDir" : "quickstart/",
"filter" : "ingestion-tutorial-data.json"
},
"inputFormat" : {
"type" : "json"
}
},
"tuningConfig" : {
"type" : "index_parallel",
"partitionsSpec": {
"type": "dynamic",
"maxRowsPerSegment" : 5000000
}
}
}
}提交任务并查询数据
在 apache-druid-39.0.0 软件包根目录下,运行以下命令创建名为 ingestion tutorial 的数据源:
curl -X POST http://localhost:8081/druid/indexer/v1/task \
-H "Content-Type: application/json" \
-d @quickstart/ingestion-tutorial-index.json摄取完成后,我们来查询数据。
在 Web 控制台的 Query(查询)视图中打开一个新标签页,运行以下查询查看已摄取的数据:
select * from "ingestion-tutorial"返回结果如下:
__time | bytes | cost | count | dstIP | dstPort | packets | protocol | srcIP | srcPort |
|---|---|---|---|---|---|---|---|---|---|
2018-01-01T01:01:00.000Z | 6000 | 4.9 | 3 | 2.2.2.2 | 3000 | 60 | 6 | 1.1.1.1 | 2000 |
2018-01-01T01:02:00.000Z | 9000 | 18.1 | 2 | 2.2.2.2 | 7000 | 90 | 6 | 1.1.1.1 | 5000 |
2018-01-01T01:03:00.000Z | 6000 | 4.3 | 1 | 2.2.2.2 | 7000 | 60 | 6 | 1.1.1.1 | 5000 |
2018-01-01T02:33:00.000Z | 30000 | 56.9 | 2 | 8.8.8.8 | 5000 | 300 | 17 | 7.7.7.7 | 4000 |
2018-01-01T02:35:00.000Z | 30000 | 46.3 | 1 | 8.8.8.8 | 5000 | 300 | 17 | 7.7.7.7 | 4000 |