【发布时间】:2016-01-06 14:24:55
【问题描述】:
我在 Storm 中的一个关键是在 Neo4j 上添加边缘的模块。 如果我使用并行性,我有重复。代码如下:
try {
Connection connection = getNeo4jConnection();
try {
Trip trip = (Trip) tuple.getValueByField("trip");
try (Statement stmt = connection.createStatement()) {
String query = "MATCH (u:Airport {name:'" +
trip.getOutgoingAirport() + "'}), (r:Airport {name:'" +
trip.getIngoingAirport() + "'})" +
" CREATE UNIQUE (u)-[:FLIGHT_TO { amount: '"+ 1 +"' }]->(r)";
System.out.println("QUERY " + query);
stmt.execute(query);
}
} finally {
connection.close();
}
} catch (SQLException e) {
System.out.println(e.getMessage());
}
collector.ack(tuple);
我正在试验,所以不要介意丑陋的try catch,它不是生产代码。
连接是通过以下方式创建的:
private static Connection getNeo4jConnection() throws SQLException {
Driver driver = new org.neo4j.jdbc.Driver();
Properties properties = new Properties();
properties.put("user", "neo4j");
properties.put("password", "neo4j");
String url = "jdbc:neo4j://localhost:7474/";
return driver.connect(url, properties);
}
如您所见,查询类似于:MATCH (u:Airport {name:'CIA'}), (r:Airport {name:'STN'}) CREATE UNIQUE (u)-[:FLIGHT_TO { amount: '1' }]->(r)。
现在我已经预料到了,正如您在文档中看到的那样,neo4j 有酸事务,所以我假设我没有重复,但在使用并发螺栓时我有它们。我会假设这是一个竞速条件,因为如果我使用 1 个线程,它就不会发生。
你能帮我弄清楚我做错了什么吗?
【问题讨论】: