List of usage examples for java.util.stream Collectors toMap
public static <T, K, U> Collector<T, ?, Map<K, U>> toMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper)
From source file:com.thinkbiganalytics.ingest.TableRegisterSupportTest.java
@Test public void testRemovingColumns() { ColumnSpec[] feedSpecs = ColumnSpec/* w ww .j av a2s.c o m*/ .createFromString("id|string|my comment|0|0|0|id\n" + "name|string||0|0|0|name\n" + "company|string|some description|0|0|0|change_company\n" + "zip|string||0|0|0|zip_code\n" + "phone|string\n" + "email|string\n" + "country|string\n" + "hired|string"); ColumnSpec[] targetSpecs = ColumnSpec.createFromString("id|bigint|my comment|0|0|0|id\n" + "name|string||0|0|0|name\n" + "change_company|string|some description|0|0|0|company\n" + "zip_code|string||0|0|0|zip\n" + "email|string\n" + "hired|date||0|0|0|hired"); ColumnSpec[] parts = ColumnSpec.createFromString("year|int\ncountry|string"); TableRegisterConfiguration conf = new TableRegisterConfiguration(); TableRegisterSupport support = new TableRegisterSupport(connection, conf); ColumnSpec[] invalidColumnSpecs = support.adjustInvalidColumnSpec(feedSpecs, targetSpecs); assertEquals(targetSpecs.length, invalidColumnSpecs.length); Map<String, ColumnSpec> feedColumnSpecMap = Arrays.asList(feedSpecs).stream() .collect(Collectors.toMap(ColumnSpec::getName, Function.identity())); for (ColumnSpec invalid : invalidColumnSpecs) { if (StringUtils.isNotBlank(invalid.getOtherColumnName())) { assertEquals(invalid.getDataType(), feedColumnSpecMap.get(invalid.getOtherColumnName()).getDataType()); } } TableType[] tableTypes = new TableType[] { TableType.FEED, TableType.INVALID, TableType.VALID, TableType.MASTER }; for (TableType tableType : tableTypes) { ColumnSpec[] useColumnSpecs = targetSpecs; if (tableType == TableType.INVALID) { useColumnSpecs = invalidColumnSpecs; } else if (tableType == TableType.FEED) { useColumnSpecs = feedSpecs; } String ddl = support.createDDL("source_table", "target_table", useColumnSpecs, parts, "ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.OpenCSVSerde'", "stored as orc", "tblproperties (\"orc.compress\"=\"SNAPPY\")", tableType); int i = 0; } }
From source file:com.netflix.spinnaker.front50.model.S3StorageService.java
@Override public Map<String, Long> listObjectKeys(ObjectType objectType) { long startTime = System.currentTimeMillis(); ObjectListing bucketListing = amazonS3.listObjects( new ListObjectsRequest(bucket, buildTypedFolder(rootFolder, objectType.group), null, null, 10000)); List<S3ObjectSummary> summaries = bucketListing.getObjectSummaries(); while (bucketListing.isTruncated()) { bucketListing = amazonS3.listNextBatchOfObjects(bucketListing); summaries.addAll(bucketListing.getObjectSummaries()); }//from w w w . jav a2 s. co m log.debug("Took {}ms to fetch {} object keys for {}", value("fetchTime", (System.currentTimeMillis() - startTime)), summaries.size(), value("type", objectType)); return summaries.stream().filter(s -> filterS3ObjectSummary(s, objectType.defaultMetadataFilename)) .collect(Collectors.toMap((s -> buildObjectKey(objectType, s.getKey())), (s -> s.getLastModified().getTime()))); }
From source file:hydrograph.ui.engine.ui.converter.impl.OutputFileExcelUiConverter.java
private ExcelFormattingDataStructure getExcelFormat() { ExcelFile excelFile = (ExcelFile) typeBaseComponent; CellFormat cellFormat = excelFile.getCellFormat(); FieldFormat data = cellFormat.getData(); FieldFormat header = cellFormat.getHeader(); String dataCopyOfField;/*from w ww . j a va 2 s. co m*/ ExcelFormattingDataStructure excelFormattingDataStructure = new ExcelFormattingDataStructure(); List<String> dataCopyFieldList = data.getField().stream().filter( x -> (null != x.getCopyOfFiled() && StringUtils.isNotEmpty(x.getCopyOfFiled().getFieldName()))) .map(m -> m.getName()).collect(Collectors.toList()); if (dataCopyFieldList != null && dataCopyFieldList.size() > 0) { dataCopyOfField = data.getField().stream().filter( x -> (null != x.getCopyOfFiled() && StringUtils.isNotEmpty(x.getCopyOfFiled().getFieldName()))) .collect(Collectors.toList()).get(0).getCopyOfFiled().getFieldName(); } else { dataCopyOfField = "Select"; } List<ExcelConfigurationDataStructure> listEcds = data.getField().stream() .filter(x -> (null != x.getProperty() && x.getProperty().size() > 0)).map(m -> { ExcelConfigurationDataStructure ecds = new ExcelConfigurationDataStructure(); ecds.setFieldName(m.getName()); Map<String, String> fieldDatapropMap = m.getProperty().stream().collect(Collectors .toMap(FieldFormat.Field.Property::getName, FieldFormat.Field.Property::getValue)); ecds.setDataMap(fieldDatapropMap); Map<String, String> fieldHeaderPropMap = header.getField().stream() .filter(hf -> (StringUtils.equalsIgnoreCase(hf.getName(), m.getName()))).findFirst() .get().getProperty().stream().collect(Collectors.toMap( FieldFormat.Field.Property::getName, FieldFormat.Field.Property::getValue)); ecds.setHeaderMap(fieldHeaderPropMap); return ecds; }).collect(Collectors.toList()); excelFormattingDataStructure.setCopyFieldList(dataCopyFieldList); excelFormattingDataStructure.setCopyOfField(dataCopyOfField); excelFormattingDataStructure.setListOfExcelConfiguration(listEcds); return excelFormattingDataStructure; }
From source file:de.whs.poodle.controllers.instructor.FeedbackOverviewController.java
private List<FeedbackOverviewRowData> createRows(List<Statistic> courseTermStatistics) { // map from the studentId to all of the student's statistics Map<Student, List<Statistic>> studentToStatisticsMap = courseTermStatistics.stream() .collect(Collectors.groupingBy(Statistic::getStudent)); // create the rows from the studentToStatisticsMap List<FeedbackOverviewRowData> rows = new ArrayList<>(); for (Map.Entry<Student, List<Statistic>> e : studentToStatisticsMap.entrySet()) { Student student = e.getKey();/* www. j av a 2 s. co m*/ List<Statistic> studentStatistics = e.getValue(); /* studentStatistics is now the list of all statistics that the student * has created in this courseTerm. We now transform this list into a * map which maps each exercise to its statistic (a student can only * ever create one statistic for each exercise). */ Map<Integer, Statistic> rootIdToStatisticMap = studentStatistics.stream() .collect(Collectors.toMap(Statistic::getExerciseRootId, Function.identity())); // create the row rows.add(new FeedbackOverviewRowData(student, rootIdToStatisticMap)); } return rows; }
From source file:io.pravega.controller.server.ControllerService.java
public CompletableFuture<Map<SegmentId, Long>> getSegmentsAtTime(final String scope, final String stream, final long timestamp) { Exceptions.checkNotNullOrEmpty(scope, "scope"); Exceptions.checkNotNullOrEmpty(stream, "stream"); // First fetch segments active at specified timestamp from the specified stream. // Divide current segments in segmentFutures into at most count positions. return streamStore.getActiveSegments(scope, stream, timestamp, null, executor).thenApply(segments -> { return segments.stream().map(number -> ModelHelper.createSegmentId(scope, stream, number)) .collect(Collectors.toMap(id -> id, id -> 0L)); //TODO: Implement https://github.com/pravega/pravega/issues/191 (Which will supply a value besides 0) });/*from w w w .j a v a 2 s .c om*/ }
From source file:com.yahoo.parsec.filters.RequestResponeLoggingFilterTest.java
private String createLogStringPatternForError(String requestMethod, String url, String queryString, Map<String, Collection<String>> reqHeaders, String reqBodyJson, Object error) throws JsonProcessingException { Map<String, String> headers = reqHeaders.entrySet().stream() .collect(Collectors.toMap(Map.Entry::getKey, e -> String.join(",", e.getValue()))); String reqHeaderString = _OBJECT_MAPPER.writeValueAsString(headers); String pattern = String.format( "{" + "\"time\":\"${json-unit.any-number}\"," + "\"request\": {" + "\"method\": \"%s\"," + "\"uri\": \"%s\"," + "\"query\": \"%s\"," + "\"headers\": %s," + "\"payload\": \"%s\"" + "}," + "\"response\": {}," + "\"response-error\": \"${json-unit.ignore}\", " + "\"progress\": {" + "\"namelookup_time\": \"${json-unit.any-number}\"," + "\"connect_time\": \"${json-unit.any-number}\"," + "\"pretransfer_time\": \"${json-unit.any-number}\"," + "\"starttransfer_time\": \"${json-unit.any-number}\"," + "\"total_time\":\"${json-unit.any-number}\"" + "}" + "}", requestMethod, url, queryString, reqHeaderString, StringEscapeUtils.escapeJson(reqBodyJson)); return pattern; }
From source file:org.wallride.repository.ArticleRepositoryImpl.java
private FullTextQuery buildFullTextQuery(ArticleSearchRequest request, Pageable pageable, Criteria criteria) { FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager); QueryBuilder qb = fullTextEntityManager.getSearchFactory().buildQueryBuilder().forEntity(Article.class) .get();//from w w w . j ava2 s. c o m @SuppressWarnings("rawtypes") BooleanJunction<BooleanJunction> junction = qb.bool(); junction.must(qb.all().createQuery()); junction.must(qb.keyword().onField("drafted").ignoreAnalyzer().matching("_null_").createQuery()); if (StringUtils.hasText(request.getKeyword())) { Analyzer analyzer = fullTextEntityManager.getSearchFactory().getAnalyzer("synonyms"); String[] fields = new String[] { "title", "body", "categories.name", "tags.name", "customFieldValues.value" }; MultiFieldQueryParser parser = new MultiFieldQueryParser(fields, analyzer); parser.setDefaultOperator(QueryParser.Operator.AND); Query query = null; try { query = parser.parse(request.getKeyword()); } catch (ParseException e1) { try { query = parser.parse(QueryParser.escape(request.getKeyword())); } catch (ParseException e2) { throw new RuntimeException(e2); } } junction.must(query); } if (request.getStatus() != null) { junction.must(qb.keyword().onField("status").matching(request.getStatus()).createQuery()); } if (StringUtils.hasText(request.getLanguage())) { junction.must(qb.keyword().onField("language").matching(request.getLanguage()).createQuery()); } if (request.getDateFrom() != null) { junction.must(qb.range().onField("date").above(request.getDateFrom()).createQuery()); } if (request.getDateTo() != null) { junction.must(qb.range().onField("date").below(request.getDateTo()).createQuery()); } if (!CollectionUtils.isEmpty(request.getCategoryIds())) { BooleanJunction<BooleanJunction> subJunction = qb.bool(); for (long categoryId : request.getCategoryIds()) { subJunction.should(qb.keyword().onField("categories.id").matching(categoryId).createQuery()); } junction.must(subJunction.createQuery()); } if (!CollectionUtils.isEmpty(request.getCategoryCodes())) { BooleanJunction<BooleanJunction> subJunction = qb.bool(); for (String categoryCode : request.getCategoryCodes()) { subJunction.should(qb.keyword().onField("categories.code").matching(categoryCode).createQuery()); } junction.must(subJunction.createQuery()); } if (!CollectionUtils.isEmpty(request.getTagIds())) { BooleanJunction<BooleanJunction> subJunction = qb.bool(); for (long tagId : request.getTagIds()) { subJunction.should(qb.keyword().onField("tags.id").matching(tagId).createQuery()); } junction.must(subJunction.createQuery()); } if (!CollectionUtils.isEmpty(request.getTagNames())) { BooleanJunction<BooleanJunction> subJunction = qb.bool(); for (String tagName : request.getTagNames()) { subJunction.should(qb.phrase().onField("tags.name").sentence(tagName).createQuery()); } junction.must(subJunction.createQuery()); } if (!CollectionUtils.isEmpty(request.getCustomFields())) { javax.persistence.Query query = entityManager.createQuery( "from CustomField where language = :language and code in (:codes)", CustomField.class); query.setParameter("language", request.getLanguage()).setParameter("codes", request.getCustomFields().keySet()); List<CustomField> customFields = query.getResultList(); if (!CollectionUtils.isEmpty(customFields)) { Map<String, CustomField> customFieldMap = customFields.stream() .collect(Collectors.toMap(CustomField::getCode, Function.identity())); BooleanJunction<BooleanJunction> subJunction = qb.bool(); for (String key : request.getCustomFields().keySet()) { List<Object> values = (List<Object>) request.getCustomFields().get(key); CustomField target = customFieldMap.get(key); BooleanJunction<BooleanJunction> customFieldJunction = qb.bool(); switch (target.getFieldType()) { case TEXT: case TEXTAREA: case HTML: for (Object value : values) { customFieldJunction.must(qb.keyword().onField("customFieldValues." + key) .ignoreFieldBridge().matching(value.toString()).createQuery()); } break; default: for (Object value : values) { customFieldJunction.must(qb.phrase().onField("customFieldValues." + key) .ignoreFieldBridge().sentence(value.toString()).createQuery()); } } subJunction.must(customFieldJunction.createQuery()); } junction.must(subJunction.createQuery()); } } if (request.getAuthorId() != null) { junction.must(qb.keyword().onField("author.id").matching(request.getAuthorId()).createQuery()); } Query searchQuery = junction.createQuery(); Sort sort = new Sort(new SortField("sortDate", SortField.Type.STRING, true), new SortField("sortId", SortField.Type.LONG, true)); FullTextQuery persistenceQuery = fullTextEntityManager.createFullTextQuery(searchQuery, Article.class) .setCriteriaQuery(criteria).setSort(sort); if (pageable != null) { persistenceQuery.setFirstResult(pageable.getOffset()); persistenceQuery.setMaxResults(pageable.getPageSize()); } return persistenceQuery; }
From source file:net.dv8tion.jda.managers.ChannelManager.java
/** * Sets the position of this Channel.// w w w . ja va 2 s. co m * If another Channel of the same Type and target newPosition already exists in this Guild, * this channel will get placed above the existing one (newPosition gets decremented). * <p> * This change will only be applied, if {@link #update()} is called. * So multiple changes can be made at once. * * @param newPosition The new position of the Channel, or -1 to keep current one * @return this */ public ChannelManager setPosition(int newPosition) { if (!PermissionUtil.checkPermission(channel.getJDA().getSelfInfo(), Permission.MANAGE_CHANNEL, channel.getGuild())) throw new PermissionException("Do not have " + Permission.MANAGE_CHANNEL + " for this Guild. Cannot change the position of channels."); newPositions.clear(); if (newPosition < 0 || newPosition == channel.getPosition()) { return this; } this.position = newPosition; Map<Integer, Channel> currentPositions = (channel instanceof TextChannel ? channel.getGuild().getTextChannels() : channel.getGuild().getVoiceChannels()).stream() .collect(Collectors.toMap(chan -> chan.getPosition(), chan -> chan)); //We create a search index to make sure we insert at the right place. If the position we are inserting at // is greater than where the channel was before, that means it will be skipping over its original position // in the channel list, thus the iterating index will be 1 less than expected, so we decrement the // search index by 1 to account for this. int searchIndex = newPosition > channel.getPosition() ? newPosition - 1 : newPosition; int index = 0; for (Channel chan : currentPositions.values()) { //When we encounter the old position of the channel, ignore it. if (chan == channel) continue; if (index == searchIndex) { newPositions.put(index, channel); index++; } newPositions.put(index, chan); index++; } //If the channel was moved to the very bottom, this will make sure it is properly handled. if (!newPositions.containsValue(channel)) newPositions.put(newPosition, channel); return this; }
From source file:org.jboss.as.test.integration.management.extension.customcontext.testbase.CustomManagementContextTestBase.java
private void test(final ManagementClient managementClient) throws IOException { //if (true) return; final String urlBase = "http://" + managementClient.getMgmtAddress() + ":9990/"; final String remapUrl = urlBase + "remap/foo"; final String badRemapUrl = urlBase + "remap/bad"; final String staticUrl = urlBase + "static/hello.txt"; final String staticUrlDirectory = urlBase + "static/"; final String badStaticUrl = urlBase + "static/bad.txt"; // Sanity check try (CloseableHttpClient client = HttpClients.createDefault()) { HttpResponse resp = client.execute(new HttpGet(remapUrl)); assertEquals(404, resp.getStatusLine().getStatusCode()); resp = client.execute(new HttpGet(staticUrl)); assertEquals(404, resp.getStatusLine().getStatusCode()); }// w ww.j av a 2s . com // Add extension and subsystem executeOp(Util.createAddOperation(getExtensionAddress()), managementClient); executeOp(Util.createAddOperation(getSubsystemAddress()), managementClient); // Unauthenticated check try (CloseableHttpClient client = HttpClients.createDefault()) { HttpResponse resp = client.execute(new HttpGet(remapUrl)); assertEquals(401, resp.getStatusLine().getStatusCode()); resp = client.execute(new HttpGet(staticUrl)); assertEquals(200, resp.getStatusLine().getStatusCode()); } try (CloseableHttpClient client = createAuthenticatingClient(managementClient)) { // Authenticated check HttpResponse resp = client.execute(new HttpGet(remapUrl)); assertEquals(200, resp.getStatusLine().getStatusCode()); ModelNode respNode = ModelNode.fromJSONString(EntityUtils.toString(resp.getEntity())); assertEquals(respNode.toString(), CustomContextExtension.EXTENSION_NAME, respNode.get("module").asString()); assertTrue(respNode.toString(), respNode.hasDefined("subsystem")); assertTrue(respNode.toString(), respNode.get("subsystem").has(CustomContextExtension.SUBSYSTEM_NAME)); resp = client.execute(new HttpGet(staticUrl)); assertEquals(200, resp.getStatusLine().getStatusCode()); String text = EntityUtils.toString(resp.getEntity()); assertTrue(text, text.startsWith("Hello")); // the response should contain headers: // X-Frame-Options: SAMEORIGIN // Cache-Control: public, max-age=2678400 final Map<String, String> headersMap = Arrays.stream(resp.getAllHeaders()) .collect(Collectors.toMap(Header::getName, Header::getValue)); Assert.assertTrue("'X-Frame-Options: SAMEORIGIN' header is expected", headersMap.getOrDefault("X-Frame-Options", "").contains("SAMEORIGIN")); Assert.assertTrue("Cache-Control header with max-age=2678400 is expected,", headersMap.getOrDefault("Cache-Control", "").contains("max-age=2678400")); // directory listing is not allowed resp = client.execute(new HttpGet(staticUrlDirectory)); assertEquals(404, resp.getStatusLine().getStatusCode()); // POST check resp = client.execute(new HttpPost(remapUrl)); assertEquals(405, resp.getStatusLine().getStatusCode()); resp = client.execute(new HttpPost(staticUrl)); assertEquals(200, resp.getStatusLine().getStatusCode()); // Bad URL check resp = client.execute(new HttpGet(badRemapUrl)); assertEquals(404, resp.getStatusLine().getStatusCode()); resp = client.execute(new HttpGet(badStaticUrl)); assertEquals(404, resp.getStatusLine().getStatusCode()); // Remove subsystem executeOp(Util.createRemoveOperation(getSubsystemAddress()), managementClient); // Requests fail resp = client.execute(new HttpGet(remapUrl)); assertEquals(404, resp.getStatusLine().getStatusCode()); resp = client.execute(new HttpGet(staticUrl)); assertEquals(404, resp.getStatusLine().getStatusCode()); } }
From source file:com.uber.hoodie.func.TestBoundedInMemoryQueue.java
/** * Test to ensure that we are reading all records from queue iterator when we have multiple producers *///from www. ja v a 2s . co m @SuppressWarnings("unchecked") @Test(timeout = 60000) public void testCompositeProducerRecordReading() throws Exception { final int numRecords = 1000; final int numProducers = 40; final List<List<HoodieRecord>> recs = new ArrayList<>(); final BoundedInMemoryQueue<HoodieRecord, Tuple2<HoodieRecord, Optional<IndexedRecord>>> queue = new BoundedInMemoryQueue( FileUtils.ONE_KB, getTransformFunction(HoodieTestDataGenerator.avroSchema)); // Record Key to <Producer Index, Rec Index within a producer> Map<String, Tuple2<Integer, Integer>> keyToProducerAndIndexMap = new HashMap<>(); for (int i = 0; i < numProducers; i++) { List<HoodieRecord> pRecs = hoodieTestDataGenerator.generateInserts(commitTime, numRecords); int j = 0; for (HoodieRecord r : pRecs) { Assert.assertTrue(!keyToProducerAndIndexMap.containsKey(r.getRecordKey())); keyToProducerAndIndexMap.put(r.getRecordKey(), new Tuple2<>(i, j)); j++; } recs.add(pRecs); } List<BoundedInMemoryQueueProducer<HoodieRecord>> producers = new ArrayList<>(); for (int i = 0; i < recs.size(); i++) { final List<HoodieRecord> r = recs.get(i); // Alternate between pull and push based iterators if (i % 2 == 0) { producers.add(new IteratorBasedQueueProducer<>(r.iterator())); } else { producers.add(new FunctionBasedQueueProducer<HoodieRecord>((buf) -> { Iterator<HoodieRecord> itr = r.iterator(); while (itr.hasNext()) { try { buf.insertRecord(itr.next()); } catch (Exception e) { throw new HoodieException(e); } } return true; })); } } final List<Future<Boolean>> futureList = producers.stream().map(producer -> { return executorService.submit(() -> { producer.produce(queue); return true; }); }).collect(Collectors.toList()); // Close queue Future<Boolean> closeFuture = executorService.submit(() -> { try { for (Future f : futureList) { f.get(); } queue.close(); } catch (Exception e) { throw new RuntimeException(e); } return true; }); // Used to ensure that consumer sees the records generated by a single producer in FIFO order Map<Integer, Integer> lastSeenMap = IntStream.range(0, numProducers).boxed() .collect(Collectors.toMap(Function.identity(), x -> -1)); Map<Integer, Integer> countMap = IntStream.range(0, numProducers).boxed() .collect(Collectors.toMap(Function.identity(), x -> 0)); // Read recs and ensure we have covered all producer recs. while (queue.iterator().hasNext()) { final Tuple2<HoodieRecord, Optional<IndexedRecord>> payload = queue.iterator().next(); final HoodieRecord rec = payload._1(); Tuple2<Integer, Integer> producerPos = keyToProducerAndIndexMap.get(rec.getRecordKey()); Integer lastSeenPos = lastSeenMap.get(producerPos._1()); countMap.put(producerPos._1(), countMap.get(producerPos._1()) + 1); lastSeenMap.put(producerPos._1(), lastSeenPos + 1); // Ensure we are seeing the next record generated Assert.assertEquals(lastSeenPos + 1, producerPos._2().intValue()); } for (int i = 0; i < numProducers; i++) { // Ensure we have seen all the records for each producers Assert.assertEquals(Integer.valueOf(numRecords), countMap.get(i)); } //Ensure Close future is done closeFuture.get(); }