Author Archives: Raghavendar T S

Create Vertex If Does Not Exist – Gremlin Query – Titan

Create Vertex If Does Not Exist - Gremlin Query - Titan

Create Vertex If Does Not Exist – Gremlin Query – Titan

Create Vertex If Does Not Exist

Return a vertex with given label and property. If the vertex does not exist, a new vertex is created and returned to vertexObject.

vertexObject = g.V().hasLabel('VERTEX_LABEL').has('property','value').tryNext().orElseGet{
    graph.addVertex(label, 'VERTEX_LABEL', 'property', 'value');
};

Related Links:

  1. Gremlin Query Language - Create Vertex/Node and Edge/Relationship
  2. Check If Vertex/Edge Exist - Gremlin Query - Titan
  3. Update Properties of Multiple Edges - Gremlin Query - Titan
  4. Update Properties of Multiple Vertices - Gremlin Query - Titan
  5. Create Vertex If Does Not Exist - Gremlin Query - Titan
  6. Query/Access Root Level Properties and Nested Properties of Vertex in Single Query - Gremlin Query - Titan
  7. Update Vertex If Exist - Gremlin Query - Titan
  8. Update Multiple Properties of Vertex - Gremlin Query - Titan
Read More...

Query/Access Root Level Properties and Nested Properties of Vertex in Single Query – Gremlin Query – Titan

Query/Access Root Level Properties and Nested Properties of Vertex in Single Query - Gremlin Query - Titan

Query/Access Root Level Properties and Nested Properties of Vertex in Single Query – Gremlin Query – Titan

Gremlin Query:

g.V().hasLabel('TEST_LABEL').has('_id','xxxxxxxxxxxxxx').as('v')
.select('v').valueMap(true).as('properties')
.select('v').properties('nestedPropertiesKey').valueMap().as('nestedProperties')
.select('properties','nestedProperties')

Sample Response:

{
     "properties":{
          "attr1":"value1"
     },
     "nestedProperties":{
          "nestedAttr1":"nestedValue1"
     }

}

Related Links:

  1. Gremlin Query Language - Create Vertex/Node and Edge/Relationship
  2. Check If Vertex/Edge Exist - Gremlin Query - Titan
  3. Update Properties of Multiple Edges - Gremlin Query - Titan
  4. Update Properties of Multiple Vertices - Gremlin Query - Titan
  5. Create Vertex If Does Not Exist - Gremlin Query - Titan
  6. Query/Access Root Level Properties and Nested Properties of Vertex in Single Query - Gremlin Query - Titan
  7. Update Vertex If Exist - Gremlin Query - Titan
  8. Update Multiple Properties of Vertex - Gremlin Query - Titan
Read More...

Java – Break Filter Chain and Return Custom POJO Response from Servlet Filter

Java - Break Filter Chain and Return Custom POJO Response from Servlet Filter

Java – Break Filter Chain and Return Custom POJO Response from Servlet Filter

Sample Servlet Filter :

package com.application.test.security;

import com.application.test.response.ErrorResponse;
import com.fasterxml.jackson.databind.ObjectMapper;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class TestFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,ServletException {

        if(ANY CONDITION){
            //ANY POJO CLASS
            ErrorResponse errorResponse = new ErrorResponse();
            errorResponse.setCode(401);
            errorResponse.setMessage("Unauthorized Access");
   
            byte[] responseToSend = restResponseBytes(errorResponse);
            ((HttpServletResponse) response).setHeader("Content-Type", "application/json");
            ((HttpServletResponse) response).setStatus(401);
            response.getOutputStream().write(responseToSend);
            return;
        }

        //ANY OTHER BUSINESS LOGIC
        chain.doFilter(request, response);
    }

    @Override
    public void destroy() {

    }

    private byte[] restResponseBytes(ErrorResponse eErrorResponse) throws IOException {
        String serialized = new ObjectMapper().writeValueAsString(eErrorResponse);
        return serialized.getBytes();
    }
}

Gremlin Query Language – Create Vertex/Node and Edge/Relationship

