Java Code Examples for org.apache.tinkerpop.gremlin.util.iterator.IteratorUtils#map()

The following examples show how to use org.apache.tinkerpop.gremlin.util.iterator.IteratorUtils#map() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SparkContextStorage.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public <K, V> Iterator<KeyValue<K, V>> head(final String location, final String memoryKey, final Class readerClass, final int totalLines) {
    final Configuration configuration = new BaseConfiguration();
    configuration.setProperty(Constants.GREMLIN_HADOOP_INPUT_LOCATION, location);
    configuration.setProperty(Constants.GREMLIN_HADOOP_GRAPH_READER, readerClass.getCanonicalName());
    try {
        if (InputRDD.class.isAssignableFrom(readerClass)) {
            return IteratorUtils.map(((InputRDD) readerClass.getConstructor().newInstance()).readMemoryRDD(configuration, memoryKey, new JavaSparkContext(Spark.getContext())).take(totalLines).iterator(), tuple -> new KeyValue(tuple._1(), tuple._2()));
        } else if (InputFormat.class.isAssignableFrom(readerClass)) {
            return IteratorUtils.map(new InputFormatRDD().readMemoryRDD(configuration, memoryKey, new JavaSparkContext(Spark.getContext())).take(totalLines).iterator(), tuple -> new KeyValue(tuple._1(), tuple._2()));
        }
    } catch (final Exception e) {
        throw new IllegalArgumentException(e.getMessage(), e);
    }
    throw new IllegalArgumentException("The provided parserClass must be an " + InputFormat.class.getCanonicalName() + " or an " + InputRDD.class.getCanonicalName() + ": " + readerClass.getCanonicalName());
}
 
Example 2
Source File: SparkContextStorage.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public Iterator<Vertex> head(final String location, final Class readerClass, final int totalLines) {
    final Configuration configuration = new BaseConfiguration();
    configuration.setProperty(Constants.GREMLIN_HADOOP_INPUT_LOCATION, location);
    configuration.setProperty(Constants.GREMLIN_HADOOP_GRAPH_READER, readerClass.getCanonicalName());
    try {
        if (InputRDD.class.isAssignableFrom(readerClass)) {
            return IteratorUtils.map(((InputRDD) readerClass.getConstructor().newInstance()).readGraphRDD(configuration, new JavaSparkContext(Spark.getContext())).take(totalLines).iterator(), tuple -> tuple._2().get());
        } else if (InputFormat.class.isAssignableFrom(readerClass)) {
            return IteratorUtils.map(new InputFormatRDD().readGraphRDD(configuration, new JavaSparkContext(Spark.getContext())).take(totalLines).iterator(), tuple -> tuple._2().get());
        }
    } catch (final Exception e) {
        throw new IllegalArgumentException(e.getMessage(), e);
    }
    throw new IllegalArgumentException("The provided parserClass must be an " + InputFormat.class.getCanonicalName() + " or an " + InputRDD.class.getCanonicalName() + ": " + readerClass.getCanonicalName());
}
 
Example 3
Source File: SpecializedTinkerVertex.java    From tinkergraph-gremlin with Apache License 2.0 5 votes vote down vote up
@Override
public Iterator<Vertex> vertices(final Direction direction, final String... edgeLabels) {
    Iterator<Edge> edges = edges(direction, edgeLabels);
    if (direction == Direction.IN) {
        return IteratorUtils.map(edges, Edge::outVertex);
    } else if (direction == Direction.OUT) {
        return IteratorUtils.map(edges, Edge::inVertex);
    } else if (direction == Direction.BOTH) {
        return IteratorUtils.concat(vertices(Direction.IN, edgeLabels), vertices(Direction.OUT, edgeLabels));
    } else {
        return Collections.emptyIterator();
    }
}
 
Example 4
Source File: HBaseEdge.java    From hgraphdb with Apache License 2.0 5 votes vote down vote up
@Override
public <V> Iterator<Property<V>> properties(final String... propertyKeys) {
    Iterable<String> keys = getPropertyKeys();
    Iterator<String> filter = IteratorUtils.filter(keys.iterator(),
            key -> ElementHelper.keyExists(key, propertyKeys));
    return IteratorUtils.map(filter,
            key -> new HBaseProperty<>(graph, this, key, getProperty(key)));
}
 
