zabnicola
21-12-2021, 17:21
Buonasera a tutti,
ho scritto una query cypher per il database neo4j per grafi
MATCH (b:Book)
WITH max(b.ide) AS maximum
MATCH (b:Book)
WHERE b.ide = maximum
RETURN b
ORDER BY ID(b) DESC
LIMIT 1
Questa query se eseguita in "Neo4j Browser" restituisce il primo massimo record dall'elenco di Book, cioè quello con proprietà ide maggiore tra tutti.
ecco il risultato
{
"identity": 23,
"labels": [
"Book"
],
"properties": {
"name": "L'archivio di Agostino Rocca",
"about": "Rocca, Agostino - Archivio - Inventari",
"inLanguage": "it",
"bookFormat": "EPUB",
"ide": 100,
"id": "https://www.fondazioneeinaudi.it/library?id=1c3cfa82-39ed-4d19-9177-a5df3fe77cc4",
"type": "http://schema.org/Book"
}
}
Il problema è che se invece eseguo il mio programma java la Lista è vuota. La lista è ottenuta dall'oggetto Result.
public int getMaxResult(){
Driver driver = GraphDatabase.driver( "bolt://localhost:11003",
AuthTokens.basic( "neo4j",
"Admpa" ) );
Session session = driver.session(SessionConfig.forDatabase( "eindb" ));
Transaction transaction = session.beginTransaction();
nodeMaxQuery = "MATCH (b:Book) ";
nodeMaxQuery += "WITH max(b.ide) AS maximum ";
nodeMaxQuery += "MATCH (b:Book) ";
nodeMaxQuery += "WHERE b.ide = maximum ";
nodeMaxQuery += "RETURN b ";
nodeMaxQuery += "ORDER BY ID(b) DESC ";
nodeMaxQuery += "LIMIT 1 ";
Result res = transaction.run(nodeMaxQuery);
if(res == null ) {
System.out.println("is null");
}
if(res.list().size() <= 0) {
System.out.println("size list is zero");
}else {
System.out.println("size list is " + res.list().size());
}
if(res.list().isEmpty()) {
System.out.println("list is empty");
}
if(!res.hasNext()) {
System.out.println("result has no next record. ");
}
List<Record> resList = res.list();
System.out.println("list of records: " + resList);
int nodeBookId = 0;
for (Record record : resList) {
Value rec = record.get("b");
System.out.print("Record --> [");
for (String key : rec.keys()) {
System.out.print(key + " : " + rec.get(key) + ", ");
if(key == "ide") nodeBookId = rec.get(key).asInt() + 1;
}
System.out.println("]");
}
transaction.commit();
return nodeBookId;
}
output del programma è
- size list 0
- list is empty
- result has no next record
Come è possibile che con il programma java non restituisca nessun Record invece se eseguito in "Neo4j browser" cioè la shell per connettersi al database a grafo restituisce il Record voluto? E' lo stesso db, stessa query.
Ho notato pero' che qualsiasi query come questa restituisce sempre lista vuota: MATCH (b:Book) RETURN b LIMIT 5;
ho scritto una query cypher per il database neo4j per grafi
MATCH (b:Book)
WITH max(b.ide) AS maximum
MATCH (b:Book)
WHERE b.ide = maximum
RETURN b
ORDER BY ID(b) DESC
LIMIT 1
Questa query se eseguita in "Neo4j Browser" restituisce il primo massimo record dall'elenco di Book, cioè quello con proprietà ide maggiore tra tutti.
ecco il risultato
{
"identity": 23,
"labels": [
"Book"
],
"properties": {
"name": "L'archivio di Agostino Rocca",
"about": "Rocca, Agostino - Archivio - Inventari",
"inLanguage": "it",
"bookFormat": "EPUB",
"ide": 100,
"id": "https://www.fondazioneeinaudi.it/library?id=1c3cfa82-39ed-4d19-9177-a5df3fe77cc4",
"type": "http://schema.org/Book"
}
}
Il problema è che se invece eseguo il mio programma java la Lista è vuota. La lista è ottenuta dall'oggetto Result.
public int getMaxResult(){
Driver driver = GraphDatabase.driver( "bolt://localhost:11003",
AuthTokens.basic( "neo4j",
"Admpa" ) );
Session session = driver.session(SessionConfig.forDatabase( "eindb" ));
Transaction transaction = session.beginTransaction();
nodeMaxQuery = "MATCH (b:Book) ";
nodeMaxQuery += "WITH max(b.ide) AS maximum ";
nodeMaxQuery += "MATCH (b:Book) ";
nodeMaxQuery += "WHERE b.ide = maximum ";
nodeMaxQuery += "RETURN b ";
nodeMaxQuery += "ORDER BY ID(b) DESC ";
nodeMaxQuery += "LIMIT 1 ";
Result res = transaction.run(nodeMaxQuery);
if(res == null ) {
System.out.println("is null");
}
if(res.list().size() <= 0) {
System.out.println("size list is zero");
}else {
System.out.println("size list is " + res.list().size());
}
if(res.list().isEmpty()) {
System.out.println("list is empty");
}
if(!res.hasNext()) {
System.out.println("result has no next record. ");
}
List<Record> resList = res.list();
System.out.println("list of records: " + resList);
int nodeBookId = 0;
for (Record record : resList) {
Value rec = record.get("b");
System.out.print("Record --> [");
for (String key : rec.keys()) {
System.out.print(key + " : " + rec.get(key) + ", ");
if(key == "ide") nodeBookId = rec.get(key).asInt() + 1;
}
System.out.println("]");
}
transaction.commit();
return nodeBookId;
}
output del programma è
- size list 0
- list is empty
- result has no next record
Come è possibile che con il programma java non restituisca nessun Record invece se eseguito in "Neo4j browser" cioè la shell per connettersi al database a grafo restituisce il Record voluto? E' lo stesso db, stessa query.
Ho notato pero' che qualsiasi query come questa restituisce sempre lista vuota: MATCH (b:Book) RETURN b LIMIT 5;