Search in sources :

Example 1 with LocatedSegmentDescriptor

use of org.apache.druid.query.LocatedSegmentDescriptor in project hive by apache.

the class DruidQueryBasedInputFormat method distributeScanQuery.

/* New method that distributes the Scan query by creating splits containing
   * information about different Druid nodes that have the data for the given
   * query. */
private static HiveDruidSplit[] distributeScanQuery(String address, ScanQuery query, Path dummyPath) throws IOException {
    // If it has a limit, we use it and we do not distribute the query
    final boolean isFetch = query.getScanRowsLimit() < Long.MAX_VALUE;
    if (isFetch) {
        return new HiveDruidSplit[] { new HiveDruidSplit(DruidStorageHandlerUtils.JSON_MAPPER.writeValueAsString(query), dummyPath, new String[] { address }) };
    }
    final List<LocatedSegmentDescriptor> segmentDescriptors = fetchLocatedSegmentDescriptors(address, query);
    // Create one input split for each segment
    final int numSplits = segmentDescriptors.size();
    final HiveDruidSplit[] splits = new HiveDruidSplit[segmentDescriptors.size()];
    for (int i = 0; i < numSplits; i++) {
        final LocatedSegmentDescriptor locatedSD = segmentDescriptors.get(i);
        final String[] hosts = new String[locatedSD.getLocations().size() + 1];
        for (int j = 0; j < locatedSD.getLocations().size(); j++) {
            hosts[j] = locatedSD.getLocations().get(j).getHost();
        }
        // Default to broker if all other hosts fail.
        hosts[locatedSD.getLocations().size()] = address;
        // Create partial Select query
        final SegmentDescriptor newSD = new SegmentDescriptor(locatedSD.getInterval(), locatedSD.getVersion(), locatedSD.getPartitionNumber());
        final Query partialQuery = query.withQuerySegmentSpec(new MultipleSpecificSegmentSpec(Lists.newArrayList(newSD)));
        splits[i] = new HiveDruidSplit(DruidStorageHandlerUtils.JSON_MAPPER.writeValueAsString(partialQuery), dummyPath, hosts);
    }
    return splits;
}
Also used : MultipleSpecificSegmentSpec(org.apache.druid.query.spec.MultipleSpecificSegmentSpec) LocatedSegmentDescriptor(org.apache.druid.query.LocatedSegmentDescriptor) BaseQuery(org.apache.druid.query.BaseQuery) ScanQuery(org.apache.druid.query.scan.ScanQuery) Query(org.apache.druid.query.Query) LocatedSegmentDescriptor(org.apache.druid.query.LocatedSegmentDescriptor) SegmentDescriptor(org.apache.druid.query.SegmentDescriptor)

Example 2 with LocatedSegmentDescriptor

use of org.apache.druid.query.LocatedSegmentDescriptor in project hive by apache.

the class DruidQueryBasedInputFormat method fetchLocatedSegmentDescriptors.

private static List<LocatedSegmentDescriptor> fetchLocatedSegmentDescriptors(String address, BaseQuery query) throws IOException {
    // Comma-separated intervals without brackets
    final String intervals = StringUtils.join(query.getIntervals(), ",");
    final String request = String.format("http://%s/druid/v2/datasources/%s/candidates?intervals=%s", address, query.getDataSource().getNames().get(0), URLEncoder.encode(intervals, "UTF-8"));
    LOG.debug("sending request {} to query for segments", request);
    final InputStream response;
    try {
        response = DruidStorageHandlerUtils.submitRequest(DruidStorageHandler.getHttpClient(), new Request(HttpMethod.GET, new URL(request)));
    } catch (Exception e) {
        throw new IOException(org.apache.hadoop.util.StringUtils.stringifyException(e));
    }
    // Retrieve results
    final List<LocatedSegmentDescriptor> segmentDescriptors;
    try {
        segmentDescriptors = DruidStorageHandlerUtils.JSON_MAPPER.readValue(response, new TypeReference<List<LocatedSegmentDescriptor>>() {
        });
    } catch (Exception e) {
        response.close();
        throw new IOException(org.apache.hadoop.util.StringUtils.stringifyException(e));
    }
    return segmentDescriptors;
}
Also used : LocatedSegmentDescriptor(org.apache.druid.query.LocatedSegmentDescriptor) InputStream(java.io.InputStream) Request(org.apache.druid.java.util.http.client.Request) IOException(java.io.IOException) TypeReference(com.fasterxml.jackson.core.type.TypeReference) URL(java.net.URL) IOException(java.io.IOException)

Example 3 with LocatedSegmentDescriptor

use of org.apache.druid.query.LocatedSegmentDescriptor in project druid by druid-io.

the class ServerViewUtil method getTargetLocations.

public static List<LocatedSegmentDescriptor> getTargetLocations(TimelineServerView serverView, DataSource datasource, List<Interval> intervals, int numCandidates) {
    final DataSourceAnalysis analysis = DataSourceAnalysis.forDataSource(datasource);
    final Optional<? extends TimelineLookup<String, ServerSelector>> maybeTimeline = serverView.getTimeline(analysis);
    if (!maybeTimeline.isPresent()) {
        return Collections.emptyList();
    }
    List<LocatedSegmentDescriptor> located = new ArrayList<>();
    for (Interval interval : intervals) {
        for (TimelineObjectHolder<String, ServerSelector> holder : maybeTimeline.get().lookup(interval)) {
            for (PartitionChunk<ServerSelector> chunk : holder.getObject()) {
                ServerSelector selector = chunk.getObject();
                final SegmentDescriptor descriptor = new SegmentDescriptor(holder.getInterval(), holder.getVersion(), chunk.getChunkNumber());
                long size = selector.getSegment().getSize();
                List<DruidServerMetadata> candidates = selector.getCandidates(numCandidates);
                located.add(new LocatedSegmentDescriptor(descriptor, size, candidates));
            }
        }
    }
    return located;
}
Also used : ArrayList(java.util.ArrayList) DataSourceAnalysis(org.apache.druid.query.planning.DataSourceAnalysis) DruidServerMetadata(org.apache.druid.server.coordination.DruidServerMetadata) ServerSelector(org.apache.druid.client.selector.ServerSelector) LocatedSegmentDescriptor(org.apache.druid.query.LocatedSegmentDescriptor) LocatedSegmentDescriptor(org.apache.druid.query.LocatedSegmentDescriptor) SegmentDescriptor(org.apache.druid.query.SegmentDescriptor) Interval(org.joda.time.Interval)

Aggregations

LocatedSegmentDescriptor (org.apache.druid.query.LocatedSegmentDescriptor)3 SegmentDescriptor (org.apache.druid.query.SegmentDescriptor)2 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 ServerSelector (org.apache.druid.client.selector.ServerSelector)1 Request (org.apache.druid.java.util.http.client.Request)1 BaseQuery (org.apache.druid.query.BaseQuery)1 Query (org.apache.druid.query.Query)1 DataSourceAnalysis (org.apache.druid.query.planning.DataSourceAnalysis)1 ScanQuery (org.apache.druid.query.scan.ScanQuery)1 MultipleSpecificSegmentSpec (org.apache.druid.query.spec.MultipleSpecificSegmentSpec)1 DruidServerMetadata (org.apache.druid.server.coordination.DruidServerMetadata)1 Interval (org.joda.time.Interval)1