Package org.apache.flink.test.javaApiOperators

Source Code of org.apache.flink.test.javaApiOperators.AggregateITCase

/*
* 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.test.javaApiOperators;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Collection;
import java.util.LinkedList;

import org.apache.flink.api.java.aggregation.Aggregations;
import org.apache.flink.api.java.tuple.Tuple1;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.api.java.tuple.Tuple3;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.test.javaApiOperators.util.CollectionDataSets;
import org.apache.flink.test.util.JavaProgramTestBase;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.apache.flink.api.java.DataSet;
import org.apache.flink.api.java.ExecutionEnvironment;

@RunWith(Parameterized.class)
public class AggregateITCase extends JavaProgramTestBase {
 
  private static int NUM_PROGRAMS = 3;
 
  private int curProgId = config.getInteger("ProgramId", -1);
  private String resultPath;
  private String expectedResult;
 
  public AggregateITCase(Configuration config) {
    super(config);
  }
 
  @Override
  protected void preSubmit() throws Exception {
    resultPath = getTempDirPath("result");
  }

  @Override
  protected void testProgram() throws Exception {
    expectedResult = AggregateProgs.runProgram(curProgId, resultPath);
  }
 
  @Override
  protected void postSubmit() throws Exception {
    compareResultsByLinesInMemory(expectedResult, resultPath);
  }
 
  @Parameters
  public static Collection<Object[]> getConfigurations() throws FileNotFoundException, IOException {

    LinkedList<Configuration> tConfigs = new LinkedList<Configuration>();

    for(int i=1; i <= NUM_PROGRAMS; i++) {
      Configuration config = new Configuration();
      config.setInteger("ProgramId", i);
      tConfigs.add(config);
    }
   
    return toParameterList(tConfigs);
  }
 
  private static class AggregateProgs {
   
    public static String runProgram(int progId, String resultPath) throws Exception {
     
      switch(progId) {
      case 1: {
        /*
         * Full Aggregate
         */
       
        final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
       
        DataSet<Tuple3<Integer, Long, String>> ds = CollectionDataSets.get3TupleDataSet(env);
        DataSet<Tuple2<Integer, Long>> aggregateDs = ds
            .aggregate(Aggregations.SUM, 0)
            .and(Aggregations.MAX, 1)
            .project(0, 1).types(Integer.class, Long.class);
       
        aggregateDs.writeAsCsv(resultPath);
        env.execute();
       
        // return expected result
        return "231,6\n";
      }
      case 2: {
        /*
         * Grouped Aggregate
         */
       
        final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
       
        DataSet<Tuple3<Integer, Long, String>> ds = CollectionDataSets.get3TupleDataSet(env);
        DataSet<Tuple2<Long, Integer>> aggregateDs = ds.groupBy(1)
            .aggregate(Aggregations.SUM, 0)
            .project(1, 0).types(Long.class, Integer.class);
       
        aggregateDs.writeAsCsv(resultPath);
        env.execute();
       
        // return expected result
        return "1,1\n" +
        "2,5\n" +
        "3,15\n" +
        "4,34\n" +
        "5,65\n" +
        "6,111\n";
      }
      case 3: {
        /*
         * Nested Aggregate
         */
       
        final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
       
        DataSet<Tuple3<Integer, Long, String>> ds = CollectionDataSets.get3TupleDataSet(env);
        DataSet<Tuple1<Integer>> aggregateDs = ds.groupBy(1)
            .aggregate(Aggregations.MIN, 0)
            .aggregate(Aggregations.MIN, 0)
            .project(0).types(Integer.class);
       
        aggregateDs.writeAsCsv(resultPath);
        env.execute();
       
        // return expected result
        return "1\n";
      }
      default:
        throw new IllegalArgumentException("Invalid program id");
      }
    }
  }
}
TOP

Related Classes of org.apache.flink.test.javaApiOperators.AggregateITCase

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.