Skip to content
This repository was archived by the owner on Oct 5, 2021. It is now read-only.

Commit e53abb4

Browse files
Addison SlabaughAlexander Patrikalakis
authored andcommitted
Generalizing GraphFactory classes as Abstract classes. Adding ScenarioTest for loading with Triples.
adding javadocs, license, and author tags to newest classes; deleting unused TravelGraphSchema reverting file changes for MultiRowParallelScanInterpreter.java; moving all new classes to src/test reverting file changes for MultiRowParallelScanInterpreter.java; moving all new classes to src/test removing shell script updating constructor logic and preconditions in Triple; moving HotelTriples to test resources.
1 parent 209212a commit e53abb4

File tree

11 files changed

+544
-5
lines changed

11 files changed

+544
-5
lines changed

src/main/java/com/amazon/janusgraph/example/MarvelGraphFactory.java

100644100755
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ public class MarvelGraphFactory {
6565
public static final String WEAPON = "weapon";
6666
public static final MetricRegistry REGISTRY = MetricManager.INSTANCE.getRegistry();
6767
public static final ConsoleReporter REPORTER = ConsoleReporter.forRegistry(REGISTRY).build();
68-
private static final String TIMER_LINE = "MarvelGraphFactory.line";
69-
private static final String TIMER_CREATE = "MarvelGraphFactory.create_";
70-
private static final String COUNTER_GET = "MarvelGraphFactory.get_";
68+
private static final String TIMER_LINE = "MarvelGraph.line";
69+
private static final String TIMER_CREATE = "MarvelGraph.create_";
70+
private static final String COUNTER_GET = "MarvelGraph.get_";
7171
private static final String[] WEAPONS = { "claws", "ring", "shield", "robotic suit", "cards", "surf board", "glider", "gun", "swords", "lasso" };
7272
private static final AtomicInteger COMPLETED_TASK_COUNT = new AtomicInteger(0);
7373
private static final int POOL_SIZE = 10;
@@ -164,7 +164,7 @@ public static void load(final JanusGraph graph, final int rowsToLoad, final bool
164164
REPORTER.report();
165165
}
166166
}
167-
LOG.info("MarvelGraphFactory.load complete");
167+
LOG.info("MarvelGraph.load complete");
168168
}
169169