Example 5
Source File: HBaseVertex.java    From hgraphdb with Apache License 2.0 5 votes vote down vote up
@Override
public <V> Iterator<VertexProperty<V>> properties(final String... propertyKeys) {
    Iterable<String> keys = getPropertyKeys();
    Iterator<String> filter = IteratorUtils.filter(keys.iterator(),
            key -> ElementHelper.keyExists(key, propertyKeys));
    return IteratorUtils.map(filter,
            key -> new HBaseVertexProperty<>(graph, this, key, getProperty(key)));
}
 
Example 6
Source File: TinkerVertex.java    From tinkergraph-gremlin with Apache License 2.0 5 votes vote down vote up
@Override
public Iterator<Vertex> vertices(final Direction direction, final String... edgeLabels) {
    return TinkerHelper.inComputerMode(this.graph) ?
            direction.equals(Direction.BOTH) ?
                    IteratorUtils.concat(
                            IteratorUtils.map(this.edges(Direction.OUT, edgeLabels), Edge::inVertex),
                            IteratorUtils.map(this.edges(Direction.IN, edgeLabels), Edge::outVertex)) :
                    IteratorUtils.map(this.edges(direction, edgeLabels), edge -> edge.vertices(direction.opposite()).next()) :
            (Iterator) TinkerHelper.getVertices(this, direction, edgeLabels);
}
 
Example 7
Source File: PersistedOutputRDD.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public <K, V> Iterator<KeyValue<K, V>> writeMemoryRDD(final Configuration configuration, final String memoryKey, final JavaPairRDD<K, V> memoryRDD) {
    if (!configuration.getBoolean(Constants.GREMLIN_SPARK_PERSIST_CONTEXT, false))
        LOGGER.warn("The SparkContext should be persisted in order for the RDD to persist across jobs. To do so, set " + Constants.GREMLIN_SPARK_PERSIST_CONTEXT + " to true");
    if (!configuration.containsKey(Constants.GREMLIN_HADOOP_OUTPUT_LOCATION))
        throw new IllegalArgumentException("There is no provided " + Constants.GREMLIN_HADOOP_OUTPUT_LOCATION + " to write the persisted RDD to");
    final String memoryRDDName = Constants.getMemoryLocation(configuration.getString(Constants.GREMLIN_HADOOP_OUTPUT_LOCATION), memoryKey);
    Spark.removeRDD(memoryRDDName);
    memoryRDD.setName(memoryRDDName).persist(StorageLevel.fromString(configuration.getString(Constants.GREMLIN_SPARK_PERSIST_STORAGE_LEVEL, "MEMORY_ONLY")))
            // call action to eager store rdd
            .count();
    Spark.refresh(); // necessary to do really fast so the Spark GC doesn't clear out the RDD
    return IteratorUtils.map(memoryRDD.collect().iterator(), tuple -> new KeyValue<>(tuple._1(), tuple._2()));
}
 
Example 8
Source File: TinkerVertex.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public Iterator<Vertex> vertices(final Direction direction, final String... edgeLabels) {
    return TinkerHelper.inComputerMode(this.graph) ?
            direction.equals(Direction.BOTH) ?
                    IteratorUtils.concat(
                            IteratorUtils.map(this.edges(Direction.OUT, edgeLabels), Edge::inVertex),
                            IteratorUtils.map(this.edges(Direction.IN, edgeLabels), Edge::outVertex)) :
                    IteratorUtils.map(this.edges(direction, edgeLabels), edge -> edge.vertices(direction.opposite()).next()) :
            (Iterator) TinkerHelper.getVertices(this, direction, edgeLabels);
}
 
Example 9
Source File: StarGraph.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public Iterator<Vertex> vertices(final Direction direction, final String... edgeLabels) {
    if (direction.equals(Direction.OUT))
        return IteratorUtils.map(this.edges(direction, edgeLabels), Edge::inVertex);
    else if (direction.equals(Direction.IN))
        return IteratorUtils.map(this.edges(direction, edgeLabels), Edge::outVertex);
    else
        return IteratorUtils.concat(this.vertices(Direction.IN, edgeLabels), this.vertices(Direction.OUT, edgeLabels));
}
 
