Return-Path: X-Original-To: apmail-flink-issues-archive@minotaur.apache.org Delivered-To: apmail-flink-issues-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id A410B175E2 for ; Mon, 15 Jun 2015 12:33:04 +0000 (UTC) Received: (qmail 11303 invoked by uid 500); 15 Jun 2015 12:33:04 -0000 Delivered-To: apmail-flink-issues-archive@flink.apache.org Received: (qmail 11256 invoked by uid 500); 15 Jun 2015 12:33:04 -0000 Mailing-List: contact issues-help@flink.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@flink.apache.org Delivered-To: mailing list issues@flink.apache.org Received: (qmail 11247 invoked by uid 99); 15 Jun 2015 12:33:04 -0000 Received: from Unknown (HELO spamd1-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 15 Jun 2015 12:33:04 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd1-us-west.apache.org (ASF Mail Server at spamd1-us-west.apache.org) with ESMTP id 7F7DECDF01 for ; Mon, 15 Jun 2015 12:33:03 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd1-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: 0.521 X-Spam-Level: X-Spam-Status: No, score=0.521 tagged_above=-999 required=6.31 tests=[KAM_LAZY_DOMAIN_SECURITY=1, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, RP_MATCHES_RCVD=-0.46, URIBL_BLOCKED=0.001] autolearn=disabled Received: from mx1-eu-west.apache.org ([10.40.0.8]) by localhost (spamd1-us-west.apache.org [10.40.0.7]) (amavisd-new, port 10024) with ESMTP id O1vH_vDv-DRd for ; Mon, 15 Jun 2015 12:32:59 +0000 (UTC) Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx1-eu-west.apache.org (ASF Mail Server at mx1-eu-west.apache.org) with SMTP id 6F25824CEA for ; Mon, 15 Jun 2015 12:32:58 +0000 (UTC) Received: (qmail 11084 invoked by uid 99); 15 Jun 2015 12:32:57 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 15 Jun 2015 12:32:57 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 76AD9E04D7; Mon, 15 Jun 2015 12:32:57 +0000 (UTC) From: tillrohrmann To: issues@flink.incubator.apache.org Reply-To: issues@flink.incubator.apache.org References: In-Reply-To: Subject: [GitHub] flink pull request: [FLINK-2152] Added zipWithIndex Content-Type: text/plain Message-Id: <20150615123257.76AD9E04D7@git1-us-west.apache.org> Date: Mon, 15 Jun 2015 12:32:57 +0000 (UTC) Github user tillrohrmann commented on a diff in the pull request: https://github.com/apache/flink/pull/832#discussion_r32414522 --- Diff: flink-java/src/main/java/org/apache/flink/api/java/utils/DataSetUtils.java --- @@ -0,0 +1,106 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.api.java.utils; + +import org.apache.flink.api.common.functions.RichMapPartitionFunction; +import org.apache.flink.api.java.DataSet; +import org.apache.flink.api.java.tuple.Tuple2; +import org.apache.flink.configuration.Configuration; +import org.apache.flink.util.Collector; + +import java.util.Collections; +import java.util.Comparator; +import java.util.List; + +/** + * This class provides simple utility methods for zipping elements in a file with an index. + * + * @param The type of the DataSet, i.e., the type of the elements of the DataSet. + */ +public class DataSetUtils { + + /** + * Method that goes over all the elements in each partition in order to retireve + * the total number of elements. + * + * @param input the DataSet received as input + * @return a data set containing tuples of subtask index, number of elements mappings. + */ + public DataSet> countElements(DataSet input) { + return input.mapPartition(new RichMapPartitionFunction>() { + @Override + public void mapPartition(Iterable values, Collector> out) throws Exception { + long counter = 0; + for(T value: values) { + counter ++; + } + + out.collect(new Tuple2(getRuntimeContext().getIndexOfThisSubtask(), counter)); + } + }); + } + + /** + * Method that takes a set of subtask index, total number of elements mappings + * and assigns ids to all the elements from the input data set. + * + * @param input the input data set + * @return a data set of tuple 2 consisting of consecutive ids and initial values. + */ + public DataSet> zipWithIndex(DataSet input) { --- End diff -- Well, moving it to `DataSet` means to change the API of Flink and that's something we first have to decide. Therefore, we first wanted to provide this functionality as part of a utils class. Since `zipWithIndex` is stateless with respect to the utils class, it should be `static`. For the Scala API you can, though, use the pimp my library pattern to allow a syntax `ds.zipWithIndex()`, with `ds` being some `DataSet`, without having to change the Scala API. Once we have decided whether these methods shall become part of the API, we can simply add them to the Java and Scala `DataSet` classes. --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastructure@apache.org or file a JIRA ticket with INFRA. ---