|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.druid.testing.embedded.indexing; |
| 21 | + |
| 22 | +import org.apache.druid.indexing.common.task.TaskBuilder; |
| 23 | +import org.apache.druid.java.util.common.StringUtils; |
| 24 | +import org.apache.druid.segment.IndexSpec; |
| 25 | +import org.apache.druid.segment.nested.NestedCommonFormatColumnFormatSpec; |
| 26 | +import org.apache.druid.segment.nested.ObjectStorageEncoding; |
| 27 | +import org.apache.druid.testing.embedded.EmbeddedBroker; |
| 28 | +import org.apache.druid.testing.embedded.EmbeddedClusterApis; |
| 29 | +import org.apache.druid.testing.embedded.EmbeddedCoordinator; |
| 30 | +import org.apache.druid.testing.embedded.EmbeddedDruidCluster; |
| 31 | +import org.apache.druid.testing.embedded.EmbeddedHistorical; |
| 32 | +import org.apache.druid.testing.embedded.EmbeddedIndexer; |
| 33 | +import org.apache.druid.testing.embedded.EmbeddedOverlord; |
| 34 | +import org.apache.druid.testing.embedded.EmbeddedRouter; |
| 35 | +import org.apache.druid.testing.embedded.junit5.EmbeddedClusterTestBase; |
| 36 | +import org.junit.jupiter.api.Assertions; |
| 37 | +import org.junit.jupiter.api.BeforeAll; |
| 38 | +import org.junit.jupiter.api.Test; |
| 39 | + |
| 40 | +/** |
| 41 | + * Embedded tests for nested data, ingested in different {@link NestedCommonFormatColumnFormatSpec}. |
| 42 | + */ |
| 43 | +public class NestedDataFormatsTest extends EmbeddedClusterTestBase |
| 44 | +{ |
| 45 | + private final EmbeddedBroker broker = new EmbeddedBroker(); |
| 46 | + private final EmbeddedOverlord overlord = new EmbeddedOverlord(); |
| 47 | + private final EmbeddedCoordinator coordinator = new EmbeddedCoordinator(); |
| 48 | + |
| 49 | + private final String datasourceWithDefaultFormat = EmbeddedClusterApis.createTestDatasourceName(); |
| 50 | + |
| 51 | + @Override |
| 52 | + protected EmbeddedDruidCluster createCluster() |
| 53 | + { |
| 54 | + return EmbeddedDruidCluster.withEmbeddedDerbyAndZookeeper() |
| 55 | + .useLatchableEmitter() |
| 56 | + .useDefaultTimeoutForLatchableEmitter(120) |
| 57 | + .addServer(overlord) |
| 58 | + .addServer(coordinator) |
| 59 | + .addServer(new EmbeddedIndexer()) |
| 60 | + .addServer(new EmbeddedHistorical()) |
| 61 | + .addServer(broker) |
| 62 | + .addServer(new EmbeddedRouter()); |
| 63 | + } |
| 64 | + |
| 65 | + @BeforeAll |
| 66 | + protected void ingestWithDefaultFormat() |
| 67 | + { |
| 68 | + final TaskBuilder.IndexParallel indexTask = |
| 69 | + TaskBuilder.ofTypeIndexParallel() |
| 70 | + .dataSource(datasourceWithDefaultFormat) |
| 71 | + .timestampColumn("timestamp") |
| 72 | + .jsonInputFormat() |
| 73 | + .inputSource(Resources.HttpData.kttmNested1Day()) |
| 74 | + .schemaDiscovery(); |
| 75 | + |
| 76 | + final String taskId = EmbeddedClusterApis.newTaskId(datasourceWithDefaultFormat); |
| 77 | + cluster.callApi().runTask(indexTask.withId(taskId), overlord); |
| 78 | + cluster.callApi().waitForAllSegmentsToBeAvailable(datasourceWithDefaultFormat, coordinator, broker); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void test_objectStorageEncoding() |
| 83 | + { |
| 84 | + // Ingest kttm data with skipping smile raw json format, comparing diff with defaultFormat |
| 85 | + NestedCommonFormatColumnFormatSpec spec = |
| 86 | + NestedCommonFormatColumnFormatSpec.builder().setObjectStorageEncoding(ObjectStorageEncoding.NONE).build(); |
| 87 | + final TaskBuilder.IndexParallel indexTask = |
| 88 | + TaskBuilder.ofTypeIndexParallel() |
| 89 | + .dataSource(dataSource) |
| 90 | + .timestampColumn("timestamp") |
| 91 | + .jsonInputFormat() |
| 92 | + .inputSource(Resources.HttpData.kttmNested1Day()) |
| 93 | + .schemaDiscovery() |
| 94 | + .tuningConfig(t -> t.withIndexSpec(IndexSpec.builder().withAutoColumnFormatSpec(spec).build())); |
| 95 | + final String taskId = EmbeddedClusterApis.newTaskId(dataSource); |
| 96 | + cluster.callApi().runTask(indexTask.withId(taskId), overlord); |
| 97 | + cluster.callApi().waitForAllSegmentsToBeAvailable(dataSource, coordinator, broker); |
| 98 | + |
| 99 | + // Test ingesting with skipping raw json smile format works, same row count, with ~20% storage saving |
| 100 | + final String metadataSql = "select sum(num_rows), sum(size) from sys.segments where datasource = '%s'"; |
| 101 | + final String defaultFormatResult = cluster.runSql(metadataSql, datasourceWithDefaultFormat); |
| 102 | + final String noneObjectStorageFormatResult = cluster.runSql(metadataSql, dataSource); |
| 103 | + Assertions.assertEquals(StringUtils.format("%d,%d", 465_346, 53_000_804), defaultFormatResult); |
| 104 | + Assertions.assertEquals(StringUtils.format("%d,%d", 465_346, 41_938_750), noneObjectStorageFormatResult); |
| 105 | + |
| 106 | + // Test querying on a nested field works |
| 107 | + final String groupByQuery = |
| 108 | + """ |
| 109 | + select json_value(event, '$.type') as event_type, count(*) as total from %s |
| 110 | + group by 1 order by 2 desc, 1 asc limit 10 |
| 111 | + """; |
| 112 | + final String queryResultDefaultFormat = cluster.runSql(groupByQuery, datasourceWithDefaultFormat); |
| 113 | + final String queryResultNoneObjectStorage = cluster.runSql(groupByQuery, dataSource); |
| 114 | + Assertions.assertEquals(queryResultDefaultFormat, queryResultNoneObjectStorage); |
| 115 | + |
| 116 | + // Test reconstruct json column works, the ordering of the fields has changed, but all values are perserved. |
| 117 | + final String scanQuery = |
| 118 | + """ |
| 119 | + select event, to_json_string(agent) as agent from %s |
| 120 | + where json_value(event, '$.type') = 'PercentClear' and json_value(agent, '$.os') = 'Android' |
| 121 | + order by __time asc limit 1 |
| 122 | + """; |
| 123 | + final String scanQueryResultDefaultFormat = cluster.runSql(scanQuery, datasourceWithDefaultFormat); |
| 124 | + final String scanQueryResultNoneObjectStorage = cluster.runSql(scanQuery, dataSource); |
| 125 | + // CHECKSTYLE: text blocks not supported in current Checkstyle version |
| 126 | + Assertions.assertEquals( |
| 127 | + """ |
| 128 | + "{""type"":""PercentClear"",""percentage"":85}","{""type"":""Mobile Browser"",""category"":""Smartphone"",""browser"":""Chrome Mobile"",""browser_version"":""50.0.2661.89"",""os"":""Android"",""platform"":""Android""}" |
| 129 | + """.trim(), |
| 130 | + scanQueryResultDefaultFormat |
| 131 | + ); |
| 132 | + Assertions.assertEquals( |
| 133 | + """ |
| 134 | + "{""percentage"":85,""type"":""PercentClear""}","{""browser"":""Chrome Mobile"",""browser_version"":""50.0.2661.89"",""category"":""Smartphone"",""os"":""Android"",""platform"":""Android"",""type"":""Mobile Browser""}" |
| 135 | + """.trim(), |
| 136 | + scanQueryResultNoneObjectStorage |
| 137 | + ); |
| 138 | + } |
| 139 | +} |
0 commit comments