Gremlin Query Language - Create Vertex/Node and Edge/Relationship

Gremlin Query Language – Create Vertex/Node and Edge/Relationship

  1. Open Gremlin Console
  2. Initial command to work/test with inmemory storage
graph = TitanFactory.build().set('storage.backend','inmemory').open()
g=graph.traversal(standard())

Example 1:

Create Vertex. Label property is optional. The below queries will return vertex references.

graph.addVertex('property1','value1','property2','value2');
OR
graph.addVertex(label,'USER','property1','value1','property2','value2');
OR
graph.addVertex(T.label,'USER','property1','value1','property2','value2');
OR
graph.addVertex(T.label,'USER').property('property1','value1').property('property2','value2');
OR
graph.addVertex('LABEL_NAME');

Example 2:

Create Edge with/without properties using system generated unique ID

g.V(123).next().addEdge('EDGE_LABEL',g.V(456).next())
OR
g.E(g.V(123).next().addEdge('EDGE_LABEL',g.V(456).next())).property('property1','value1').property('property2','value2')

Example 3:

Create Edge with/without properties using custom filter properties.

g.E(g.V().hasLabel('USER').has('property','value').next().addEdge('FRIEND',g.V().hasLabel('USER').has('property','value').next())).property('property1','value1').property('property2','value2')

Example 4:

Get vertices with only required properties

g.V().valueMap('property1','property2')

Related Links:

  1. Gremlin Query Language - Create Vertex/Node and Edge/Relationship
  2. Check If Vertex/Edge Exist - Gremlin Query - Titan
  3. Update Properties of Multiple Edges - Gremlin Query - Titan
  4. Update Properties of Multiple Vertices - Gremlin Query - Titan
  5. Create Vertex If Does Not Exist - Gremlin Query - Titan
  6. Query/Access Root Level Properties and Nested Properties of Vertex in Single Query - Gremlin Query - Titan
  7. Update Vertex If Exist - Gremlin Query - Titan
  8. Update Multiple Properties of Vertex - Gremlin Query - Titan
Read More...

Neo4j Query Tutorial – MATCH and MERGE

Neo4j Query Tutorial - MATCH and MERGE

Neo4j Query Tutorial – MATCH and MERGE

Example 1:

Match two nodes and create a relationship between them.

MATCH (person1:LABEL{id:'1234'}),(person2:LABEL{id:'5678'})
MERGE (person1)-[r:FRIEND]-(person2)

Example 2:

Match two nodes and create/update a relation between them. Optionally set properties to the nodes or relationships with ON CREATE SET or ON MATCH SET. Use r+={property:’value’} to add properties or update existing properties.

MATCH (user1:USER{id:'123'}),(user2:USER{id:'456'}) 
MERGE (user1)-[r:FRIEND]-(user2)
ON CREATE SET
    r={createdDate:'01-04-2016',updatedDate:'01-04-2016'}
ON MATCH SET 
    r={createdDate:'01-04-2016',updatedDate:'01-04-2016'}
RETURN user1,user2

Example 3:

MATCH a node and pass the required node or relationship to next level of processing using WITH option.

MATCH (user1:USER{id:'123'})
WITH user1
MATCH (user2:USER{id:'456'})
WITH user1,user2
MERGE (user1)-[r:FRIEND]-(user2)
ON CREATE SET
    r={createdDate:'01-04-2016',updatedDate:'01-04-2016'}
ON MATCH SET 
    r={createdDate:'01-04-2016',updatedDate:'01-04-2016'}
RETURN user1,user2,r

Example 4:

Update out going releationships for a node

MATCH (user1:USER{id:'123'})-[r]-()
SET 
    r.property='value'
RETURN user1

Example 5:

Use a property value of a one object/node/relationship with another object/node/relationship.

MATCH (user1:LABEL{id:'123'})-[r]-(user2:LABEL{property:user1.propertyValue})
SET 
   r={someProperty:'value'}
RETURN user1.id,r.id,user2.id

Example 6:

MATCH (n:LABEL) WHERE n.property='value'
RETURN n