Example 10
Source File: BytecodeUtil.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
/**
 * Parses {@link Bytecode} to find {@link TraversalStrategy} objects in the source instructions.
 */
public static <A extends TraversalStrategy> Iterator<A> findStrategies(final Bytecode bytecode, final Class<A> clazz) {
    return IteratorUtils.map(
            IteratorUtils.filter(bytecode.getSourceInstructions().iterator(),
                    s -> s.getOperator().equals(TraversalSource.Symbols.withStrategies) && clazz.isAssignableFrom(s.getArguments()[0].getClass())),
            os -> (A) os.getArguments()[0]);
}
 
Example 11
Source File: ComputerResultStep.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
public Iterator<Traverser.Admin<S>> attach(final Iterator<Traverser.Admin<S>> iterator, final Graph graph) {
    return IteratorUtils.map(iterator, traverser -> {
        traverser.setSideEffects(this.getTraversal().getSideEffects());   // necessary to ensure no NPE
        if (this.attachElements && (traverser.get() instanceof Attachable) && !(traverser.get() instanceof Property))
            traverser.set((S) ((Attachable<Element>) traverser.get()).attach(Attachable.Method.get(graph)));
        return traverser;
    });
}
 
Example 12
Source File: SparkContextStorage.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Override
public Iterator<String> head(final String location, final int totalLines) {
    return IteratorUtils.map(Spark.getRDD(location).toJavaRDD().take(totalLines).iterator(), Object::toString);
}
 
Example 13
Source File: ComputerGraph.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Override
public Iterator<Vertex> vertices(final Direction direction, final String... edgeLabels) {
    if (state.equals(State.MAP_REDUCE))
        throw GraphComputer.Exceptions.incidentAndAdjacentElementsCanNotBeAccessedInMapReduce();
    return IteratorUtils.map(this.getBaseVertex().vertices(direction, edgeLabels), v -> v.equals(starVertex) ? starVertex : new ComputerAdjacentVertex(v));
}
 
Example 14
Source File: ComputerGraph.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Override
public <V> Iterator<V> values(final String... propertyKeys) {
    return IteratorUtils.map(this.<V>properties(propertyKeys), property -> property.value());
}
 
Example 15
Source File: Element.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
/**
 * Get the values of properties as an {@link Iterator}.
 */
public default <V> Iterator<V> values(final String... propertyKeys) {
    return IteratorUtils.map(this.<V>properties(propertyKeys), property -> property.value());
}
 
Example 16
Source File: HadoopConfiguration.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Override
public Iterator iterator() {
    return IteratorUtils.map(this.getKeys(), k -> new Pair<>(k, this.getProperty(k)));
}
 
Example 17
Source File: HadoopVertexProperty.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Override
public <U> Iterator<Property<U>> properties(final String... propertyKeys) {
    return IteratorUtils.<Property<U>, Property<U>>map(this.getBaseVertexProperty().properties(propertyKeys), property -> new HadoopProperty<>(property, HadoopVertexProperty.this));
}
 
Example 18
Source File: HadoopVertex.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Override
public <V> Iterator<VertexProperty<V>> properties(final String... propertyKeys) {
    return IteratorUtils.<VertexProperty<V>, VertexProperty<V>>map(this.getBaseVertex().properties(propertyKeys), property -> new HadoopVertexProperty<>(property, HadoopVertex.this));
}
 
Example 19
Source File: HadoopVertex.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Override
public Iterator<Vertex> vertices(final Direction direction, final String... edgeLabels) {
    return IteratorUtils.map(this.getBaseVertex().vertices(direction, edgeLabels), vertex -> HadoopVertex.this.graph.vertices(vertex.id()).next());
}
 
Example 20
Source File: HadoopEdge.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
@Override
public <V> Iterator<Property<V>> properties(final String... propertyKeys) {
    return IteratorUtils.<Property<V>, Property<V>>map(this.getBaseEdge().properties(propertyKeys), property -> new HadoopProperty<>(property, HadoopEdge.this));
}