JanusGraph Java unable to add vertex/edge - java

I've been trying to create interact with my JanusGraph setup in docker. But after many tries I still don't succeed.
How I connect to JG.
public boolean connect() {
try {
graph = traversal().withRemote("path/to/janusgraph-cql-lucene-server.properties");
return true;
} catch (Exception e) {
log.error("Unable to create connection with graph", e);
return false;
}
}
How I try to add a vertex. It looks like this doesn't do anything.
GraphTraversal<Vertex, Vertex> yt = graph.addV("link")
.property("url", "https://www.youtube.com/123")
.property("page_type", "contact");
GraphTraversal<Vertex, Vertex> fb = graph.addV("link")
.property("url", "https://www.facebook.com/456");
graph.tx().commit();
I've added a node with the gremlin console. This works, so the setup is not invalid or something like that. And when I fetch all nodes I in my application get a valid response.
System.out.println(graph.V().hasLabel("link").count().next()); //returns 1 (the node I added manually)
My assumptions:
Setup is alright because it works in the gremlin console
connection
connection must be alright because the initialization doesn't throw an exception and we get a valid count response.
The only thing I'm not sure about is if there's a transaction commit that I am missing. I didn't find any other than graph.tx().commit();
Could you please help me and tell me what I am doing wrong?

The GraphTraversal object is only a "plan" to be carried out. To have it take effect, you need a closing method like next, toList, etc., like you did for the count.
The confusion probably arose from the fact that the gremlin console automatically keeps nexting the traversal a configured number of times.

Related

Updating site property make all pages vanish (Sakai 2.9.3)

