Until now I have only interacted with neo4j through its browser and REST interface.
Now I want to interact with neo4j via Java and an embedded database.
I get a null pointer exception when I attempt to run a Cypher query.
The console output did not help me:
Exception in thread "main" java.lang.NullPointerException
at org.neo4j.cypher.ExecutionEngine.execute(ExecutionEngine.scala:58)
at org.neo4j.cypher.ExecutionEngine.execute(ExecutionEngine.scala:54)
at org.neo4j.cypher.javacompat.ExecutionEngine.execute(ExecutionEngine.java:65)
at neo.project1.App.main(App.java:59)
My config
Ubuntu 14.04
neo4j/stable,now 2.0.2 all [installed]
java version "1.7.0_55"
Link to .java file and pom file
Try this:
ExecutionEngine engine = new ExecutionEngine(graphDb);
Sounds like your original constructor call (with the second parameter as NULL) is letting the ExecutionEngine try to use a NULL logger.
Related
I'm new to postgres but am attempting to call a procedure in Postgres 11 (new "procedure" not a "function"), calling from java as a spring SimpleJDBCCall (using Postgresql-42.2.5 jdbc driver). However, when I execute the procedure I am encountering the following exception:
org.springframework.jdbc.BadSqlGrammarException:
CallableStatementCallback; bad SQL grammar [{call
pa_test_schema.pr_dosomething(?)}]; nested exception is
org.postgresql.util.PSQLException: ERROR:
pa_test_schema.pr_dosomething(bigint) is a procedure Hint: To call a
procedure, use CALL. Position: 15 at
org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.doTranslate(SQLStateSQLExceptionTranslator.java:101)
at
org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72)
at
org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)
at
org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)
at
org.springframework.jdbc.core.JdbcTemplate.translateException(JdbcTemplate.java:1402)
at
org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:1065)
at
org.springframework.jdbc.core.JdbcTemplate.call(JdbcTemplate.java:1104)
at
org.springframework.jdbc.core.simple.AbstractJdbcCall.executeCallInternal(AbstractJdbcCall.java:414)
at
org.springframework.jdbc.core.simple.AbstractJdbcCall.doExecute(AbstractJdbcCall.java:397)
at
org.springframework.jdbc.core.simple.SimpleJdbcCall.execute(SimpleJdbcCall.java:193)
My procedure code:
CREATE PROCEDURE pa_test_schema.pr_DoSomething
( P_input_ID IN inputs.input_ID%TYPE
) AS $$
BEGIN
-- do something
END;
$$ LANGUAGE plpgsql;
My java code:
SimpleJdbcCallOperations pr_DoSomething = new SimpleJdbcCall(jdbcTemplate)
.withSchemaName("pa_test_schema")
.withProcedureName("pr_DoSomething");
Map<String, Object> inputs = Maps.newHashMap();
inputs.put("p_input_id", 123456);
pr_DoSomething.execute(inputs);
When I step through the code I can see the driver is modifying the sql of the callable statement to the syntax required for calling a postgres function:
select * from pa_test_schema.pr_dosomething(?) as result
This is the method in the driver that is doing this conversion:
https://github.com/pgjdbc/pgjdbc/blob/faab499853c56f67cb70fb242f75b918452f2a6f/pgjdbc/src/main/java/org/postgresql/core/Parser.java#L766
I understand procedures were only introduced in Postgres 11 (previously one would have used void-returning functions) and have read through the postgres driver documentation but don't see any reference to calling procedures rather than functions.
Does this mean that the current postgres driver does not yet support this or is there another approach I should be using? Should I just be using postgres functions instead?
Currently (as of Postgres 11.1 and driver version 42.2.5) the standard JDBC approach using a CallableStatement cannot be used to call a stored procedure.
I don't really use Spring JDBC Template, but the following code works in plain JDBC and should be adaptable to Spring JDBC Tempalte:
Connection con = DriverManager.getConnection(...);
PreparedStatement pstmt = con.prepareStatement("call pa_test_schema.pr_DoSomething(?)");
pstmt.setInt(1, 42);
pstmt.execute();
Note that this uses Postgres' call command. Do not confuse this with the "{call ...}" syntax for a CallableStatement.
Some more details on why currently a CallableStatement does not work can be found in the JDBC mailing list here and here
I've installed a neo4j in my lab's centOS server.I want to import a json file into neo4j.
I've already put the apoc-3.3.0.1.jar in the NEO4J_HOME/plugins and add the
apoc.import.file.enabled=true
into neo4j.conf
Then i open the web UI of neo4j with my PC(windows 10) and input the cypher
call apoc.load.json("file:///C://Users//stefen//Desktop//test.json")yield value as company
create(c:company)
with c,company
unwind company.organizationInformationList as a
set c = a
The json file is saved in my pc.
And error info is:
Failed to invoke procedure apoc.load.json: Caused by: java.lang.RuntimeException: Can't read url file:/C://Users//stefen//Desktop//test.json as json: /C:/Users/stefen/Desktop/test.json
I have no idea where is the problem.Please help me.
PS:my neo4j version is 3.3.1
I'm sorry. I have made a mistake that i forget to put the data file in my server.
Now i have solved the question.
I decided to return to Dropwizard after a very long affair with Spring. I quickly got the absolute barebones REST service built, and it runs without any problems.
Using Dropwizard 0.7.1 and Java 1.8, only POM entries are the dropwizard-core dependency and the maven compiler plugin to enforce Java 1.8, as recommended by the Dropwizard user manual
However, as soon as I try to add an Optional QueryParam to the basic controller, the application fails to start with the following error (cut for brevity):
INFO [2015-01-03 17:44:58,059] io.dropwizard.jersey.DropwizardResourceConfig: The following paths were found for the configured resources:
GET / (edge.dw.sample.controllers.IndexController)
ERROR [2015-01-03 17:44:58,158] com.sun.jersey.spi.inject.Errors: The following errors and warnings have been detected with resource and/or provider classes:
SEVERE: Missing dependency for method public java.lang.String edge.dw.sample.controllers.IndexController.index(java.util.Optional) at parameter at index 0
Exception in thread "main" javax.servlet.ServletException: com.sun.jersey.spi.container.servlet.ServletContainer-6c2ed0cd#330103b7==com.sun.jersey.spi.container.servlet.ServletContainer,1,false
The code for the controller is as follows:
#Path("/")
public class IndexController {
#GET
#Timed
public String index(#QueryParam("name") Optional<String> name) {
String saying = "Hi";
if(name != null && name.isPresent()) {
saying += " " + name.get();
}
return saying;
}
}
If I remove Optional from the mix, the application runs just fine. I replace the Optional-specific code with null checks and it works perfectly.
Am I missing something fundamental here? Both Google Guava Optional and java.util.Optional fail with the same error. (And yes, I did narrow it down to the Optional object)
A quick Google/SO search yielded nothing useful, but feel free to point me to a resource I may have missed
Thanks in advance!
Moments after posting this, I found that the issue was my use of Java 1.8. If using Java 1.8, I have to add the Java8Bundle to my app:
POM Entry:
<dependency>
<groupId>io.dropwizard.modules</groupId>
<artifactId>dropwizard-java8</artifactId>
<version>0.7.0-1</version>
</dependency>
And code in the Application class:
#Override
public void initialize(Bootstrap<SampleConfiguration> bootstrap) {
bootstrap.addBundle(new Java8Bundle());
}
See: https://github.com/dropwizard/dropwizard-java8
This enables both Google Guava Optional and java.util.Optional to work just fine.
If I revert to Java 1.7 and use the Google Guava Optional, it works just fine as well and I don't have to include the Java8Bundle. I'll opt for the Java8Bundle for now, though, as using Java8 features is lucrative for me :)
Cheers!
So, I recently upgraded our Grails app from version 1.3.7 to 2.3.4. I'm now getting an exception in a SOAP handler that attempts to extract the message content and log it to the DB. This worked in 1.3.7, but I'm assuming that some new dependency or something has messed with the classpath.
The code looks like this:
private String extractSOAPMessage(SOAPMessageContext smc) {
Source source = smc.getMessage().getSOAPPart().getContent()
TransformerFactory factory = TransformerFactory.newInstance()
Transformer transformer = factory.newTransformer()
transformer.setOutputProperty( OutputKeys.METHOD, "xml" )
java.io.StringWriter writer = new StringWriter()
Result result = new StreamResult( writer )
transformer.transform( source, result )
return writer.toString()
}
The exception I'm seeing is:
Caused by: java.lang.LinkageError: loader constraint violation: loader (instance of <bootloader>) previously initiated loading for a different type with name "javax/activation/DataHandler"
It happens on this line:
Source source = smc.getMessage().getSOAPPart().getContent()
It looks like the culprit is the getSOAPart() call.
Note that I am using the 1.1.1 version of the cxf plugin for Grails. Any help on this would be greatly appreciated. I've found several similar issues with solutions, but none of them have been for the "javax/activation/DataHandler", so I am not sure what's going on here.
I suspect something has a transitive dependency on the activation library which you need to exclude - try running a dependency-report. Since Java 6 that JAR has been un-necessary as it's built in to the core Java class library, but many things still have dependencies on it so they can work on Java 5 (or date back to when Java 5 was still in widespread use).
I am running neo4j enterprise 1.8 on grails and wanted to upgrade to 1.8.1. As I am also using the Cypher Engine extensively, after upgrade I tried to execute some queries. Unfortunately I encountered an Exception when trying to initiate the Cypher ExecutionEngine.
I've written a small Service which does the work for me:
import org.neo4j.cypher.javacompat.ExecutionEngine
import org.neo4j.kernel.impl.util.StringLogger
class CypherService {
def graphDatabaseService
static transactional = true
def executeString(String cypherString) {
log.debug "start method executeString"
ExecutionEngine executionEngine = new ExecutionEngine(graphDatabaseService, StringLogger.DEV_NULL)
if(executionEngine) {
def result = executionEngine.execute(cypherString)
return result
} else {
log.error "Could not initialize the execution engine for Cypher"
return null
}
}
}
When initiating the Execution Engine, I got the following Exception:
java.lang.NoSuchMethodError: com.googlecode.concurrentlinkedhashmap.ConcurrentLinkedHashMap$Builder.maximumWeightedCapacity(J)Lcom/googlecode/concurrentlinkedhashmap/ConcurrentLinkedHashMap$Builder;
at org.neo4j.cypher.internal.LRUCache.<init>(LRUCache.scala:31)
at org.neo4j.cypher.ExecutionEngine$$anon$1.<init>(ExecutionEngine.scala:91)
at org.neo4j.cypher.ExecutionEngine.<init>(ExecutionEngine.scala:91)
at org.neo4j.cypher.javacompat.ExecutionEngine.<init>(ExecutionEngine.java:54)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:77)
at org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteNoUnwrapNoCoerce.callConstructor(ConstructorSite.java:102)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.java:52)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:190)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:202)
at com.netjay.fanorakel.CypherService.executeString(CypherService.groovy:16)
Any idea what is going wrong here, and is there a solution to avoid this?
Best
Michael
You could probably also just rebuild Neo4j's cypher module against the older version of the lib, or actually exclude the dependency from neo4j.
I had the same issue- with 1.8.1 I needed to separately include concurrentlinkedhashmap-lru.1.3.1.jar
http://code.google.com/p/concurrentlinkedhashmap/downloads/detail?name=concurrentlinkedhashmap-lru-1.3.1.jar&can=2&q=
The problem here is that:
Neo4j 1.8.1 introduced a new dependency to concurrentlinkedhashmap_lru version 1.3.1, AND
Grails has a dependency to concurrentlinkedhashmap_lru version 1.2_jdk5, AND
concurrentlinkedhashmap_lru's API is not backwards compatible for these two versions.
Therefore the grails codebase has been recently upgraded to move the dependency to 1.3.1, see https://github.com/SpringSource/grails-data-mapping/commit/b15e207a2a08ac16e77de399733cb9cc14eff48e and
https://github.com/grails/grails-core/commit/6dfab1a5db4da8c176351f23d65c7fc0d4aa6364.
So to use Neo4j 1.8.1 (and newer) with Grails you could either wait for a new Grails release or build a Grails snaptshot on your own.