Connecting JanusGraph with springboot - gremlin

Connect JanusGraph using Springboot and perform some CRUD operations. Gremlin Server is started at 8182.
Tried with providing absolute path to read properties file.
is anything wrong with code or property file? need to configure anything in gremlin server yaml file?
getting error
"java.lang.ClassNotFoundException: org.apache.tinkerpop.gremlin.process.traversal.step.sideEffect.IoStep"
java.lang.ClassNotFoundException: org.apache.tinkerpop.gremlin.process.traversal.step.filter.NoneStep
Dependencies:
<dependency>
<groupId>org.janusgraph</groupId>
<artifactId>janusgraph-core</artifactId>
<version>0.5.2</version>
</dependency>
<dependency>
<groupId>org.janusgraph</groupId>
<artifactId>janusgraph-berkeleyje</artifactId>
<version>0.6.1</version>
</dependency>
<dependency>
<groupId>org.apache.tinkerpop</groupId>
<artifactId>gremlin-driver</artifactId>
<version>3.2.9</version>
</dependency>
Graph class
#RestController
public class GraphT {
#GetMapping("/test")
String all() {
Graph graph = JanusGraphFactory.open("conf/janusgraph-berkeleyje.properties");
GraphTraversalSource g = graph.traversal();
//Add one vertex
g.addV("person").property("name", "bob").property("age", 27).iterate();
//Confirm change
g.tx().commit();
//Try issuing a query to retrieve the value
List<Map<String, Object>> result = g.V().valueMap("name", "age").toList();
//The return value is[{name: ["bob"], age: [27]}]Should be returned
for(Map<String, Object> vertex : result){
//vertex is{name:["bob"], age: [27]}Should be
ArrayList<String> names = (ArrayList<String>)vertex.get("name"); // ["bob"]Get
ArrayList<Integer> ages = (ArrayList<Integer>)vertex.get("age"); // [27]Get
String name = names.get(0); //"bob"Get
Integer age = ages.get(0); //Get 27
System.out.printf("name: %s, age: %s\n", name, age);
}
//Try issuing a query to retrieve the number of vertices
Long count = g.V().count().next();
System.out.printf("vertex count is %d\n", count);
try {
graph.close();
} catch (Exception ex) {
//deal with it
}
return " some result ";
}
}
3. config file
gremlin.graph=org.janusgraph.core.JanusGraphFactory
storage.backend=berkeleyje
storage.directory=../db/berkeley

I see two different problems in the setup you described:
Incompatible library versions
You are mixing versions of JanusGraph and TinkerPop that are not compatible with another.
If you want to use version 0.6.1 of janusgraph-berkeleyje, then you should also use the same version for janusgraph-core. The newest version is 0.6.2 right now by the way so you may just want to use that version.
But more importantly: Versions 0.6.* of JanusGraph are compatible with versions 3.5.* of TinkerPop, but you are using version 3.2.9 of gremlin-driver.
The JanusGraph release notes show which versions each JanusGraph release is compatible with. For 0.6.2 of JanusGraph, compatibility is tested with TinkerPop version 3.5.3, so I would use that version.
JanusGraph Server vs Embedded JanusGraph
Apart from that, you say that you have started Gremlin Server, but then you are not connecting to that server in your Java code, but instead you are starting a local JanusGraph instance which directly uses Berkeley via JanusGraphFactory.open(). This means that your Java application will not use the started Gremlin Server at all.
You should decide whether you want to run JanusGraph as a server to which you can connect from various applications or using JanusGraph as a library which directly uses the storage backend.
You can read more about this difference in the section Deployment Scenarios of the JanusGraph documentation. Embedded JanusGraph is the scenario where JanusGraph is used as a library.
If you want to use JanusGraph Server (which is the Gremlin Server configured for JanusGraph) and connect to that remotely from your Java application, then the section Connecting from Java of the documentation explains how that can be done.

Related

Updating from Document DB SDK (2.7.0) to Cosmos SDK (3.12.0)

