Example usage for java.util.stream Collectors toMap

List of usage examples for java.util.stream Collectors toMap

Introduction

In this page you can find the example usage for java.util.stream Collectors toMap.

Prototype

public static <T, K, U> Collector<T, ?, Map<K, U>> toMap(Function<? super T, ? extends K> keyMapper,
        Function<? super T, ? extends U> valueMapper) 

Source Link

Document

Returns a Collector that accumulates elements into a Map whose keys and values are the result of applying the provided mapping functions to the input elements.

Usage

From source file:com.homeadvisor.kafdrop.service.CuratorKafkaMonitor.java

/**
 * Returns the map of partitionId to consumer offset for the given group and
 * topic. Uses the given blocking channel to execute the offset fetch request.
 *
 * @param channel          The channel to send requests on
 * @param groupId          Consumer group to use
 * @param topic            Topic to query
 * @param zookeeperOffsets If true, use a version of the API that retrieves
 *                         offsets from Zookeeper. Otherwise use a version
 *                         that pulls the offsets from Kafka itself.
 * @return Map where the key is partitionId and the value is the consumer
 * offset for that partition./*  ww  w .  ja v  a 2  s  .  c  o  m*/
 */
private Map<Integer, Long> getConsumerOffsets(BlockingChannel channel, String groupId, TopicVO topic,
        boolean zookeeperOffsets) {

    final OffsetFetchRequest request = new OffsetFetchRequest(groupId,
            topic.getPartitions().stream().map(p -> new TopicAndPartition(topic.getName(), p.getId()))
                    .collect(Collectors.toList()),
            (short) (zookeeperOffsets ? 0 : 1), 0, // version 0 = zookeeper offsets, 1 = kafka offsets
            clientId());

    LOG.debug("Sending consumer offset request: {}", request);

    channel.send(request.underlying());

    final kafka.api.OffsetFetchResponse underlyingResponse = kafka.api.OffsetFetchResponse
            .readFrom(channel.receive().buffer());

    LOG.debug("Received consumer offset response: {}", underlyingResponse);

    OffsetFetchResponse response = new OffsetFetchResponse(underlyingResponse);

    return response.offsets().entrySet().stream()
            .filter(entry -> entry.getValue().error() == ErrorMapping.NoError())
            .collect(Collectors.toMap(entry -> entry.getKey().partition(), entry -> entry.getValue().offset()));
}

From source file:org.graylog2.indexer.indices.Indices.java

public Map<String, Boolean> areReopened(Collection<String> indices) {
    final JestResult jestResult = JestUtils.execute(jestClient, new State.Builder().withMetadata().build(),
            () -> "Couldn't read cluster state for indices " + indices);

    final JsonObject indicesJson = getClusterStateIndicesMetadata(jestResult.getJsonObject());
    return indices.stream().collect(Collectors.toMap(Function.identity(),
            index -> checkForReopened(getIndexSettings(indicesJson, index))));
}

From source file:ddf.catalog.impl.operations.UpdateOperations.java

private UpdateRequest populateMetacards(UpdateRequest updateRequest) throws IngestException {
    QueryRequestImpl queryRequest = createQueryRequest(updateRequest);
    QueryResponse queryResponse;/*w w w.  j  a  va2 s.c  om*/
    try {
        queryResponse = queryOperations.doQuery(queryRequest, frameworkProperties.getFederationStrategy());
    } catch (FederationException e) {
        LOGGER.debug("Unable to complete query for updated metacards.", e);
        throw new IngestException("Exception during runtime while performing update");
    }

    if (!foundAllUpdateRequestMetacards(updateRequest, queryResponse)) {
        logFailedQueryInfo(updateRequest, queryResponse);
        throw new IngestException("Could not find all metacards specified in request");
    }

    updateRequest = rewriteRequestToAvoidHistoryConflicts(updateRequest, queryResponse);

    // Construct the metacardMap using the metacard's ID in order to match the UpdateRequest
    HashMap<String, Metacard> metacardMap = new HashMap<>(
            queryResponse.getResults().stream().map(Result::getMetacard).collect(Collectors
                    .toMap(metacard -> getAttributeStringValue(metacard, Core.ID), Function.identity())));
    updateRequest.getProperties().put(Constants.ATTRIBUTE_UPDATE_MAP_KEY, metacardMap);
    updateRequest.getProperties().put(Constants.OPERATION_TRANSACTION_KEY,
            new OperationTransactionImpl(OperationTransaction.OperationType.UPDATE, metacardMap.values()));
    return updateRequest;
}

From source file:com.ikanow.aleph2.analytics.spark.services.EnrichmentPipelineService.java