170170
@RequiredArgsConstructor
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2014-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
package com.amazon.janusgraph;
16+
17+
import com.amazon.janusgraph.triple.Triple;
18+
import org.janusgraph.core.JanusGraph;
19+
import org.slf4j.Logger;
20+
import org.slf4j.LoggerFactory;
21+
22+
import java.net.URL;
23+
import java.util.Set;
24+
import java.util.concurrent.*;
25+
26+
27+
/**
28+
*
29+
* @author Addison Slabaugh
30+
*
31+
*/
32+
public interface AbstractGraph {
33+
34+
int BATCH_SIZE = 10;
35+
Logger LOG = LoggerFactory.getLogger(AbstractGraph.class);
36+
int POOL_SIZE = 10;
37+
38+
ClassLoader classLoader = TravelGraph.class.getClassLoader();
39+
BlockingQueue<Runnable> creationQueue = new LinkedBlockingQueue<>();
40+
ExecutorService executor = Executors.newFixedThreadPool(POOL_SIZE);
41+
42+
static void load(final JanusGraph graph, boolean report) throws Exception {
43+
}
44+
45+
public abstract Set<Triple> processFile(URL url);
46+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright 2014-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
package com.amazon.janusgraph;
16+
17+
18+
/**
19+
*
20+
* @author Addison Slabaugh
21+
*
22+
*/
23+
public interface AbstractGraphFactory {
24+
public AbstractGraph createGraphFactory();
25+
}

src/test/java/com/amazon/janusgraph/MarvelTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.Collection;
2121
import java.util.Iterator;
2222

23+
import com.amazon.janusgraph.example.MarvelGraphFactory;
2324
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource;
2425
import org.apache.tinkerpop.gremlin.structure.Direction;
2526
import org.apache.tinkerpop.gremlin.structure.Edge;
@@ -35,7 +36,6 @@
3536
import org.junit.runners.Parameterized;
3637

3738
import com.amazon.janusgraph.diskstorage.dynamodb.BackendDataModel;
38-
import com.amazon.janusgraph.example.MarvelGraphFactory;
3939
import com.amazon.janusgraph.graphdb.dynamodb.TestCombination;
4040
import com.amazon.janusgraph.testcategory.IsolateRemainingTestsCategory;
4141
import com.codahale.metrics.MetricRegistry;

src/test/java/com/amazon/janusgraph/ScenarioTests.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,16 @@ public void lockingTest() throws BackendException, InterruptedException {
9090
createSchemaAndDemoLockExpiry(USE_STORAGE_NATIVE_LOCKING, USE_EDGESTORE_LOCKING, USE_GRAPHINDEX_LOCKING, 100);
9191
}
9292

93+
/**
94+
* https://stackoverflow.com/questions/44535054/generalizing-dynamodb-janusgraph-factory-lock-and-schema-problems
95+
* @throws Exception
96+
*/
97+
@Test
98+
public void tripleIngestTest(StandardJanusGraph graph) throws Exception {
99+
TravelGraphFactory graphFactory = new TravelGraphFactory();
100+
graphFactory.loadGraphFactory(graph, false);
101+
}
102+
93103
@Test
94104
public void demoNonnativeLockersWithDynamoDB() throws BackendException, InterruptedException {
95105
createSchemaAndDemoLockExpiry(USE_JANUSGRAPH_LOCKING, USE_EDGESTORE_LOCKING, USE_GRAPHINDEX_LOCKING, 200);
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Copyright 2014-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
package com.amazon.janusgraph;
16+
17+
import java.io.BufferedReader;
18+
import java.io.IOException;
19+
import java.io.InputStreamReader;
20+
import java.net.URL;
21+
import java.util.HashSet;
22+
import java.util.Set;
23+
import java.util.concurrent.TimeUnit;
24+
25+
import com.amazon.janusgraph.batch.BatchCommand;
26+
import com.amazon.janusgraph.creator.ObjectCreationCommand;
27+
import org.janusgraph.core.JanusGraph;
28+
import org.janusgraph.util.stats.MetricManager;
29+
import com.codahale.metrics.ConsoleReporter;
30+
import com.codahale.metrics.MetricRegistry;
31+
32+
import com.amazon.janusgraph.triple.Triple;
33+
34+
/**
35+
*
36+
* @author Addison Slabaugh
37+
*
38+
*/
39+
public class TravelGraph implements AbstractGraph {
40+
41+
public static final MetricRegistry REGISTRY = MetricManager.INSTANCE.getRegistry();
42+
public static final ConsoleReporter REPORTER = ConsoleReporter.forRegistry(REGISTRY).build();
43+
private static final int POOL_SIZE = 10;
44+
private static Set<Triple> triples;
45+
46+
/**
47+
*
48+
* @param graph
49+
* @param report allows client to determine whether or not to generate a report after ingestion
50+
* @throws Exception
51+
*/
52+
public void load(final JanusGraph graph, boolean report) throws Exception {
53+
54+
URL resource = classLoader.getResource("META-INF/HotelTriples.txt");
55+
56+
triples = processFile(resource);
57+
for (Triple t : triples) {
58+
creationQueue.add(new ObjectCreationCommand(graph, t, REGISTRY, LOG));
59+
}
60+
61+
for (int i = 0; i < POOL_SIZE; i++) {
62+
executor.execute(new BatchCommand(graph, creationQueue, LOG, BATCH_SIZE));
63+
}
64+
executor.shutdown();
65+
66+
while (!executor.awaitTermination(60, TimeUnit.MILLISECONDS)) {
67+
LOG.info("Awaiting:" + creationQueue.size());
68+
if(report) {
69+
REPORTER.report();
70+
}
71+
}
72+
executor.shutdown();
73+
74+
LOG.info("TravelGraph.load complete");
75+
}
76+
77+
/**
78+
* A method that processes a file containing left and right objects, properties, and relationships and
79+
* returns a set of triples.
80+
*
81+
* @param url URL that points to a resource containing left and right objects, properties
82+
* and relationships
83+
* @return a set of triples that can be ingested into a graph
84+
*/
85+
@Override
86+
public Set<Triple> processFile(URL url) {
87+
Set<Triple> triples = new HashSet<>();
88+
try {
89+
BufferedReader bf = new BufferedReader(new InputStreamReader(url.openStream()));
90+
String line = "";
91+
while ((line = bf.readLine()) != null) {
92+
String[] split = line.split("\t");
93+
Triple t = new Triple(split);
94+
System.out.println(String.valueOf(t));
95+
triples.add(t);
96+
}
97+
} catch (IOException e) {
98+
e.printStackTrace();
99+
}
100+
return triples;
101+
}
102+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2014-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
package com.amazon.janusgraph;
16+
17+
import org.janusgraph.graphdb.database.StandardJanusGraph;
18+
19+
/**
20+
*
21+
* @author Addison Slabaugh
22+
*
23+
*/
24+
public class TravelGraphFactory implements AbstractGraphFactory {
25+
@Override
26+
public AbstractGraph createGraphFactory() {
27+
TravelGraph graph = new TravelGraph();
28+
return graph;
29+
}
30+
31+
/**
32+
* This method creates a new TravelGraph instance and loads it. The client can specify whether or not
33+
* to generate a report after ingestion.
34+
*
35+
* @param graph
36+
* @param report indicates whether or not to produce a report after ingestion
37+
* @throws Exception
38+
*/
39+
public static void loadGraphFactory(StandardJanusGraph graph, boolean report) throws Exception {
40+
TravelGraph g = new TravelGraph();
41+
g.load(graph, report);
42+
}
43+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2014-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
package com.amazon.janusgraph.batch;
16+
17+
import org.apache.commons.lang.exception.ExceptionUtils;
18+
import org.janusgraph.core.JanusGraph;
19+
import org.slf4j.Logger;
20+
21+
import java.util.concurrent.BlockingQueue;
22+
23+
/**
24+
*
25+
* @author Addison Slabaugh
26+
*
27+
*/
28+
public class BatchCommand implements Runnable {
29+
30+
final JanusGraph graph;
31+
final BlockingQueue<Runnable> commands;
32+
public static Logger LOG;
33+
public static int BATCH_SIZE;
34+
35+
public BatchCommand(JanusGraph graph, BlockingQueue<Runnable> commands, Logger LOG, int BATCH_SIZE) {
36+
this.graph = graph;
37+
this.commands = commands;
38+
this.LOG = LOG;
39+
this.BATCH_SIZE = BATCH_SIZE;
40+
}
41+
42+
@Override
43+
public void run() {
44+
int i = 0;
45+
Runnable command = null;
46+
while ((command = commands.poll()) != null) {
47+
try {
48+
command.run();
49+
} catch (Throwable e) {
50+
Throwable rootCause = ExceptionUtils.getRootCause(e);
51+
String rootCauseMessage = null == rootCause ? "" : rootCause.getMessage();
52+
LOG.error("Error processing line {} {}", e.getMessage(), rootCauseMessage, e);
53+
}
54+
if (i++ % BATCH_SIZE == 0) {
55+
try {
56+
graph.tx().commit();
57+
} catch (Throwable e) {
58+
LOG.error("Error processing commit {} {}", e.getMessage(), ExceptionUtils.getRootCause(e).getMessage());
59+
}
60+
}
61+
}
62+
63+
try {
64+
graph.tx().commit();
65+
} catch (Throwable e) {
66+
LOG.error("Error processing commit {} {}", e.getMessage(), ExceptionUtils.getRootCause(e).getMessage());
67+
}
68+
}
69+
70+
}

0 commit comments

Comments
 (0)