I'm updating my a site property through a job. The job runs ok, it does the work right and updates the property I want it to update properly and never failed.
But, when I save the site, it mess my pages, making them vanish. Worst, it don't always happen #.# what is driving me crazy! =(
I'm sure the job is running right because once I get all pages back (through worksite setup and site editor) thing get working all. Here the method where I update the property (I have already tried both options of property edition ways:
#Override
public void updateStringProperty(Site site, String name, String value) {
try {
// ResourcePropertiesEdit rpe = site.getPropertiesEdit();
// rpe.addProperty(name, value);
ResourceProperties rp = site.getProperties();
rp.addProperty(name, value);
siteService.save(site);
} catch (IdUnusedException e) {
e.printStackTrace();
} catch (PermissionException e) {
e.printStackTrace();
}
}
It's written on the proxy layer, in which my application talks with Sakai interfaces to read/write data onto Sakai databse (You can see the instance of the site come from above) and it's how I get the list of sites to apply changes (also sakai proxy layer):
#Override
public List<Site> getReadInWebSites(Long course) {
Map<String, String> m = new HashMap<String, String>();
m.put(Property.COURSE.getName(), Long.toString(course));
m.put(Property.COURSEFINISHED.getName(), Boolean.toString(false));
return new ArrayList<Site>(siteService.getSites(SelectionType.ANY,
null, null, m, SortType.CREATED_BY_ASC, null));
}
Thanks in advance!
EDIT: New info, when I try to access the site (before rebuild its structure through worksite setup, I get this Warn in the log:
org.sakaiproject.portal.charon.site.DefaultSiteViewImpl - Failed to set canAddSite for current user. Defaulting to false ...
This is because when Sakai Sites are loaded with getSites they are lazy objects and don't have all their member variables loaded. This is far from ideal and the SiteService should really either throw an exception when you do this or load the required data.
However to get it working when you're modifying a site fully load the site first before you make changes.
Site site = siteService.getSite(site.getId());
ResourceProperties rp = site.getProperties();
rp.addProperty(name, value);
siteService.save(site);

Why is my Java app getting killed?

I could be wrong about which line is causing the problem, but the error changes everytime I change this if() statement:
try {
while(true) {
String LRU = hashOperation();
System.out.println("In worker thread, this should be valid JSON: " + LRU);
if (jsonValidator.isStringValidJSON(LRU) && !LRU.isEmpty()) {
HashMap<String,String> messageMap = jsonGenerator.readJSON(LRU);
So I assume the problem is with this if() statement. If I write it like this:
if (jsonValidator.isStringValidJSON(LRU)) {
Then the app starts up and I get this exception:
In worker thread, this should be valid JSON:
Exception in worker thread in Main::main: No content to map to Object due to end of input
but if I write it like this:
if (jsonValidator.isStringValidJSON(LRU) && !LRU.isEmpty()) {
Then the app starts up, but then almost instantly dies:
/usr/bin/java -cp /home/jenkins/run-nlp/SSAM.jar com.sofar.SSAM.Main
Starting NLP app 2015/08/30 21:42:28
Loading classifier from dependencies/english.all.7class.distsim.crf.ser.gz ... Killed
The basic idea here is that the app starts up and then spins up some background threads that poll endlessly on Redis, looking for input (the input comes from another app, that publishes data to a channel on Redis).
When I see "Killed" I assume that an Exception went uncaught, but I have this whole Thread::run() wrapped in a try/catch that ends with:
} catch (Exception e) {
System.out.println("Exception in worker thread in Main::main: " + e.getMessage());
}
You can see that this is the Exception message that I get if I do this:
if (jsonValidator.isStringValidJSON(LRU)) {
Although it is possible the error is elsewhere, and I think it is odd that an empty string would be valid JSON, I thought I would screen out that possibility with:
if (jsonValidator.isStringValidJSON(LRU) && !LRU.isEmpty()) {
Why would this one change cause my app to be Killed?
UPDATE:
I refactored the app to this:
static void processMessage(ssamBrain ssamBrain, Jedis jedis, HashMap<String, String> responseMap, JsonGenerator jsonGenerator, JSONValidator jsonValidator, String LRU) {
try {
if (!LRU.isEmpty()) {
HashMap<String,String> messageMap = jsonGenerator.readJSON(LRU);
Transformer transformer = new Transformer();
Again, the line that causes the problem seems to be:
if (!LRU.isEmpty()) {
If I don't have that if() statement, then the code gets:
java.io.EOFException: No content to map to Object due to end of input
on the next line, since an empty string is not valid JSON.
But when I add this:
if (!LRU.isEmpty()) {
Then my app dies on startup.
Maybe autoboxing is the problem? Or the absence of it? Assume for now:
LRU = "";
I assume I can call methods on it, but maybe not? And why wouldn't I get an Exception?
It's possible the problem is elsewhere, but that if() statement seems to be the main thing that causes the problem to surface.
UPDATE
UPDATE
The app sometimes survives for a few minutes, but other times it dies after a few seconds. So I guess I need to ask:
1.) when a Java app says "Killed" in the terminal, does that always mean that an Exception went uncaught?
2.) what might cause such variable behavior?
How to works if operator:
if (CONDITION_1 && CONDITION_2){
doSomething();
}
checks CONDITION_1 - if it is true:
checks CONDITION_2 - if it is true:
runs doSometring().
If CONDITION_1 is false or throws an exception, CONDITION_2 never checks.
Please, show the exception stackTrace.
Change:
if (jsonValidator.isStringValidJSON(LRU) && !LRU.isEmpty())
to:
if (!LRU.isEmpty() && jsonValidator.isStringValidJSON(LRU))
So, you first check if LRU is empty and if it is not - you run validation.

orientdb property constraints like MANDATORY not failing on improper java api insertions

I have an orientdb setup with a class called MessageLog with a property messageId that has a constraint MANADATORY and NOT NULL. If i try to insert a record from the console with a null messageId it raises an exception telling that the property cannot be null. But when i do a simple insertion from the Java API the record is inserted with the property value null. How is that possible.
The java code is:
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.blueprints.impls.orient.OrientGraph;
import com.tinkerpop.blueprints.impls.orient.OrientVertexType;
public class OrientDbTrials {
public static void main(String[] args) {
OrientGraph graph = new OrientGraph("remote:localhost/blah","root","*****");
System.out.println("Connected to the db.");
Vertex messageLog = graph.addVertex("class:MessageLog");
System.out.println("Created new vertex : " + messageLog.toString());
messageLog.setProperty("messageId", null);
graph.commit();
System.out.println("Successfully saved it.");
}
}
Can somebody please explain this.
From http://www.orientechnologies.com/docs/1.7.8/orientdb.wiki/Graph-Database-Tinkerpop.html
Every time the graph is modified an implicit transaction is started automatically if no previous transaction was running. Transactions are committed automatically when the graph is closed by calling the shutdown() method or by explicit commit(). To rollback changes call the rollback() method. Changes inside a transaction will be temporary till the commit or the close of the graph instance. Concurrent threads or external clients can see the changes only when the transaction has been fully committed.
Because you are not defining how to handle exceptions, I think that when one is raised the graph is being closed and thus committing the change. When I run the code you give in a try catch block, I get an exception.
OrientGraph graph = new OrientGraph("remote:localhost/blah","root","*****");
System.out.println("Connected to the db.");
try { Vertex messageLog = graph.addVertex("class:MessageLog");
System.out.println("Created new vertex : " + messageLog.toString());
messageLog.setProperty("messageId", null);
graph.commit();
} catch(Exception e) {
graph.rollback();
}
finally {
graph.shutdown();
}
That code (pardon the ugly) raises this exception
java.lang.IllegalArgumentException: Property value can not be null
And because I handle the exception with a graph.rollback() the vertex doesn't show up in the graph.
What's interesting is that just having the mandatory property doesn't seem to be enough, i.e if you never fill the field with a null in the first place it will allow the commit, not raising an exception. I haven't looked into it, but maybe it has something to do with a graph instance being mixed-schema by default or something like that? I hope someone else knows.

IO Exception, my program with input data of x,y sometimes works without any error and randomly sometimes doesnt work with the same x,y input data

i am looping a method with a Thread which reads from a website(dynamically)
all the methods work perfectly, but my problem is that sometimes (3 out of 10 times) that i start the program it throws IO exception at me although i haven't changed my input data from the last known good execution , the exception is coming from the method below:
public String readThisUrlContent() throws ExceptionHandler
{
try {
#SuppressWarnings("static-access")
Document doc = Jsoup.connect(url).timeout(1000).get();
return doc.body().text();
} catch (IOException e) {
throw new ExceptionHandler("IO Exception for reading the site in method setUrlContent in Url class");
}
}
my best guess is that since i'm reading more than one Url with looping this method but the timeout is not sometimes at the best range (considering the internet speed etc. it sometimes doesn't work) but its just my theory and it can be dead wrong but even if its correct i have no idea how to handle it
The problem exactly was the time to live of the opened port. since i had other functions working at the same time program simply needed more connected time so i expanded timeout to (5000) and also reduced the timer of another Time.Schedule method in another method, and so it worked

Any Java API in Azure to get existing ServiceBusContract?

I am using the tutorial here for pushing data and consuming, data from Azure Service Bus. When I run the example the second time, I get back an error PUT https://asbtest.servicebus.windows.net/TestQueue?api-version=2012-08 returned a response status of 409 Conflict, which is way of saying you have already a configuration with that name, so do not create it another time. Most probably, this is the guilty code
Configuration config =
ServiceBusConfiguration.configureWithWrapAuthentication(
"HowToSample",
"your_service_bus_owner",
"your_service_bus_key",
".servicebus.windows.net",
"-sb.accesscontrol.windows.net/WRAPv0.9");
ServiceBusContract service = ServiceBusService.create(config);
QueueInfo queueInfo = new QueueInfo("TestQueue");
That is recalling create() is causing the problem, I would guess. But all methods in com.microsoft.windowsazure.services.serviceBus.ServiceBusService from http://dl.windowsazure.com/javadoc/ are only create, and I am unable to find a method like
ServiceBusContract service = A_class_that_finds_existing_bus_contract.find(config);
Am I thinking the wrong way, or is there another way out. Any pointers are appreciated.
EDIT:
I realized my code example for what I was asking was config, not service bus contract. Updated it, to reflect so.
Turns out I was wrong. The create() function in ServiceBusService does not throw any exception, as I gathered from Javadocs. Also, you can create the service bus contracts multiple times, as it being only a connection. The exception arises, when you attempt to create a queue with a name that already exists. That is this line.
String path = "TestQueue";
QueueInfo queueInfo = new QueueInfo(path);
To overcome this, you can go this way.
import com.microsoft.windowsazure.services.serviceBus.Util;
...
...
Iterable<QueueInfo> iqnf = Util.iterateQueues(service);
boolean queue_created = false;
for( QueueInfo qi : iqnf )
{
if( path.toLowerCase().equals( qi.getPath() ))
{
System.out.println(" Queue already exists. Do not create one.");
queue_created = true;
}
}
if ( !queue_created ) {
service.createQueue(queueInfo);
}
Hope, this helps anybody who may be stuck on create conflicts for queue on Azure.
EDIT: Even after I got the path code, my code refused to work. Turns out there is another caveat. Azure makes all queue names in lower case. I have edited the code to use toLower() for this work around.
I upvoted Soham's Question and Answer. I did not know about lowercase though I have not verified it. It did confirm the problem I am having right now as well.
The way #Soham has addressed it is good but not good for large ServicebUs where we may have tons of Queues it's added overhead to iterate it. The only way is to catch the ServiceException which is very generic and ignore that Exception.
Example:
QueueInfo queueInfo = new QueueInfo(queName);
try {
CreateQueueResult qr = service.createQueue(queueInfo);
} catch (ServiceException e) {
//Silently ignore for now.
}
The right way would be for the Azure library to extend the ServiceException and throw "ConcflictException" for e.g. which is present in httpStatusCode of ServiceException but unfortunately it's set to Private.
Since it is not We would have to extend the ServiceException and override the httpStatusCode setter.
Again, not the best way but the library can improve if we list as feedback on their Github issues.
Note: ServiceBus is still in preview phase.

Categories