/** * Converts runtime edge data to persistent edge data (includes source/target vertex data) and * writes it to HBase. * * @param epgmDatabase EPGM database instance * @param edgeDataHandler edge data handler * @param persistentEdgeDataFactory persistent edge data factory * @param edgeDataTableName HBase edge data table name * @param <PED> persistent edge data type * @throws IOException */ public <PED extends PersistentEdgeData<VD>> void writeEdgeData( final EPGMDatabase<VD, ED, GD> epgmDatabase, final EdgeDataHandler<ED, VD> edgeDataHandler, final PersistentEdgeDataFactory<ED, VD, PED> persistentEdgeDataFactory, final String edgeDataTableName) throws IOException { Graph<Long, VD, ED> graph = epgmDatabase.getDatabaseGraph().getGellyGraph(); DataSet<PersistentEdgeData<VD>> persistentEdgeDataSet = graph .getVertices() // join vertex with edges on edge source vertex id .join(graph.getEdges()) .where(0) .equalTo(1) // join result with vertices on edge target vertex id .join(graph.getVertices()) .where("1.1") .equalTo(0) // ((source-vertex-data, edge-data), target-vertex-data) .with(new PersistentEdgeDataJoinFunction<>(persistentEdgeDataFactory)); // write (persistent-edge-data) to HBase table Job job = Job.getInstance(); job.getConfiguration().set(TableOutputFormat.OUTPUT_TABLE, edgeDataTableName); persistentEdgeDataSet .map(new HBaseWriter.EdgeDataToHBaseMapper<>(edgeDataHandler)) .output(new HadoopOutputFormat<>(new TableOutputFormat<LongWritable>(), job)); }
/** * Converts runtime vertex data to persistent vertex data (includes incoming and outgoing edge * data) and writes it to HBase. * * @param epgmDatabase EPGM database instance * @param vertexDataHandler vertex data handler * @param persistentVertexDataFactory persistent vertex data factory * @param vertexDataTableName HBase vertex data table name * @param <PVD> persistent vertex data type * @throws Exception */ public <PVD extends PersistentVertexData<ED>> void writeVertexData( final EPGMDatabase<VD, ED, GD> epgmDatabase, final VertexDataHandler<VD, ED> vertexDataHandler, final PersistentVertexDataFactory<VD, ED, PVD> persistentVertexDataFactory, final String vertexDataTableName) throws Exception { final Graph<Long, VD, ED> graph = epgmDatabase.getDatabaseGraph().getGellyGraph(); // group edges by source vertex id (vertex-id, [out-edge-data]) GroupReduceOperator<Edge<Long, ED>, Tuple2<Long, Set<ED>>> vertexToOutgoingEdges = graph .getEdges() .groupBy(0) // group by source vertex id .reduceGroup( new GroupReduceFunction<Edge<Long, ED>, Tuple2<Long, Set<ED>>>() { @Override public void reduce( Iterable<Edge<Long, ED>> edgeIterable, Collector<Tuple2<Long, Set<ED>>> collector) throws Exception { Set<ED> outgoingEdgeData = Sets.newHashSet(); Long vertexId = null; boolean initialized = false; for (Edge<Long, ED> edgeData : edgeIterable) { if (!initialized) { vertexId = edgeData.getSource(); initialized = true; } outgoingEdgeData.add(edgeData.getValue()); } collector.collect(new Tuple2<>(vertexId, outgoingEdgeData)); } }); // group edges by target vertex id (vertex-id, [in-edge-data]) GroupReduceOperator<Edge<Long, ED>, Tuple2<Long, Set<ED>>> vertexToIncomingEdges = graph .getEdges() .groupBy(1) // group by target vertex id .reduceGroup( new GroupReduceFunction<Edge<Long, ED>, Tuple2<Long, Set<ED>>>() { @Override public void reduce( Iterable<Edge<Long, ED>> edgeIterable, Collector<Tuple2<Long, Set<ED>>> collector) throws Exception { Set<ED> outgoingEdgeData = Sets.newHashSet(); Long vertexId = null; boolean initialized = false; for (Edge<Long, ED> edgeData : edgeIterable) { if (!initialized) { vertexId = edgeData.getTarget(); initialized = true; } outgoingEdgeData.add(edgeData.getValue()); } collector.collect(new Tuple2<>(vertexId, outgoingEdgeData)); } }); // co-group (vertex-data) with (vertex-id, [out-edge-data]) to simulate left // outer join DataSet<Tuple2<Vertex<Long, VD>, Set<ED>>> vertexDataWithOutgoingEdges = graph .getVertices() .coGroup(vertexToOutgoingEdges) .where(0) .equalTo(0) .with( new CoGroupFunction< Vertex<Long, VD>, Tuple2<Long, Set<ED>>, Tuple2<Vertex<Long, VD>, Set<ED>>>() { @Override public void coGroup( Iterable<Vertex<Long, VD>> vertexIterable, Iterable<Tuple2<Long, Set<ED>>> outEdgesIterable, Collector<Tuple2<Vertex<Long, VD>, Set<ED>>> collector) throws Exception { Vertex<Long, VD> vertex = null; Set<ED> outgoingEdgeData = null; // read vertex data from left group for (Vertex<Long, VD> v : vertexIterable) { vertex = v; } // read outgoing edge from right group (may be empty) for (Tuple2<Long, Set<ED>> oEdges : outEdgesIterable) { outgoingEdgeData = oEdges.f1; } collector.collect(new Tuple2<>(vertex, outgoingEdgeData)); } }); // co-group (vertex-data, (vertex-id, [out-edge-data])) with (vertex-id, // [in-edge-data]) to simulate left outer join DataSet<PersistentVertexData<ED>> persistentVertexDataSet = vertexDataWithOutgoingEdges .coGroup(vertexToIncomingEdges) .where("0.0") .equalTo(0) .with(new PersistentVertexDataCoGroupFunction<>(persistentVertexDataFactory)); // write (persistent-vertex-data) to HBase table Job job = Job.getInstance(); job.getConfiguration().set(TableOutputFormat.OUTPUT_TABLE, vertexDataTableName); persistentVertexDataSet .map(new HBaseWriter.VertexDataToHBaseMapper<>(vertexDataHandler)) .output(new HadoopOutputFormat<>(new TableOutputFormat<LongWritable>(), job)); }
/** * Converts runtime graph data to persistent graph data (including vertex and edge identifiers) * and writes it to HBase. * * @param epgmDatabase EPGM database instance * @param graphDataHandler graph data handler * @param persistentGraphDataFactory persistent graph data factory * @param graphDataTableName HBase graph data table name * @param <PGD> persistent graph data type * @throws IOException */ public <PGD extends PersistentGraphData> void writeGraphData( final EPGMDatabase<VD, ED, GD> epgmDatabase, final GraphDataHandler<GD> graphDataHandler, final PersistentGraphDataFactory<GD, PGD> persistentGraphDataFactory, final String graphDataTableName) throws IOException { final Graph<Long, VD, ED> graph = epgmDatabase.getDatabaseGraph().getGellyGraph(); // build (graph-id, vertex-id) tuples from vertices FlatMapOperator<Vertex<Long, VD>, Tuple2<Long, Long>> graphIdToVertexId = graph .getVertices() .flatMap( new FlatMapFunction<Vertex<Long, VD>, Tuple2<Long, Long>>() { @Override public void flatMap( Vertex<Long, VD> vertex, Collector<Tuple2<Long, Long>> collector) throws Exception { if (vertex.getValue().getGraphCount() > 0) { for (Long graphID : vertex.getValue().getGraphs()) { collector.collect(new Tuple2<>(graphID, vertex.f0)); } } } }); // build (graph-id, edge-id) tuples from vertices FlatMapOperator<Edge<Long, ED>, Tuple2<Long, Long>> graphIdToEdgeId = graph .getEdges() .flatMap( new FlatMapFunction<Edge<Long, ED>, Tuple2<Long, Long>>() { @Override public void flatMap(Edge<Long, ED> edge, Collector<Tuple2<Long, Long>> collector) throws Exception { if (edge.getValue().getGraphCount() > 0) { for (Long graphId : edge.getValue().getGraphs()) { collector.collect(new Tuple2<>(graphId, edge.getValue().getId())); } } } }); // co-group (graph-id, vertex-id) and (graph-id, edge-id) tuples to // (graph-id, {vertex-id}, {edge-id}) triples CoGroupOperator<Tuple2<Long, Long>, Tuple2<Long, Long>, Tuple3<Long, Set<Long>, Set<Long>>> graphToVertexIdsAndEdgeIds = graphIdToVertexId .coGroup(graphIdToEdgeId) .where(0) .equalTo(0) .with( new CoGroupFunction< Tuple2<Long, Long>, Tuple2<Long, Long>, Tuple3<Long, Set<Long>, Set<Long>>>() { @Override public void coGroup( Iterable<Tuple2<Long, Long>> graphToVertexIds, Iterable<Tuple2<Long, Long>> graphToEdgeIds, Collector<Tuple3<Long, Set<Long>, Set<Long>>> collector) throws Exception { Set<Long> vertexIds = Sets.newHashSet(); Set<Long> edgeIds = Sets.newHashSet(); Long graphId = null; boolean initialized = false; for (Tuple2<Long, Long> graphToVertexTuple : graphToVertexIds) { if (!initialized) { graphId = graphToVertexTuple.f0; initialized = true; } vertexIds.add(graphToVertexTuple.f1); } for (Tuple2<Long, Long> graphToEdgeTuple : graphToEdgeIds) { edgeIds.add(graphToEdgeTuple.f1); } collector.collect(new Tuple3<>(graphId, vertexIds, edgeIds)); } }); // join (graph-id, {vertex-id}, {edge-id}) triples with // (graph-id, graph-data) and build (persistent-graph-data) JoinOperator.EquiJoin< Tuple3<Long, Set<Long>, Set<Long>>, Subgraph<Long, GD>, PersistentGraphData> persistentGraphDataSet = graphToVertexIdsAndEdgeIds .join(epgmDatabase.getCollection().getSubgraphs()) .where(0) .equalTo(0) .with(new PersistentGraphDataJoinFunction<>(persistentGraphDataFactory)); // write (persistent-graph-data) to HBase table Job job = Job.getInstance(); job.getConfiguration().set(TableOutputFormat.OUTPUT_TABLE, graphDataTableName); persistentGraphDataSet .map(new HBaseWriter.GraphDataToHBaseMapper<>(graphDataHandler)) .output(new HadoopOutputFormat<>(new TableOutputFormat<LongWritable>(), job)); }