我正在使用Neo4j java driver
将mysql数据库中的节点添加到Neo4j
中。
我的问题是记录中的某些列值可以为null。我该如何处理密码查询,因为对于mysql表中的空值,它没有创建任何节点。
这是我使用neo4j java驱动程序创建节点的代码
public void readObsTableAndCreateNode() throws Exception {
try {
Class.forName("com.mysql.jdbc.Driver");
connect = DriverManager.getConnection("jdbc:mysql://localhost/openmrsnew?user=root&password=qwas");
statement = connect.createStatement();
resultSet = statement.executeQuery("select obs_id, person_id, concept_id, encounter_id, order_id, obs_datetime, value_text, date_created, creator, uuid, voided from obs where `obs`.`date_created` > '2018-09-20 00:00:00' and concept_id != 33334002 limit 2");
List<HashMap<String, Object>> cypherQueriesList = new ArrayList<HashMap<String, Object>>();
while (resultSet.next()) {
HashMap<String, Object> cipherQueryParams = new HashMap<String, Object>();
Integer obsId = Integer.parseInt(resultSet.getString("obs_id"));
Integer personId = Integer.parseInt(resultSet.getString("person_id"));
Integer conceptId = Integer.parseInt(resultSet.getString("concept_id"));
Integer creator = Integer.parseInt(resultSet.getString("creator"));
Integer voided = Integer.parseInt(resultSet.getString("voided"));
Integer encounterId = encounterId = Integer.parseInt(resultSet.getString("encounter_id"));
String date_created = resultSet.getString("date_created");
String value_text= resultSet.getString("value_text");
String uuid = resultSet.getString("uuid");
String obs_datetime= resultSet.getString("obs_datetime");
((HashMap<String, Object>) cipherQueryParams).put("creator", creator);
((HashMap<String, Object>) cipherQueryParams).put("valueText", value_text);
((HashMap<String, Object>) cipherQueryParams).put("uuid", uuid);
((HashMap<String, Object>) cipherQueryParams).put("dateCreated", date_created);
((HashMap<String, Object>) cipherQueryParams).put("obsDatetime", obs_datetime);
((HashMap<String, Object>) cipherQueryParams).put("obsId", obsId);
((HashMap<String, Object>) cipherQueryParams).put("encounterId", encounterId);
((HashMap<String, Object>) cipherQueryParams).put("conceptId", conceptId);
((HashMap<String, Object>) cipherQueryParams).put("personId", personId);
((HashMap<String, Object>) cipherQueryParams).put("voided", voided);
cypherQueriesList.add(cipherQueryParams);
}
HashMap<String, Object> cypherQuerybatch = new HashMap<String, Object>();
cypherQuerybatch.put("batch", cypherQueriesList);
// I am using merge because I dont want duplicated enteries
String cypherQuery = "UNWIND {batch} as row MERGE (m:temp { obsId: row.obsId ,valueText: row.valueText, uuid: row.uuid, dateCreated: row.dateCreated , obsDatetime: row.obsDatetime })"
+ " WITH m, row MATCH (c:Concept {conceptId: row.conceptId }) MERGE (m)-[:CONCEPT]->(c) "
+ " WITH m, row MATCH (e:Encounter {encounterId: row.encounterId }) MERGE (m)-[:ENCOUNTER]->(e) "
+ " WITH m, row MATCH (u:Users {userId: row.creator }) MERGE (m)-[:USERS]->(u) "
+ " WITH m, row MATCH (p:Person {personId: row.personId}) MERGE (m)-[:PERSON]->(p) "
+ " RETURN m";
runCypherWithParams(cypherQuery, cypherQuerybatch);
} catch (Exception e) {
throw e;
} finally {
close();
}
}
private void runCypherWithParams(String cypherQuery, Map<String, Object> parameters) throws Exception{
System.out.println("Posting Data to Neo4J browser...");
try {
connNeo4j = new ConNeo4j("bolt://localhost:7687", "neo4j", "qwas");
connNeo4j.execQueryBySession(cypherQuery, parameters);
} catch (Exception e) {
e.printStackTrace();
}finally {
}
}
public void execQueryBySession(String query, Map<String, Object> parameters) {
try {
Session session = driver.session();
StatementResult result = session.run(query, parameters);
System.out.println("Success:- " + result);
} catch (Exception e) {
System.out.println("[WARNING] Null Row" + e.getMessage());
}
}
在neo4j浏览器控制台中,当我在密码查询下运行时,没有结点形成。
MERGE (m:Temp { obsId: 742241 ,valueText: NULL}) return m;
更新:我能够在Java Side处理null值。但是对于另一张桌子,我得到一些奇怪的行为。
在Neo4j浏览器控制台中。我将参数设置为
:param batch: [{deathdateEstimated: 0, birthdateEstimated: 1, creator: 6, birthdate: "2018-01-01", dateCreated: "2018-09-20 04:08:0", gender: "M", personId: 1, voided: 0, uuid: "d7132677-6f7b-aeb8-af5f-850d621b6bbb"}, {deathdateEstimated: 0, birthdateEstimated: 1, creator: 1, birthdate: "2018-01-01", dateCreated: "2018-09-20", gender: "M", personId: 2, voided: 0}]
当我在密码查询下运行时,只会创建一个节点,而不是两个。
UNWIND {batch} as row MERGE (m:tempPerson) ON CREATE SET m+=row WITH m,row MATCH (u:Users {userId: row.creator }) MERGE (m)-[:USERS]->(u) return m;
答案 0 :(得分:1)
来自Neo4j docs:
您可以执行匹配,其中匹配项中的节点变量为 null(例如来自失败的可选匹配),则无法创建或 合并具有空节点变量的模式。
您可能可以使用的另一个选项是“合并”中提供的创建时和匹配时选项。 但是,此查询取决于可以使用“ obsId”字段唯一标识您的节点的事实。
MERGE (m:Temp { obsId: 742241})
ON CREATE SET m.valueText = NULL
ON MATCH SET m.valueText = NULL
return m;
答案 1 :(得分:0)
如何通过仅检查每个输入值是否为“ not null”并仅在此条件下将其添加到cypherQueryParameter HashMap
中来解决Java方面的问题?