Neo4j Java bolt driver: how to convert the result to Json? - java

I am using the Java Bolt driver (1.0.1) and I am wondering there is a way to convert the result to Json (possibly the same as in the REST api)?
I tried to use gson in this way:
Result r = null;
try ( Transaction tx = graphDb.beginTx() )
{
r = graphDb.execute("MATCH...");
tx.success();
} catch {...}
new Gson().toJson(result);
but what I get is:
java.lang.StackOverflowError
at com.google.gson.internal.$Gson$Types.canonicalize($Gson$Types.java:98)
at com.google.gson.reflect.TypeToken.<init>(TypeToken.java:72)
etc...

The API you show is not the Bolt-Driver, it's the embedded Java-API.
In the bolt-driver you can do
Driver driver = GraphDatabase.driver( "bolt://localhost", AuthTokens.basic( "neo4j", "neo4j" ) );
Session session = driver.session();
StatementResult result = session.run( "MATCH (a:Person) WHERE a.name = 'Arthur' RETURN a.name AS name, a.title AS title" );
while ( result.hasNext() ) {
Record record = result.next();
gson.toJson(record.asMap());
}
session.close();
driver.close();

I am developing an app in flask and need to do the same and then get it into a response but in Python. Im using jsonify instead of gson. Any suggestions??? Code right here:
#concepts_api.route('/concepts', methods=['GET'])
def get_concepts_of_conceptgroup():
try:
_json = request.json
_group_name = _json['group_name']
if _group_name and request.method == 'GET':
rows = concepts_service.get_concepts_of_conceptgroup(_group_name)
resp = jsonify(rows)
resp.status_code = 200
return resp
return not_found()
except:
message = {
'status': 500,
'message': 'Error: Imposible to get concepts of conceptgroup.',
}
resp = jsonify(message)
resp.status_code = 500
return resp

Related

Cannot Insert into sqlite

I'm still learning Ionic and programming in general. I followed a link on the internet and I was able to create the white and read the necessary data, but I am not able to insert data in the created table. Can anyone help me with this?
I follow this tutorial: ionic-sqlite
My code:
getRegiao() { // RegiƵes //
return new Promise<Regiao[]>((resolve, reject) => {
let sql = "SELECT NOM_REGIAO, ID " +
"FROM TB_REGIAO "
this.executeQuery(sql).then(data => {
let regioes = [];
if (data != undefined)
data.forEach(function (row) {
let regiao: Regiao = { nom_regiao: row[0], id: row[1] }
regioes.push(regiao);
});
resolve(regioes);
}).catch(error => {
console.log(error);
});
});
}
addUser() {
let sql = "INSERT INTO TB_USUARIO (EMAIL) VALUES ('BLITCRANK#HOTMAIL.COM')";
// let sql = "SELECT EMAIL FROM TB_USUARIO";
this.executeQuery(sql);
}
executeQuery(sql: string) {
let db: any;
return new Promise<any>((resolve, reject) => {
let xhr = new XMLHttpRequest();
xhr.open('GET', this.dbName, true);
xhr.responseType = 'arraybuffer';
xhr.onload = (e) => {
let uInt8Array = new Uint8Array(xhr.response);
db = new SQL.Database(uInt8Array);
let contents = db.exec(sql);
console.log(contents);
if (contents.length > 0)
resolve(contents[0].values);
else
resolve("query executada sem retorno")
};
xhr.send();
});
}
Please use this plugin , this is the plugin that I am using.
Take note in using execute sql like this db.executeSql('create table danceMoves(name VARCHAR(32))', {})
But rather use this db.executeSql('create table danceMoves(name VARCHAR(32))', [])
I dont know why instead of using object '{}' they replace it as array '[]' I think they forgot to update the documentaion

How do I save a record in Apache Jena?