We are in a process of updating our service code to using Cosmos SDK 3.12.0 from DocumentDB SDK 2.7.0. Since a change will likely be huge, we would like to do it incrementally that will result in our service using both SDKs to access the same databases (one executable loading assemblies of both SDKs). Please let us know if that is supported or if you see any issues in doing so. Also, I couldn’t figure out how to do things in same ways with Cosmos SDK (e.g. specifying “enable cross partition query” in querying items – the query method in 2.7.0 takes FeedOptions as a parameter whereas the new one in 3.12.0 doesn’t). I found this wiki and some sample code but if you have more info/guidelines for converting from Document SDK to Cosmos SDK, please let me know.
Yes,you can use both DocumentDB sdk and cosmos sdk access the same databases.
In Cosmos SDK 3.12.0,there is no need to set EnableCrossPartitionQuery true.just do something like this is ok:
QueryDefinition queryDefinition = new QueryDefinition("select * from c");
FeedIterator<JObject> feedIterator = container.GetItemQueryIterator<JObject>(queryDefinition);
while (feedIterator.HasMoreResults)
{
foreach (var item in await feedIterator.ReadNextAsync())
{
{
Console.WriteLine(item.ToString());
}
}
}

MySql.Data can't connect to AWS Aurora

I have a really weird connection problem between C# .net core and AWS Aurora Serverless (MySql). I have a really simple web API that I'm using for testing. One of my endpoints is really simple to get the first row of one table:
string cnString = "myConnectionString";
string returnData = "";
using (MySqlConnection cnData = new MySqlConnection(cnString))
{
using (MySqlCommand cmdData = new MySqlCommand("SELECT userName FROM users LIMIT 1", cnData))
{
await cnData.OpenAsync();
await cmdData.ExecuteScalarAsync();
returnData = (string)cmdData.ExecuteScalar();
}
}
return Ok(returnData);
I have the MySql.Data NuGet package installed. When I deploy the package, I get an error: Unable to connect to to any of the specified MySQL hosts. (Sequence contains more than one matching element)
If I uninstall the MySql.Data package and install the MySqlConnector package, then there is no error and everything works properly. There are no changes to the code or connection string. The only thing I change is the NuGet package.
I don't think it matters, but my app is deployed to an Amazon Linux EC2 instance.
Does anyone know why the MySql.Data package doesn't work? For my deployment, I need that package and I can't use the MySqlConnector package.
If anyone else is having the same problem, there is an issue in the MySql.Data package.
I suspect that it is related to this known bug:
MySql #97448
The way that I read it is this: the Aurora Cluster uses multiple IP addresses, so the Sequence contains more than 1 element error is thrown. (The multiple elements are IP addresses in the TcpStream().
There is no fix that I saw, but I found a workaround that downgrading the package to 8.0.16 fixes the problem.

Creating Connection Pool and using the connection pool in JanusGraph

I am using JanusGraph. How do I create a connection pool while connecting to JanusGraph remotely and then using the pool to borrow the connection?
right now I am doing something like
private static void init() {
String uri = "localhost";
int poolSize = 5;
graph = JanusGraphFactory.open("inmemory");
cluster = Cluster.build()
.addContactPoint(uri)
.port(8182)
.serializer(new GryoMessageSerializerV1d0(GryoMapper.build().addRegistry(JanusGraphIoRegistry.getInstance())))
.maxConnectionPoolSize(poolSize)
.minConnectionPoolSize(poolSize)
.create();
gts = graph
.traversal()
.withRemote(DriverRemoteConnection.using(cluster));
}
this init method is initialized once. and then anyone who requires connection simple calls the below method
public GraphTraversalSource getConnection() {
return gts.clone();
}
Please note that withRemote() method is being deprecated now. I am not sure am I doing it correctly?
I think you're confusing some concepts. You only need to use the TinkerPop driver (i.e. Cluster) if you are connecting to a Graph instance remotely. In your case you're creating your JanusGraph instance locally, so you can simply do graph.traversal() and start writing Gremlin. If you hosted your JanusGraph instance in Gremlin Server on the other hand, then you would need to use the withRemote() option. As you mention withRemote() in the fashion you are calling it is deprecated, but the javadoc mentions the new method which can also be found in the documentation:
import static org.apache.tinkerpop.gremlin.process.traversal.AnonymousTraversalSource.traversal;
GraphTraversalSource g = traversal().withRemote(DriverRemoteConnection.using(cluster));
To understand all the different options for connecting to a Graph instance I'd suggest reading this section of TinkerPop's Reference Documentation.

How to create primary index in my Couchbase server use query?

I have written the following code to create primary index in Couchbase server with my Symfony project.
// Establish connection
$cluster = new \CouchbaseCluster('http://ec2-ip_number.compute-1.amazonaws.com:8091'); // http://127.0.0.1:8091
$bucket = $cluster->openBucket("default");
$bucket->enableN1ql(array('http://ec2-ip_number.compute-1.amazonaws.com:8091'));
// Execute query
$query = 'CREATE PRIMARY INDEX `default-primary-index` ON `default` USING GSI';
$queryNql = \CouchbaseN1qlQuery::fromString($query);
$bucket->query($queryNql);
My couchbase cluster is created in amazon aws.
After run the above code, it shows me the error:
"LCB_NETWORK_ERROR: Generic network failure. Enable detailed error codes (via LCB_CNTL_DETAILED_ERRCODES, or via detailed_errcodes in the connection string) and/or enable logging to get more information
500 Internal Server Error - CouchbaseException"
I have tried it many times to solve this issue, but couldn't able to get any solution.
Can you run the CREATE INDEX command from the web-console (query-tab). Make sure there are no connectivity/network issues between client & server, or between various Couchbase services.
Did you retry after enabling logging?? What error do you see in logs?
Provide more details on couchbase version and AWS/cluster setup?
-Prasad

Problems importing derby drivers in glassfish

Good day to everyone,
I'm having some problems making Glassfish 3.1.2 seeing some imported jars. Because this i my grad project i have constraints. I cannot use anything else than Glassfish, Netbeans and i cannot use Maven, Hibernate, Ear or whathever.
I'm working with the embedded Derby driver. (I can use another DB, but in my trials the problem remains).
I've written some plain java code to connect to the DB:
try{
Connection c = DriverManager.getConnection(URL, USER, PWD);
Statement st = c.createStatement();
st.executeUpdate("CREATE TABLE Users" +
" (ACCOUNT VARCHAR(20) PRIMARY KEY," +
" PWD VARCHAR(20) NOT NULL," +
" USERTYPE INTEGER NOT NULL)");
...
}
catch (SQLException x){
System.out.println(x);
}
It's ugly, it doesn't use connection pools, it works. I'll use them later.
Obviously i had to add derbyclient.jar to the libs.
The problem arises when i try to use this method inside a servlet:
protected void processRequest(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
try{
DriverManager.registerDriver(org.apache.derby.jdbc.ClientDriver);
Connection c = DriverManager.getConnection(dbUsr, pwd, url);
Statement st = c.createStatement();
ResultSet rs = st.executeQuery(query);
...
}catch (SQLException e){
ServletException e1 = new ServletException(e.getMessage());
throw e1;
}
I cannot compile the servlet because "Package org.apache.derby.jdbc does not exists".
I can provide some more informations (20+ hours of reading and unsuccesful trials):
The servlet and the java code are in the same project, so derbyclient.jar should be seen by both of them.
When i write org.apache.berby netbeans shows me the package navigator for auto-completion, so somewhere it knows that there is a jar.
I tried to put derbyclient.jar inside:
Glassfish/glassfish/domains/domain1/lib
Glassfish/glassfish/domains/domain1/lib/ext
Glassfish/glassfish/lib
Tried without driver registration, the java code works without it, but i get "javax.servlet.ServletException: No suitable driver found for app". I find it reasonable.
I am at my wits end. Does someone care to help?
EDIT:
Yesterday I got news from the teacher: she blames Netbeans IDE for losing track of the jars. It seems to be a known bug.
I was warmly encouraged to switch to a tomcat server. I'll surely try it, bu i'll keep working to get this problem solved. Any other good ideas?
"No suitable driver found" usually means that the JDBC URL you've supplied to connect has incorrect syntax. Or the driver isn't loaded at all.
You have to load driver using Class.forName before connecting to DB.
URL for Derby is:
Class.forName("org.apache.derby.jdbc.ClientDriver");
More details on Derby check out the documentation
Also check that you have derby.jar in your classpath. I would suggest to place derby.jar at physical location to /WEB-INF/lib directory of your project.Then IDE will take care for the rest.
You should (in general) not register a driver yourself with the DriverManager That is the responsibility of the Driver implementation. For JDBC 3.0 and earliers (Java 5 and before) you need to load the class to get it to register itself:
Class.forName("org.apache.derby.jdbc.ClientDriver")
When the class is loaded, the driver registers itself with the DriverManager.
For JDBC 4.0 compliant drivers (Java 6 and 7) you don't even need to do that as JDBC 4.x uses the ServiceLoader mechanism and each compliant driver will lists its Driver implementations in a META-INF/services/java.sql.Driver file in its jar-file.
So in general you should NOT call 'DriverManager.registerDriver(..)' yourself. If for some reason you do need the register the driver yourself in code (eg because the driver actually doesn't register itself with the DriverManager automatically), then make sure that the actual driver is also on your compilation path (build path), and not just in the lib folder of your application server (this seems to be your actual problem).
Also remember that you will need to register an instance of Driver, not a class. So you will need to instantiate it:
DriverManager.registerDriver(new org.apache.derby.jdbc.ClientDriver());

Resources