/** Creates a wrapper for a single enrichment job
 * @param aleph2_context - the Aleph2 analytic context
 * @param name - the name of the enrichment pipeline from the job
 * @param dummy - just to differentatiate
 *///from   w  w  w .jav  a 2 s .  c  om
@SuppressWarnings("unchecked")
protected EnrichmentPipelineService(final IAnalyticsContext aleph2_context, boolean emit_when_done,
        final String... names) {
    final Map<String, EnrichmentControlMetadataBean> pipeline_elements_map = aleph2_context.getJob()
            .map(j -> j.config()).map(cfg -> cfg.get(EnrichmentControlMetadataBean.ENRICHMENT_PIPELINE))
            .<List<Object>>map(pipe -> Patterns.match(pipe).<List<Object>>andReturn()
                    .when(List.class, l -> (List<Object>) l).otherwise(() -> (List<Object>) null))
            .<Map<String, EnrichmentControlMetadataBean>>map(l -> l.stream()
                    .<EnrichmentControlMetadataBean>map(o -> BeanTemplateUtils
                            .from((Map<String, Object>) o, EnrichmentControlMetadataBean.class).get())
                    .collect(Collectors.toMap((EnrichmentControlMetadataBean cfg) -> cfg.name(), cfg -> cfg)))
            .orElse(Collections.emptyMap());

    final List<EnrichmentControlMetadataBean> pipeline_elements = Arrays.stream(names)
            .map(n -> pipeline_elements_map.get(n)).filter(cfg -> null != cfg).collect(Collectors.toList());

    _analytics_context = aleph2_context;
    _emit_when_done = emit_when_done;
    _pipeline_elements = pipeline_elements;
    _is_streaming = aleph2_context.getJob().map(j -> j.analytic_type())
            .map(t -> MasterEnrichmentType.streaming == t).orElse(false);

    if (_pipeline_elements.isEmpty()) {
        throw new RuntimeException(ErrorUtils.get(SparkErrorUtils.NO_PIPELINE_ELEMENTS_SPECIFIED,
                aleph2_context.getBucket().map(b -> b.full_name()).orElse("(unknown bucket)"),
                aleph2_context.getJob().map(j -> j.name()).orElse("(unknown job)")));

    }

}

From source file:com.oneops.controller.cms.CmsWoProvider.java

void addComponentConfig(CmsWorkOrder wo) {
    CmsRfcCI rfc = wo.getRfcCi();// w w w . j a  va 2  s.c o  m
    if (rfc.getCiClassName().matches(BOM_FQDN) && isTorbitService(rfc)) {
        String logKey = "Fqdn workorder " + wo.getDeploymentId() + ":" + rfc.getCiId() + ":: ";
        //send weights only when enabled
        if (isWeightsEnabled(wo.getRfcCi().getNsPath())) {
            logger.info(logKey + "finding weights for clouds");
            List<CmsCIRelation> deployedToRelations = cmProcessor
                    .getCIRelationsWithToCIAndNoAttrs(rfc.getNsPath(), DEPLOYED_TO, null, "Compute", null);
            Set<String> clouds = getPrimaryClouds(wo);
            logger.info(logKey + "primary clouds " + clouds);
            Map<String, Long> cloudInstancesMap = deployedToRelations.stream()
                    .filter(r -> (r.getToCi() != null) && clouds.contains(r.getToCi().getCiName()))
                    .collect(groupingBy(r -> r.getToCi().getCiName(), counting()));

            if (cloudInstancesMap.isEmpty()) {
                logger.info(logKey + "no active cloud compute instance found");
                return;
            }

            Long totalComputes = 0L;
            List<CloudWeight> cloudWeights = new ArrayList<>();
            for (Entry<String, Long> entry : cloudInstancesMap.entrySet()) {
                totalComputes = totalComputes + entry.getValue();
                cloudWeights.add(new CloudWeight(entry.getKey(), entry.getValue()));
            }
            logger.info(logKey + "total active computes " + totalComputes);

            cloudWeights.sort((c1, c2) -> Long.compare(c2.computes, c1.computes));

            if (totalComputes > 0) {
                int totalPercent = 0;
                for (CloudWeight cloudWeight : cloudWeights) {
                    cloudWeight.weight = (int) ((cloudWeight.computes * 100.0) / totalComputes);
                    totalPercent += cloudWeight.weight;
                }
                //bring the total percent to 100
                int remainingPercent = 100 - totalPercent;
                for (int i = 0; i < remainingPercent; i++) {
                    cloudWeights.get(i).weight++;
                }

                Map<String, String> config = configMap(wo);
                Map<String, Integer> weights = cloudWeights.stream()
                        .collect(Collectors.toMap(c -> c.name, c -> c.weight));
                logger.info(logKey + "weight map " + weights);
                config.put("weights", gson.toJson(weights));
            }
        } else {
            logger.info(logKey + "not using weights for gslb. gslb weights not enabled.");
        }
    }
}

