Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Licensed to the Apache Software Foundation (ASF) under one or more
0003  * contributor license agreements.  See the NOTICE file distributed with
0004  * this work for additional information regarding copyright ownership.
0005  * The ASF licenses this file to You under the Apache License, Version 2.0
0006  * (the "License"); you may not use this file except in compliance with
0007  * the License.  You may obtain a copy of the License at
0008  *
0009  *    http://www.apache.org/licenses/LICENSE-2.0
0010  *
0011  * Unless required by applicable law or agreed to in writing, software
0012  * distributed under the License is distributed on an "AS IS" BASIS,
0013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014  * See the License for the specific language governing permissions and
0015  * limitations under the License.
0016  */
0017 
0018 package org.apache.spark.examples;
0019 
0020 import org.apache.spark.SparkJobInfo;
0021 import org.apache.spark.SparkStageInfo;
0022 import org.apache.spark.api.java.JavaFutureAction;
0023 import org.apache.spark.api.java.JavaRDD;
0024 import org.apache.spark.api.java.JavaSparkContext;
0025 import org.apache.spark.api.java.function.Function;
0026 import org.apache.spark.sql.SparkSession;
0027 
0028 import java.util.Arrays;
0029 import java.util.List;
0030 
0031 /**
0032  * Example of using Spark's status APIs from Java.
0033  */
0034 public final class JavaStatusTrackerDemo {
0035 
0036   public static final String APP_NAME = "JavaStatusAPIDemo";
0037 
0038   public static final class IdentityWithDelay<T> implements Function<T, T> {
0039     @Override
0040     public T call(T x) throws Exception {
0041       Thread.sleep(2 * 1000);  // 2 seconds
0042       return x;
0043     }
0044   }
0045 
0046   public static void main(String[] args) throws Exception {
0047     SparkSession spark = SparkSession
0048       .builder()
0049       .appName(APP_NAME)
0050       .getOrCreate();
0051 
0052     JavaSparkContext jsc = new JavaSparkContext(spark.sparkContext());
0053 
0054     // Example of implementing a progress reporter for a simple job.
0055     JavaRDD<Integer> rdd = jsc.parallelize(Arrays.asList(1, 2, 3, 4, 5), 5).map(
0056         new IdentityWithDelay<>());
0057     JavaFutureAction<List<Integer>> jobFuture = rdd.collectAsync();
0058     while (!jobFuture.isDone()) {
0059       Thread.sleep(1000);  // 1 second
0060       List<Integer> jobIds = jobFuture.jobIds();
0061       if (jobIds.isEmpty()) {
0062         continue;
0063       }
0064       int currentJobId = jobIds.get(jobIds.size() - 1);
0065       SparkJobInfo jobInfo = jsc.statusTracker().getJobInfo(currentJobId);
0066       SparkStageInfo stageInfo = jsc.statusTracker().getStageInfo(jobInfo.stageIds()[0]);
0067       System.out.println(stageInfo.numTasks() + " tasks total: " + stageInfo.numActiveTasks() +
0068           " active, " + stageInfo.numCompletedTasks() + " complete");
0069     }
0070 
0071     System.out.println("Job results are: " + jobFuture.get());
0072     spark.stop();
0073   }
0074 }