Search in sources :

Example 1 with Result

use of org.apache.flink.graph.library.similarity.JaccardIndex.Result in project flink by apache.

the class JaccardIndex method main.

public static void main(String[] args) throws Exception {
    // Set up the execution environment
    final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
    env.getConfig().enableObjectReuse();
    ParameterTool parameters = ParameterTool.fromArgs(args);
    env.getConfig().setGlobalJobParameters(parameters);
    int little_parallelism = parameters.getInt("little_parallelism", PARALLELISM_DEFAULT);
    DataSet ji;
    switch(parameters.get("input", "")) {
        case "csv":
            {
                String lineDelimiter = StringEscapeUtils.unescapeJava(parameters.get("input_line_delimiter", CsvOutputFormat.DEFAULT_LINE_DELIMITER));
                String fieldDelimiter = StringEscapeUtils.unescapeJava(parameters.get("input_field_delimiter", CsvOutputFormat.DEFAULT_FIELD_DELIMITER));
                GraphCsvReader reader = Graph.fromCsvReader(parameters.getRequired("input_filename"), env).ignoreCommentsEdges("#").lineDelimiterEdges(lineDelimiter).fieldDelimiterEdges(fieldDelimiter);
                switch(parameters.get("type", "")) {
                    case "integer":
                        {
                            Graph<LongValue, NullValue, NullValue> graph = reader.keyType(LongValue.class);
                            if (parameters.getBoolean("simplify", false)) {
                                graph = graph.run(new org.apache.flink.graph.asm.simple.undirected.Simplify<LongValue, NullValue, NullValue>(false).setParallelism(little_parallelism));
                            }
                            ji = graph.run(new org.apache.flink.graph.library.similarity.JaccardIndex<LongValue, NullValue, NullValue>().setLittleParallelism(little_parallelism));
                        }
                        break;
                    case "string":
                        {
                            Graph<StringValue, NullValue, NullValue> graph = reader.keyType(StringValue.class);
                            if (parameters.getBoolean("simplify", false)) {
                                graph = graph.run(new org.apache.flink.graph.asm.simple.undirected.Simplify<StringValue, NullValue, NullValue>(false).setParallelism(little_parallelism));
                            }
                            ji = graph.run(new org.apache.flink.graph.library.similarity.JaccardIndex<StringValue, NullValue, NullValue>().setLittleParallelism(little_parallelism));
                        }
                        break;
                    default:
                        throw new ProgramParametrizationException(getUsage("invalid CSV type"));
                }
            }
            break;
        case "rmat":
            {
                int scale = parameters.getInt("scale", DEFAULT_SCALE);
                int edgeFactor = parameters.getInt("edge_factor", DEFAULT_EDGE_FACTOR);
                RandomGenerableFactory<JDKRandomGenerator> rnd = new JDKRandomGeneratorFactory();
                long vertexCount = 1L << scale;
                long edgeCount = vertexCount * edgeFactor;
                Graph<LongValue, NullValue, NullValue> graph = new RMatGraph<>(env, rnd, vertexCount, edgeCount).setParallelism(little_parallelism).generate();
                boolean clipAndFlip = parameters.getBoolean("clip_and_flip", DEFAULT_CLIP_AND_FLIP);
                if (scale > 32) {
                    ji = graph.run(new Simplify<LongValue, NullValue, NullValue>(clipAndFlip).setParallelism(little_parallelism)).run(new org.apache.flink.graph.library.similarity.JaccardIndex<LongValue, NullValue, NullValue>().setLittleParallelism(little_parallelism));
                } else {
                    ji = graph.run(new TranslateGraphIds<LongValue, IntValue, NullValue, NullValue>(new LongValueToUnsignedIntValue()).setParallelism(little_parallelism)).run(new Simplify<IntValue, NullValue, NullValue>(clipAndFlip).setParallelism(little_parallelism)).run(new org.apache.flink.graph.library.similarity.JaccardIndex<IntValue, NullValue, NullValue>().setLittleParallelism(little_parallelism));
                }
            }
            break;
        default:
            throw new ProgramParametrizationException(getUsage("invalid input type"));
    }
    switch(parameters.get("output", "")) {
        case "print":
            System.out.println();
            for (Object e : ji.collect()) {
                Result result = (Result) e;
                System.out.println(result.toPrintableString());
            }
            break;
        case "hash":
            System.out.println();
            System.out.println(DataSetUtils.checksumHashCode(ji));
            break;
        case "csv":
            String filename = parameters.getRequired("output_filename");
            String lineDelimiter = StringEscapeUtils.unescapeJava(parameters.get("output_line_delimiter", CsvOutputFormat.DEFAULT_LINE_DELIMITER));
            String fieldDelimiter = StringEscapeUtils.unescapeJava(parameters.get("output_field_delimiter", CsvOutputFormat.DEFAULT_FIELD_DELIMITER));
            ji.writeAsCsv(filename, lineDelimiter, fieldDelimiter);
            env.execute("Jaccard Index");
            break;
        default:
            throw new ProgramParametrizationException(getUsage("invalid output type"));
    }
    JobExecutionResult result = env.getLastJobExecutionResult();
    NumberFormat nf = NumberFormat.getInstance();
    System.out.println();
    System.out.println("Execution runtime: " + nf.format(result.getNetRuntime()) + " ms");
}
Also used : ParameterTool(org.apache.flink.api.java.utils.ParameterTool) LongValueToUnsignedIntValue(org.apache.flink.graph.asm.translate.translators.LongValueToUnsignedIntValue) ExecutionEnvironment(org.apache.flink.api.java.ExecutionEnvironment) RandomGenerableFactory(org.apache.flink.graph.generator.random.RandomGenerableFactory) DataSet(org.apache.flink.api.java.DataSet) JDKRandomGeneratorFactory(org.apache.flink.graph.generator.random.JDKRandomGeneratorFactory) JobExecutionResult(org.apache.flink.api.common.JobExecutionResult) Result(org.apache.flink.graph.library.similarity.JaccardIndex.Result) NullValue(org.apache.flink.types.NullValue) StringValue(org.apache.flink.types.StringValue) LongValueToUnsignedIntValue(org.apache.flink.graph.asm.translate.translators.LongValueToUnsignedIntValue) IntValue(org.apache.flink.types.IntValue) TranslateGraphIds(org.apache.flink.graph.asm.translate.TranslateGraphIds) JobExecutionResult(org.apache.flink.api.common.JobExecutionResult) GraphCsvReader(org.apache.flink.graph.GraphCsvReader) RMatGraph(org.apache.flink.graph.generator.RMatGraph) Graph(org.apache.flink.graph.Graph) ProgramParametrizationException(org.apache.flink.client.program.ProgramParametrizationException) LongValue(org.apache.flink.types.LongValue) NumberFormat(java.text.NumberFormat)