I want to create a person with nick name jd and e-mail john.doe#provider.com in a persistent Apache Jena database.
I wrote following code:
var dataSet:Dataset? = null
val Dir = "data/MyDataSet"
dataSet = TDBFactory.createDataset(Dir)
dataSet.begin(ReadWrite.WRITE)
val model = dataSet.defaultModel
createPerson("john.doe#provider.com", model, "jd")
dataSet.end()
dataSet.close()
private fun createPerson(email: String, model: Model, nick: String) {
val uuid = UUID.randomUUID()
val uri = "http://mycompany.com/data/p-${uuid}"
val person = model.createResource(uri)
person.addProperty(VCARD.EMAIL, email)
person.addProperty(VCARD.N,
model.createResource()
.addProperty(VCARD.NICKNAME, nick))
}
When I run it, I get no errors.
But when I try to read the data from the file (see code below), the query doesn't find anything.
ds.begin(ReadWrite.READ)
val query = QueryFactory.create("""SELECT ?x
WHERE { ?x <http://www.w3.org/2001/vcard-rdf/3.0#EMAIL> "john.doe#provider.com" }""")
val qexec: QueryExecution
try {
qexec = QueryExecutionFactory.create(query, ds.defaultModel)
val rs = qexec.execSelect()
while (rs.hasNext()) {
val solution = rs.nextSolution()
System.out.println("")
}
}
catch (throwable:Throwable) {
logger.error("", throwable)
}
finally {
ds.end()
}
What's wrong with my code?
You should get a warning at end because you have not committed the transaction.
dataSet.begin(ReadWrite.WRITE)
val model = dataSet.defaultModel
createPerson("john.doe#provider.com", model, "jd")
dataSet.commit()
dataSet.end()

Getting all records from Elasticsearch using Java API

I am trying to get all the records from Elasticsearch using Java API. But I receive the below error
n[[Wild Thing][localhost:9300][indices:data/read/search[phase/dfs]]];
nested: QueryPhaseExecutionException[Result window is too large, from
+ size must be less than or equal to: [10000] but was [10101].
My code is as below
Client client;
try {
client = TransportClient.builder().build().
addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));
int from = 1;
int to = 100;
while (from <= 131881) {
SearchResponse response = client
.prepareSearch("demo_risk_data")
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH).setFrom(from)
.setQuery(QueryBuilders.boolQuery().mustNot(QueryBuilders.termQuery("user_agent", "")))
.setSize(to).setExplain(true).execute().actionGet();
if (response.getHits().getHits().length > 0) {
for (SearchHit searchData : response.getHits().getHits()) {
JSONObject value = new JSONObject(searchData.getSource());
System.out.println(value.toString());
}
}
}
}
Total number of records currently present are 131881 ,so I start with from = 1 and to = 100 and then get 100 records until from <= 131881. Is there are way where I can check get records in set of say 100 until there are no further records in Elasticsearch.
Yes, you can do so using the scroll API, which the Java client also supports.
You can do it like this:
Client client;
try {
client = TransportClient.builder().build().
addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));
QueryBuilder qb = QueryBuilders.boolQuery().mustNot(QueryBuilders.termQuery("user_agent", ""));
SearchResponse scrollResp = client.prepareSearch("demo_risk_data")
.addSort(SortParseElement.DOC_FIELD_NAME, SortOrder.ASC)
.setScroll(new TimeValue(60000))
.setQuery(qb)
.setSize(100).execute().actionGet();
//Scroll until no hits are returned
while (true) {
//Break condition: No hits are returned
if (scrollResp.getHits().getHits().length == 0) {
break;
}
// otherwise read results
for (SearchHit hit : scrollResp.getHits().getHits()) {
JSONObject value = new JSONObject(searchData.getSource());
System.out.println(value.toString());
}
// prepare next query
scrollResp = client.prepareSearchScroll(scrollResp.getScrollId()).setScroll(new TimeValue(60000)).execute().actionGet();
}
}

java neo4j check if a relationship exist

