Skip to content

Instantly share code, notes, and snippets.

@jagchat
Last active March 3, 2022 20:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jagchat/5a801d4b20d6c6e295c6489dbb680be5 to your computer and use it in GitHub Desktop.
Save jagchat/5a801d4b20d6c6e295c6489dbb680be5 to your computer and use it in GitHub Desktop.
const gremlin = require('gremlin');
const DriverRemoteConnection = gremlin.driver.DriverRemoteConnection;
const Graph = gremlin.structure.Graph;
dc = new DriverRemoteConnection('wss://mapping-graph-db.cluster-cgm0q33taffj.us-east-2.neptune.amazonaws.com:8182/gremlin', {});
const graph = new Graph();
const g = graph.traversal().withRemote(dc);
//const handler = async (event) => {
const addItem = async () => {
let result = await g.addV('person').property('name', 'stephen').next();
return {
statusCode: 201,
body: JSON.stringify({ message: "Testing Gremlin!", data: result }),
};
}
addItem().then((data) => {
dc.close();
console.log(data);
});
const gremlin = require('gremlin');
const DriverRemoteConnection = gremlin.driver.DriverRemoteConnection;
const Graph = gremlin.structure.Graph;
dc = new DriverRemoteConnection('wss://mapping-graph-db.cluster-cgm0q33taffj.us-east-2.neptune.amazonaws.com:8182/gremlin', {});
const graph = new Graph();
const g = graph.traversal().withRemote(dc);
const saveData = async () => {
const { t: { id } } = gremlin.process;
let result1 = await g.addV('sor').property(id, 'tcp').property('name', 'TCP').next();
let result2 = await g.addV('sor-tenant').property(id, 'tcp-tenant-abc').property('name', 'abc').next();
let result3 = await g.
V('tcp').as('a').
V('tcp-tenant-abc').as('b').
addE('contains-tenant').
from_('a').to('b').
property(id, "e1").next();
return {
statusCode: 201,
body: JSON.stringify({ message: "Testing Gremlin!", data: [result1, result2, result3] }),
};
}
const saveTransaction = async () => {
const { t: { id } } = gremlin.process;
let result = await g
.addV('sor').property(id, 'tcp').property('name', 'TCP').as('a')
.addV('sor-tenant').property(id, 'tcp-tenant-abc').property('name', 'abc').as('b')
.addE('contains-tenant').
from_('a').to('b').
property(id, "e1").iterate();
return {
statusCode: 201,
body: JSON.stringify({ message: "Testing Gremlin!", data: [result] }),
};
}
//saveTransaction().then((data) => {
saveData().then((data) => {
dc.close();
console.log(data);
});
const gremlin = require('gremlin');
const DriverRemoteConnection = gremlin.driver.DriverRemoteConnection;
const Graph = gremlin.structure.Graph;
dc = new DriverRemoteConnection('wss://mapping-graph-db.cluster-cgm0q33taffj.us-east-2.neptune.amazonaws.com:8182/gremlin', {});
const graph = new Graph();
const g = graph.traversal().withRemote(dc);
// // FROM: https://tinkerpop.apache.org/docs/current/reference/#gremlin-javascript-transactions
// // DOESN'T WORK
// const doIt = async () => {
// const tx = g.tx(); // create a Transaction
// const gtx = tx.begin();
// return Promise.all([
// gtx.addV("person").property("name", "jorge").iterate(),
// gtx.addV("person").property("name", "josh").iterate()
// ])
// }
const addItems = async (items) => {
let promiseAry = [];
const { t: { id } } = gremlin.process;
for (const item of items) {
promiseAry.push(await g.addV(item.label).property(id, item.id).property('name', item.name).next());
}
return Promise.all(promiseAry);
}
const items = [
{ id: "tcp", name: "TCP", label: "sor" },
{ id: "tcp-tenant-abc", name: "abc", label: "sor-tenant" },
{ id: "tcp-tenant-xyz", name: "xyz", label: "sor-tenant" },
{ id: "paycor", name: "Paycor", label: "ext-system" },
{ id: "adp", name: "ADP", label: "ext-system" }
];
//doIt().then(() => { //DOESN'T WORK
addItems(items).then(() => {
dc.close();
console.log("DONE!")
}).catch(err => {
dc.close();
console.log("ERROR!")
console.log(err);
});
static void Main(string[] args)
{
var endpoint = "mapping-graph-db.cluster-cgm0q33taffj.us-east-2.neptune.amazonaws.com";
var gremlinServer = new GremlinServer(endpoint, 8182, enableSsl: true);
var gremlinClient = new GremlinClient(gremlinServer);
var remoteConnection = new DriverRemoteConnection(gremlinClient, "g");
var g = AnonymousTraversalSource.Traversal().WithRemote(remoteConnection);
g.AddV("Person").Property("Name", "Justin").Iterate();
g.AddV("Custom Label").Property("name", "Custom id vertex 1").Iterate();
g.AddV("Custom Label").Property("name", "Custom id vertex 2").Iterate();
var output = g.V().Limit<Vertex>(3).ToList();
foreach (var item in output)
{
Console.WriteLine(item);
}
remoteConnection.Dispose();
Console.ReadLine();
}
const gremlin = require('gremlin');
const DriverRemoteConnection = gremlin.driver.DriverRemoteConnection;
const Graph = gremlin.structure.Graph;
dc = new DriverRemoteConnection('wss://mapping-graph-db.cluster-cgm0q33taffj.us-east-2.neptune.amazonaws.com:8182/gremlin', {});
const graph = new Graph();
const g = graph.traversal().withRemote(dc);
g.V().count().next().
then(data => {
console.log(data);
dc.close();
}).catch(error => {
console.log('ERROR', error);
dc.close();
});
// g.V().values('name').toList().
// then(data => {
// console.log(data);
// dc.close();
// }).catch(error => {
// console.log('ERROR', error);
// dc.close();
// });
const gremlin = require('gremlin');
const DriverRemoteConnection = gremlin.driver.DriverRemoteConnection;
const Graph = gremlin.structure.Graph;
dc = new DriverRemoteConnection('wss://mapping-graph-db.cluster-cgm0q33taffj.us-east-2.neptune.amazonaws.com:8182/gremlin', {});
const graph = new Graph();
const g = graph.traversal().withRemote(dc);
//g.V('88bf9723-6efe-18e0-31d2-f302f14e9640').drop().next().then((data) => {
// dc.close();
// console.log(data);
//});
const gremlin = require('gremlin');
const DriverRemoteConnection = gremlin.driver.DriverRemoteConnection;
const Graph = gremlin.structure.Graph;
dc = new DriverRemoteConnection('wss://mapping-graph-db.cluster-cgm0q33taffj.us-east-2.neptune.amazonaws.com:8182/gremlin', {});
const graph = new Graph();
const g = graph.traversal().withRemote(dc);
g
.E('e1').aggregate('x')
.V('tcp').aggregate('x')
.V('tcp-tenant-abc').aggregate('x')
.select('x').unfold().drop()
.next().then((data) => {
dc.close();
console.log(data);
});
const gremlin = require('gremlin');
const DriverRemoteConnection = gremlin.driver.DriverRemoteConnection;
const Graph = gremlin.structure.Graph;
dc = new DriverRemoteConnection('wss://mapping-graph-db.cluster-cgm0q33taffj.us-east-2.neptune.amazonaws.com:8182/gremlin', {});
const graph = new Graph();
const g = graph.traversal().withRemote(dc);
const fetchItems = async () => {
const data = await g.V().toList();
for (const v of data) {
const _properties = await g.V(v.id).properties().toList();
console.log(`${v.id} - ${v.label} - ${_properties}`);
}
};
fetchItems().then(() => {
dc.close();
});
const gremlin = require('gremlin');
const DriverRemoteConnection = gremlin.driver.DriverRemoteConnection;
const Graph = gremlin.structure.Graph;
dc = new DriverRemoteConnection('wss://mapping-graph-db.cluster-cgm0q33taffj.us-east-2.neptune.amazonaws.com:8182/gremlin', {});
const graph = new Graph();
const g = graph.traversal().withRemote(dc);
// // FROM: https://tinkerpop.apache.org/docs/current/reference/#gremlin-javascript-transactions
// // DOESN'T WORK
// const doIt = async () => {
// const tx = g.tx(); // create a Transaction
// const gtx = tx.begin();
// return Promise.all([
// gtx.addV("person").property("name", "jorge").iterate(),
// gtx.addV("person").property("name", "josh").iterate()
// ])
// }
const addVertices = async (items) => {
let promiseAry = [];
const { t: { id } } = gremlin.process;
for (const item of items) {
let newItem = g.addV(item.label);
Object.keys(item).forEach(key => {
switch (key) {
case "id":
//system prop
newItem.property(id, item.id);
break;
case "label":
//it was already added. don't do any
break;
default:
newItem.property(key, item[key]);
}
});
promiseAry.push(await newItem.next());
}
return Promise.all(promiseAry);
}
const addEdges = async (items) => {
let promiseAry = [];
const { t: { id } } = gremlin.process;
for (const item of items) {
let newItem = g.
V(item.from).as('a').
V(item.to).as('b').
addE(item.label).
from_('a').to('b').
property(id, item.id);
//let newItem = g.V(item.from).as('a').addE(item.label).to(item.to);
Object.keys(item).forEach(key => {
switch (key) {
case "id", "label", "from", "to":
//it was already added. don't do any
break;
default:
newItem.property(key, item[key]);
}
});
promiseAry.push(await newItem.next());
}
return Promise.all(promiseAry);
}
const vertices = [
{ id: "tcp", name: "TCP", label: "sor" },
{ id: "tcp-tenant-abc", name: "abc", label: "sor-tenant" },
{ id: "tcp-tenant-xyz", name: "xyz", label: "sor-tenant" },
{ id: "paycor", name: "Paycor", label: "ext-system" },
{ id: "bcbs", name: "BCBS", label: "ext-system" },
{ id: "tcp-tenant-abc-employee-101", recordId: "101", label: "employee" },
{ id: "tcp-tenant-abc-employee-102", recordId: "102", label: "employee" },
{ id: "tcp-tenant-xyz-employee-201", recordId: "201", label: "employee" },
{ id: "tcp-tenant-abc-jc-1001", recordId: "1001", label: "jobcode" },
{ id: "tcp-tenant-abc-jc-1002", recordId: "1002", label: "jobcode" },
{ id: "paycor-p101", recordId: "p101", label: "ext-record" },
{ id: "paycor-p102", recordId: "p102", label: "ext-record" },
{ id: "paycor-p201", recordId: "p201", label: "ext-record" },
{ id: "bcbs-b101", recordId: "b101", label: "ext-record" },
{ id: "bcbs-b102", recordId: "b102", label: "ext-record" },
{ id: "bcbs-b201", recordId: "b201", label: "ext-record" }
];
const edges = [
{ id: "e1", from: "tcp", to: "tcp-tenant-abc", label: "contains-tenant" },
{ id: "e2", from: "tcp", to: "tcp-tenant-xyz", label: "contains-tenant" },
{ id: "e3", from: "tcp-tenant-abc", to: "tcp-tenant-abc-employee-101", label: "contains-entity" },
{ id: "e4", from: "tcp-tenant-abc", to: "tcp-tenant-abc-employee-102", label: "contains-entity" },
{ id: "e5", from: "tcp-tenant-xyz", to: "tcp-tenant-xyz-employee-201", label: "contains-entity" },
{ id: "e6", from: "tcp-tenant-abc", to: "tcp-tenant-abc-jc-1001", label: "contains-entity" },
{ id: "e7", from: "tcp-tenant-abc", to: "tcp-tenant-abc-jc-1002", label: "contains-entity" },
{ id: "e8", from: "paycor", to: "paycor-p101", label: "contains-ext-entity" },
{ id: "e9", from: "paycor", to: "paycor-p102", label: "contains-ext-entity" },
{ id: "e10", from: "paycor", to: "paycor-p201", label: "contains-ext-entity" },
{ id: "e11", from: "bcbs", to: "bcbs-b101", label: "contains-ext-entity" },
{ id: "e12", from: "bcbs", to: "bcbs-b102", label: "contains-ext-entity" },
{ id: "e13", from: "bcbs", to: "bcbs-b201", label: "contains-ext-entity" },
{ id: "e14", from: "paycor-p101", to: "tcp-tenant-abc-employee-101", label: "belongs-to" },
{ id: "e15", from: "paycor-p102", to: "tcp-tenant-abc-employee-102", label: "belongs-to" },
{ id: "e16", from: "paycor-p201", to: "tcp-tenant-xyz-employee-201", label: "belongs-to" },
{ id: "e17", from: "bcbs-b101", to: "tcp-tenant-abc-employee-101", label: "belongs-to" },
{ id: "e18", from: "bcbs-b102", to: "tcp-tenant-abc-employee-102", label: "belongs-to" },
{ id: "e19", from: "bcbs-b201", to: "tcp-tenant-xyz-employee-201", label: "belongs-to" },
{ id: "e20", from: "tcp-tenant-abc-employee-101", to: "paycor-p101", label: "maps-to" },
{ id: "e21", from: "tcp-tenant-abc-employee-102", to: "paycor-p102", label: "maps-to" },
{ id: "e22", from: "tcp-tenant-xyz-employee-201", to: "paycor-p201", label: "maps-to" },
{ id: "e23", from: "tcp-tenant-abc-employee-101", to: "bcbs-b101", label: "maps-to" },
{ id: "e24", from: "tcp-tenant-abc-employee-102", to: "bcbs-b102", label: "maps-to" },
{ id: "e25", from: "tcp-tenant-xyz-employee-201", to: "bcbs-b201", label: "maps-to" }
];
//doIt().then(() => { //DOESN'T WORK
addVertices(vertices).then(() => {
console.log("---Vertices added!")
addEdges(edges).then(() => {
dc.close();
console.log("---Edges added!")
}).catch(err => {
dc.close();
console.log("ERROR adding Edges!")
console.log(err);
});
}).catch(err => {
dc.close();
console.log("ERROR adding Vertices!")
console.log(err);
});
const { t: { id } } = gremlin.process;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment