|
| 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, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.drill.exec.store.druid; |
| 20 | + |
| 21 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 22 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 23 | +import com.fasterxml.jackson.databind.node.ObjectNode; |
| 24 | +import org.apache.drill.common.exceptions.CustomErrorContext; |
| 25 | +import org.apache.drill.common.exceptions.UserException; |
| 26 | +import org.apache.drill.common.expression.SchemaPath; |
| 27 | +import org.apache.drill.exec.ops.FragmentContext; |
| 28 | +import org.apache.drill.exec.physical.impl.scan.framework.ManagedReader; |
| 29 | +import org.apache.drill.exec.physical.impl.scan.framework.SchemaNegotiator; |
| 30 | +import org.apache.drill.exec.physical.resultSet.ResultSetLoader; |
| 31 | +import org.apache.drill.exec.record.metadata.TupleMetadata; |
| 32 | +import org.apache.drill.exec.store.druid.DruidSubScan.DruidSubScanSpec; |
| 33 | +import org.apache.drill.exec.store.druid.common.DruidFilter; |
| 34 | +import org.apache.drill.exec.store.druid.druid.DruidScanResponse; |
| 35 | +import org.apache.drill.exec.store.druid.druid.ScanQuery; |
| 36 | +import org.apache.drill.exec.store.druid.druid.ScanQueryBuilder; |
| 37 | +import org.apache.drill.exec.store.druid.rest.DruidQueryClient; |
| 38 | +import org.apache.drill.exec.store.easy.json.loader.JsonLoader; |
| 39 | +import org.apache.drill.exec.store.easy.json.loader.JsonLoaderImpl.JsonLoaderBuilder; |
| 40 | +import org.apache.drill.exec.vector.BaseValueVector; |
| 41 | +import org.slf4j.Logger; |
| 42 | +import org.slf4j.LoggerFactory; |
| 43 | + |
| 44 | +import java.math.BigInteger; |
| 45 | +import java.util.ArrayList; |
| 46 | +import java.util.List; |
| 47 | + |
| 48 | +public class DruidBatchRecordReader implements ManagedReader<SchemaNegotiator> { |
| 49 | + private static final Logger logger = LoggerFactory.getLogger(DruidBatchRecordReader.class); |
| 50 | + |
| 51 | + private static final ObjectMapper objectMapper = new ObjectMapper(); |
| 52 | + |
| 53 | + private final DruidStoragePlugin plugin; |
| 54 | + private final DruidSubScan.DruidSubScanSpec scanSpec; |
| 55 | + private final List<String> columns; |
| 56 | + private final DruidFilter filter; |
| 57 | + private final DruidQueryClient druidQueryClient; |
| 58 | + private final FragmentContext fragmentContext; |
| 59 | + |
| 60 | + private final TupleMetadata schema; |
| 61 | + private BigInteger nextOffset = BigInteger.ZERO; |
| 62 | + private int maxRecordsToRead = -1; |
| 63 | + |
| 64 | + private JsonLoaderBuilder jsonBuilder; |
| 65 | + |
| 66 | + private JsonLoader jsonLoader; |
| 67 | + private ResultSetLoader resultSetLoader; |
| 68 | + |
| 69 | + private CustomErrorContext errorContext; |
| 70 | + |
| 71 | + |
| 72 | + public DruidBatchRecordReader(DruidSubScanSpec subScanSpec, |
| 73 | + List<SchemaPath> projectedColumns, |
| 74 | + int maxRecordsToRead, |
| 75 | + FragmentContext context, |
| 76 | + DruidStoragePlugin plugin) { |
| 77 | + columns = new ArrayList<>(); |
| 78 | + setColumns(projectedColumns); |
| 79 | + this.maxRecordsToRead = maxRecordsToRead; |
| 80 | + this.plugin = plugin; |
| 81 | + scanSpec = subScanSpec; |
| 82 | + this.schema = scanSpec.getSchema(); |
| 83 | + fragmentContext = context; |
| 84 | + this.filter = subScanSpec.getFilter(); |
| 85 | + this.druidQueryClient = plugin.getDruidQueryClient(); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public boolean open(SchemaNegotiator negotiator) { |
| 90 | + resultSetLoader = negotiator.build(); |
| 91 | + errorContext = negotiator.parentErrorContext(); |
| 92 | + |
| 93 | + negotiator |
| 94 | + jsonBuilder = new JsonLoaderBuilder() |
| 95 | + .resultSetLoader(resultSetLoader) |
| 96 | + .errorContext(errorContext); |
| 97 | + |
| 98 | + |
| 99 | + |
| 100 | + return true; |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public boolean next() { |
| 105 | + boolean result = false; |
| 106 | + try { |
| 107 | + String query = getQuery(); |
| 108 | + DruidScanResponse druidScanResponse = druidQueryClient.executeQuery(query); |
| 109 | + setNextOffset(druidScanResponse); |
| 110 | + |
| 111 | + for (ObjectNode eventNode : druidScanResponse.getEvents()) { |
| 112 | + jsonLoader = jsonBuilder |
| 113 | + .fromString(eventNode.asText()) |
| 114 | + .build(); |
| 115 | + result = jsonLoader.readBatch(); |
| 116 | + } |
| 117 | + return result; |
| 118 | + } catch (Exception e) { |
| 119 | + throw UserException |
| 120 | + .dataReadError(e) |
| 121 | + .message("Failure while executing druid query: " + e.getMessage()) |
| 122 | + .addContext(errorContext) |
| 123 | + .build(logger); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public void close() { |
| 129 | + if (jsonLoader != null) { |
| 130 | + jsonLoader.close(); |
| 131 | + jsonLoader = null; |
| 132 | + } |
| 133 | + |
| 134 | + if (! nextOffset.equals(BigInteger.ZERO)) { |
| 135 | + nextOffset = BigInteger.ZERO; |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + private String getQuery() throws JsonProcessingException { |
| 140 | + int queryThreshold = |
| 141 | + maxRecordsToRead >= 0 |
| 142 | + ? Math.min(BaseValueVector.INITIAL_VALUE_ALLOCATION, maxRecordsToRead) |
| 143 | + : BaseValueVector.INITIAL_VALUE_ALLOCATION; |
| 144 | + ScanQueryBuilder scanQueryBuilder = plugin.getScanQueryBuilder(); |
| 145 | + ScanQuery scanQuery = |
| 146 | + scanQueryBuilder.build( |
| 147 | + scanSpec.dataSourceName, |
| 148 | + columns, |
| 149 | + filter, |
| 150 | + nextOffset, |
| 151 | + queryThreshold, |
| 152 | + scanSpec.getMinTime(), |
| 153 | + scanSpec.getMaxTime() |
| 154 | + ); |
| 155 | + return objectMapper.writeValueAsString(scanQuery); |
| 156 | + } |
| 157 | + |
| 158 | + private void setNextOffset(DruidScanResponse druidScanResponse) { |
| 159 | + nextOffset = nextOffset.add(BigInteger.valueOf(druidScanResponse.getEvents().size())); |
| 160 | + } |
| 161 | +} |
0 commit comments