excluse if this is a duplicate, although I did not find the answer so far.
I have an application that creates nodes and relationships via cypher statement against the REST-API. I create relationships with the below code:
public URI createRelationship(GraphNodeTypes sourceType, URI sourceNode,
GraphNodeTypes targetType, URI targetNode,
GraphRelationshipTypes relationshipType, String[] jsonAttributes) {
URI relationShipLocation = null;
String cypherArt = getNodeIdFromLocation(sourceNode)+"-[:"+relationshipType+"]->"+getNodeIdFromLocation(targetNode);
logger.info("creating relationship ({}:{}) -[:{}]-> ({}:{})",
sourceType,
getNodeIdFromLocation(sourceNode),
relationshipType,
targetType,
getNodeIdFromLocation(targetNode));
try {
URI finalUrl = new URI( sourceNode.toString() + "/relationships" );
String cypherStatement = generateJsonRelationship( targetNode,
relationshipType,
jsonAttributes );
logger.trace("sending CREATE RELATIONSHIP cypher as {} to endpoint {}", cypherStatement, finalUrl);
WebResource resource = Client.create().resource( finalUrl );
ClientResponse response = resource
.accept( MediaType.APPLICATION_JSON )
.type( MediaType.APPLICATION_JSON )
.entity( cypherStatement )
.post( ClientResponse.class );
String responseEntity = response.getEntity(String.class).toString();
int responseStatus = response.getStatus();
logger.trace("POST to {} returned status code {}, returned data: {}",
finalUrl, responseStatus,
responseEntity);
// first check if the http code was ok
HttpStatusCodes httpStatusCodes = HttpStatusCodes.getHttpStatusCode(responseStatus);
if (!httpStatusCodes.isOk()){
if (httpStatusCodes == HttpStatusCodes.FORBIDDEN){
logger.error(HttpErrorMessages.getHttpErrorText(httpStatusCodes.getErrorCode()));
} else {
logger.error("Error {} sending data to {}: {} ", response.getStatus(), finalUrl, HttpErrorMessages.getHttpErrorText(httpStatusCodes.getErrorCode()));
}
} else {
JSONParser reponseParser = new JSONParser();
Object responseObj = reponseParser.parse(responseEntity);
JSONObject jsonResponseObj = responseObj instanceof JSONObject ?(JSONObject) responseObj : null;
if(jsonResponseObj == null)
throw new ParseException(0, "returned json object is null");
//logger.trace("returned response object is {}", jsonResponseObj.toString());
try {
relationShipLocation = new URI((String)((JSONObject)((JSONArray)((JSONObject)((JSONArray)((JSONObject)((JSONArray)jsonResponseObj.get("results")).get(0)).get("data")).get(0)).get("rest")).get(0)).get("self"));
} catch (Exception e) {
logger.warn("CREATE RELATIONSHIP statement did not return a self object, returning null -- error was {}", e.getMessage());
relationShipLocation = null;
}
}
} catch (Exception e) {
logger.error("could not create relationship ");
}
return relationShipLocation;
}
private static String generateJsonRelationship( URI endNode,
GraphRelationshipTypes relationshipType, String[] jsonAttributes ) {
StringBuilder sb = new StringBuilder();
sb.append( "{ \"to\" : \"" );
sb.append( endNode.toString() );
sb.append( "\", " );
sb.append( "\"type\" : \"" );
sb.append( relationshipType.toString() );
if ( jsonAttributes == null || jsonAttributes.length < 1 ){
sb.append( "\"" );
} else {
sb.append( "\", \"data\" : " );
for ( int i = 0; i < jsonAttributes.length; i++ ) {
sb.append( jsonAttributes[i] );
if ( i < jsonAttributes.length - 1 ){
// Miss off the final comma
sb.append( ", " );
}
}
}
sb.append( " }" );
return sb.toString();
}
My problem is that I would like to check if a given relationship of given type already exists between two nodes PRIOR creating it.
Can someone tell me, how to query for a relationship???
With nodes I do a MATCH like this:
MATCH cypher {"statements": [ {"statement": "MATCH (p:SOCIALNETWORK {sn_id: 'TW'} ) RETURN p", "resultDataContents":["REST"]} ] }
against the endpoint
http://localhost:7474/db/data/transaction/<NUMBER>
How would I construct the statement to check for a relationship, say between node 6 and 5 or whatever?
Thanks in advance,
Chris
You might want to consider doing this through cypher, and using the MERGE/ON CREATE/ON MATCH keywords.
For example, you could do something like this:
create (a:Person {name: "Bob"})-[:knows]->(b:Person {name: "Susan"});
MATCH (a:Person {name: "Bob"}), (b:Person {name: "Susan"})
MERGE (a)-[r:knows]->(b)
ON CREATE SET r.alreadyExisted=false
ON MATCH SET r.alreadyExisted=true
RETURN r.alreadyExisted;
This MATCH/MERGE query that I provide here will return true or false, depending on whether the relationship already existed or not.
Also, FWIW it looks like the code you're using to accumulate JSON via StringBuilder objects is likely to be ponderous and error prone. There are plenty of good libraries like Google GSON that will do JSON for you, so you can create JSON objects, arrays, primitives, and so on -- then let the library worry about serializing it properly to a string . This tends to make your code a lot cleaner, easier to maintain, and when you mess something up about your JSON formatting (we all do), it's way easier to find than when you accumulate strings like that.
In Java
Relationship getRelationshipBetween(Node n1, Node n2) { // RelationshipType type, Direction direction
for (Relationship rel : n1.getRelationships()) { // n1.getRelationships(type,direction)
if (rel.getOtherNode(n1).equals(n2)) return rel;
}
return null;
}

