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.mllib;
0019 
0020 import java.util.Arrays;
0021 
0022 import scala.Tuple3;
0023 
0024 import org.apache.spark.SparkConf;
0025 import org.apache.spark.api.java.JavaRDD;
0026 import org.apache.spark.api.java.JavaSparkContext;
0027 // $example on$
0028 import org.apache.spark.mllib.clustering.PowerIterationClustering;
0029 import org.apache.spark.mllib.clustering.PowerIterationClusteringModel;
0030 // $example off$
0031 
0032 /**
0033  * Java example for graph clustering using power iteration clustering (PIC).
0034  */
0035 public class JavaPowerIterationClusteringExample {
0036   public static void main(String[] args) {
0037     SparkConf sparkConf = new SparkConf().setAppName("JavaPowerIterationClusteringExample");
0038     JavaSparkContext sc = new JavaSparkContext(sparkConf);
0039 
0040     @SuppressWarnings("unchecked")
0041     // $example on$
0042     JavaRDD<Tuple3<Long, Long, Double>> similarities = sc.parallelize(Arrays.asList(
0043       new Tuple3<>(0L, 1L, 0.9),
0044       new Tuple3<>(1L, 2L, 0.9),
0045       new Tuple3<>(2L, 3L, 0.9),
0046       new Tuple3<>(3L, 4L, 0.1),
0047       new Tuple3<>(4L, 5L, 0.9)));
0048 
0049     PowerIterationClustering pic = new PowerIterationClustering()
0050       .setK(2)
0051       .setMaxIterations(10);
0052     PowerIterationClusteringModel model = pic.run(similarities);
0053 
0054     for (PowerIterationClustering.Assignment a: model.assignments().toJavaRDD().collect()) {
0055       System.out.println(a.id() + " -> " + a.cluster());
0056     }
0057     // $example off$
0058 
0059     sc.stop();
0060   }
0061 }