From source file:delfos.rs.trustbased.WeightedGraph.java

public Map<Node, Map<Node, Double>> getCompleteMapOfConnections() {
    Map<Node, Map<Node, Double>> edgesOfSubGraph = allNodes().parallelStream()
            .collect(Collectors.toMap(node1 -> node1, node1 -> {
                Map<Node, Double> edgesFromThisVertex = allNodes().parallelStream()
                        .collect(Collectors.toMap(node2 -> node2, node2 -> {
                            return this.connectionWeight(node1, node2).orElse(0.0);
                        }));/*  w ww .j av a 2 s  . c  om*/

                return edgesFromThisVertex;
            }));
    return edgesOfSubGraph;
}

From source file:com.microfocus.application.automation.tools.octane.CIJenkinsServicesImpl.java

private List<ParameterValue> createParameters(Job project, CIParameters ciParameters) {
    List<ParameterValue> result = new ArrayList<>();
    boolean parameterHandled;
    ParameterValue tmpValue;//w w w .j  a  v a  2  s  .  co m
    ParametersDefinitionProperty paramsDefProperty = (ParametersDefinitionProperty) project
            .getProperty(ParametersDefinitionProperty.class);
    if (paramsDefProperty != null) {
        Map<String, CIParameter> ciParametersMap = ciParameters.getParameters().stream()
                .collect(Collectors.toMap(CIParameter::getName, Function.identity()));
        for (ParameterDefinition paramDef : paramsDefProperty.getParameterDefinitions()) {
            parameterHandled = false;
            CIParameter ciParameter = ciParametersMap.remove(paramDef.getName());
            if (ciParameter != null) {
                tmpValue = null;
                switch (ciParameter.getType()) {
                case FILE:
                    try {
                        FileItemFactory fif = new DiskFileItemFactory();
                        FileItem fi = fif.createItem(ciParameter.getName(), "text/plain", false,
                                UUID.randomUUID().toString());
                        fi.getOutputStream()
                                .write(DatatypeConverter.parseBase64Binary(ciParameter.getValue().toString()));
                        tmpValue = new FileParameterValue(ciParameter.getName(), fi);
                    } catch (IOException ioe) {
                        logger.warn("failed to process file parameter", ioe);
                    }
                    break;
                case NUMBER:
                    tmpValue = new StringParameterValue(ciParameter.getName(),
                            ciParameter.getValue().toString());
                    break;
                case STRING:
                    tmpValue = new StringParameterValue(ciParameter.getName(),
                            ciParameter.getValue().toString());
                    break;
                case BOOLEAN:
                    tmpValue = new BooleanParameterValue(ciParameter.getName(),
                            Boolean.parseBoolean(ciParameter.getValue().toString()));
                    break;
                case PASSWORD:
                    tmpValue = new PasswordParameterValue(ciParameter.getName(),
                            ciParameter.getValue().toString());
                    break;
                default:
                    break;
                }
                if (tmpValue != null) {
                    result.add(tmpValue);
                    parameterHandled = true;
                }
            }
            if (!parameterHandled) {
                if (paramDef instanceof FileParameterDefinition) {
                    FileItemFactory fif = new DiskFileItemFactory();
                    FileItem fi = fif.createItem(paramDef.getName(), "text/plain", false, "");
                    try {
                        fi.getOutputStream().write(new byte[0]);
                    } catch (IOException ioe) {
                        logger.error("failed to create default value for file parameter '" + paramDef.getName()
                                + "'", ioe);
                    }
                    tmpValue = new FileParameterValue(paramDef.getName(), fi);
                    result.add(tmpValue);
                } else {
                    result.add(paramDef.getDefaultParameterValue());
                }
            }
        }

        //add parameters that are not defined in job
        for (CIParameter notDefinedParameter : ciParametersMap.values()) {
            tmpValue = new StringParameterValue(notDefinedParameter.getName(),
                    notDefinedParameter.getValue().toString());
            result.add(tmpValue);
        }
    }
    return result;
}

From source file:net.dv8tion.jda.core.entities.EntityBuilder.java

private Map<String, AuditLogChange> changeToMap(Set<AuditLogChange> changesList) {
    return changesList.stream().collect(Collectors.toMap(AuditLogChange::getKey, UnaryOperator.identity()));
}