Neo4j Executing cypher query via rest

Good Morning,
I set up a local Neo4j database and want to model a graph of maven dependencies.
When I execute the following statement via the webconsole, everything works fine:
start root = node(1)
create unique root -[:ROOT]-> (n{groupId:'fancyStuff',artifactId:'somewhat', version:'1.4'})
return n
(note: rootnode is there for debugging purposes, will be replaced by actual structure later)
So, here everything works fine, no matter of how much whitespaces I take or replacing ' with "
In my java application i have the following function:
private static URI getOrCreate(Artifact artifact){
String cypherUri = SERVER_ROOT_URI + "cypher";
String cypherStatement="{\"query\" : \"start x = node(1) " +
"create unique x -[:ROOT]-> (artifact{groupId:\"" + artifact.getGroupID() +
"\", artifactId:\"" + artifact.getArtifactID() +
"\", version: \"" + artifact.getVersion() +
"\"}) return artifact ,\"params\" : {}}";
WebResource resource = Client.create()
.resource( cypherUri );
ClientResponse response = resource.accept( MediaType.APPLICATION_JSON_TYPE )
.type( MediaType.APPLICATION_JSON_TYPE )
.entity( cypherStatement )
.post( ClientResponse.class );
System.out.println( String.format( "POST to [%s], status code [%d]",
cypherUri, response.getStatus() ) );
response.close();
return response.getLocation();
}
so basically I post a json file looking like
{"query" : "start root = node(1) create unique root-[:ROOT]->(artifact{groupId:'{"query" : "start root = node(1) create unique root-[:ROOT]->(artifact{groupId:'lol',artifactId:'somewhat',version:'1.4'}) return artifact","params" : {}}
also no matter what whitespacing or "/' I use I get an http 500 error, saying the first - of the relationship -[:ROOT]-> is invalid.
Posting new nodes directly via
final String nodeEntryPointUri = SERVER_ROOT_URI + "node";
WebResource resource = Client.create().resource( nodeEntryPointUri );
ClientResponse response = resource.accept( MediaType.APPLICATION_JSON_TYPE )
.type( MediaType.APPLICATION_JSON_TYPE )
.entity( /*some json*/)
.post(ClientResponse.class);
(Disclaimer: I will move the params to the right place asap this version works ;) )
I could bet it's some totally trivial error, but I'm staring at this for over half a workday right now and none of my variations want to work.
Would be awesome if somebody knows an answer.
Greetings,
Florian Rohm
The problem is you have invalid JSON. You repeat query twice. If you delete the part between stars, does it work?
**{"query" :
"start root = node(1)
create unique root-[:ROOT]->(artifact{groupId:'**
{"query" : "start root = node(1)
create unique root-[:ROOT]->(artifact{groupId:'lol',artifactId:'somewhat',version:'1.4'})
return artifact",
"params" : {}
}
Ok, i don't know what's different with this statement but this works (I tried the param split in the above code, too):
String cypherUri = SERVER_ROOT_URI + "cypher";
JSONObject jObject = new JSONObject();
try {
Map<String, String> params = new HashMap<String, String>();
params.put("groupId", artifact.getGroupID());
params.put("artifactId", artifact.getArtifactID());
params.put("version", artifact.getVersion());
String query = "start x = node(1) create unique x-[:ROOT]->(n{groupId:{groupId},artifactId:{artifactId},version:{version} }) return n";
jObject.put("query", query);
jObject.put("params", params);
} catch (Exception e) {
e.printStackTrace();
}
WebResource resource = Client.create()
.resource( cypherUri );
ClientResponse response = resource.accept( MediaType.APPLICATION_JSON_TYPE )
.type(MediaType.APPLICATION_JSON_TYPE)
.entity(jObject.toString())
.post(ClientResponse.class);
But anyways, the second attempt is nicer and I won't complain :D
It just bugs me not to know what's going on there...

Categories

Resources