Example 2 with Result

use of org.apache.flink.graph.library.similarity.JaccardIndex.Result in project flink by apache.

the class JaccardIndexTest method testRMatGraph.

@Test
public void testRMatGraph() throws Exception {
    long vertexCount = 1 << 8;
    long edgeCount = 8 * vertexCount;
    RandomGenerableFactory<JDKRandomGenerator> rnd = new JDKRandomGeneratorFactory();
    Graph<LongValue, NullValue, NullValue> graph = new RMatGraph<>(env, rnd, vertexCount, edgeCount).generate().run(new Simplify<LongValue, NullValue, NullValue>(false));
    DataSet<Result<LongValue>> ji = graph.run(new JaccardIndex<LongValue, NullValue, NullValue>().setGroupSize(4));
    Checksum checksum = new ChecksumHashCode<Result<LongValue>>().run(ji).execute();
    assertEquals(13954, checksum.getCount());
    assertEquals(0x00001b1a1f7a9d0bL, checksum.getChecksum());
}
Also used : JDKRandomGeneratorFactory(org.apache.flink.graph.generator.random.JDKRandomGeneratorFactory) ChecksumHashCode(org.apache.flink.graph.asm.dataset.ChecksumHashCode) Result(org.apache.flink.graph.library.similarity.JaccardIndex.Result) RMatGraph(org.apache.flink.graph.generator.RMatGraph) NullValue(org.apache.flink.types.NullValue) Checksum(org.apache.flink.graph.asm.dataset.ChecksumHashCode.Checksum) LongValue(org.apache.flink.types.LongValue) JDKRandomGenerator(org.apache.commons.math3.random.JDKRandomGenerator) Test(org.junit.Test)

Example 3 with Result

use of org.apache.flink.graph.library.similarity.JaccardIndex.Result in project flink by apache.

the class JaccardIndexTest method testWithRMatGraph.

@Test
public void testWithRMatGraph() throws Exception {
    DataSet<Result<LongValue>> ji = undirectedRMatGraph(8, 8).run(new JaccardIndex<LongValue, NullValue, NullValue>().setGroupSize(4));
    Checksum checksum = new ChecksumHashCode<Result<LongValue>>().run(ji).execute();
    assertEquals(13954, checksum.getCount());
    assertEquals(0x00001b1a1f7a9d0bL, checksum.getChecksum());
}
Also used : Checksum(org.apache.flink.graph.asm.dataset.ChecksumHashCode.Checksum) LongValue(org.apache.flink.types.LongValue) ChecksumHashCode(org.apache.flink.graph.asm.dataset.ChecksumHashCode) Result(org.apache.flink.graph.library.similarity.JaccardIndex.Result) Test(org.junit.Test)

Aggregations

Result (org.apache.flink.graph.library.similarity.JaccardIndex.Result)3 LongValue (org.apache.flink.types.LongValue)3 ChecksumHashCode (org.apache.flink.graph.asm.dataset.ChecksumHashCode)2 Checksum (org.apache.flink.graph.asm.dataset.ChecksumHashCode.Checksum)2 RMatGraph (org.apache.flink.graph.generator.RMatGraph)2 JDKRandomGeneratorFactory (org.apache.flink.graph.generator.random.JDKRandomGeneratorFactory)2 NullValue (org.apache.flink.types.NullValue)2 Test (org.junit.Test)2 NumberFormat (java.text.NumberFormat)1 JDKRandomGenerator (org.apache.commons.math3.random.JDKRandomGenerator)1 JobExecutionResult (org.apache.flink.api.common.JobExecutionResult)1 DataSet (org.apache.flink.api.java.DataSet)1 ExecutionEnvironment (org.apache.flink.api.java.ExecutionEnvironment)1 ParameterTool (org.apache.flink.api.java.utils.ParameterTool)1 ProgramParametrizationException (org.apache.flink.client.program.ProgramParametrizationException)1 Graph (org.apache.flink.graph.Graph)1 GraphCsvReader (org.apache.flink.graph.GraphCsvReader)1 TranslateGraphIds (org.apache.flink.graph.asm.translate.TranslateGraphIds)1 LongValueToUnsignedIntValue (org.apache.flink.graph.asm.translate.translators.LongValueToUnsignedIntValue)1 RandomGenerableFactory (org.apache.flink.graph.generator.random.RandomGenerableFactory)1