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.ml;
0019 
0020 // $example on$
0021 import java.util.Arrays;
0022 import java.util.List;
0023 
0024 import org.apache.spark.ml.fpm.PrefixSpan;
0025 import org.apache.spark.sql.Dataset;
0026 import org.apache.spark.sql.Row;
0027 import org.apache.spark.sql.RowFactory;
0028 import org.apache.spark.sql.SparkSession;
0029 import org.apache.spark.sql.types.*;
0030 // $example off$
0031 
0032 /**
0033  * An example demonstrating PrefixSpan.
0034  * Run with
0035  * <pre>
0036  * bin/run-example ml.JavaPrefixSpanExample
0037  * </pre>
0038  */
0039 public class JavaPrefixSpanExample {
0040   public static void main(String[] args) {
0041     SparkSession spark = SparkSession
0042       .builder()
0043       .appName("JavaPrefixSpanExample")
0044       .getOrCreate();
0045 
0046     // $example on$
0047     List<Row> data = Arrays.asList(
0048       RowFactory.create(Arrays.asList(Arrays.asList(1, 2), Arrays.asList(3))),
0049       RowFactory.create(Arrays.asList(Arrays.asList(1), Arrays.asList(3, 2), Arrays.asList(1,2))),
0050       RowFactory.create(Arrays.asList(Arrays.asList(1, 2), Arrays.asList(5))),
0051       RowFactory.create(Arrays.asList(Arrays.asList(6)))
0052     );
0053     StructType schema = new StructType(new StructField[]{ new StructField(
0054       "sequence", new ArrayType(new ArrayType(DataTypes.IntegerType, true), true),
0055       false, Metadata.empty())
0056     });
0057     Dataset<Row> sequenceDF = spark.createDataFrame(data, schema);
0058 
0059     PrefixSpan prefixSpan = new PrefixSpan().setMinSupport(0.5).setMaxPatternLength(5);
0060 
0061     // Finding frequent sequential patterns
0062     prefixSpan.findFrequentSequentialPatterns(sequenceDF).show();
0063     // $example off$
0064 
0065     spark.stop();
0066   }
0067 }