Pass variable within a cypher query in Shiny R - r

I will like to pass variable within the cypher query in Shiny R.
The R version that I am using is R version 3.3.3 (2017-03-06) and neo4j-community-3.1.1
There is no issue retrieving the data from neo4j DB via below cypher query in Shiny R
query = "MATCH p=()-[:Month]->()-[:Day]->(d:Day)-[Hour]->(h:Hour)<-[timerel:CONNECTED_ON]-(n:IP)-[:CONNECT_TO]->(k:IP) WHERE h.hour=\"9\" and d.day=\"2\" RETURN p limit 25
cypher(graph, query)
However, when I attempt to pass variable into the cypher query based on what I have read in http://neo4j.com/docs/developer-manual/current/cypher/syntax/parameters/, I encountered this error message:
"Warning: Error in : Client error: (400) Bad Request
Neo.ClientError.Statement.ParameterMissing
Expected a parameter named day"
day = "2"
hour = "9"
query = "MATCH p=()-[:Month]->()-[:Day]->(d:Day)-[Hour]->(h:Hour)<-[timerel:CONNECTED_ON]-(n:IP)-[:CONNECT_TO]->(k:IP) WHERE h.hour=$hour and d.day=$day RETURN p limit 25
I have also tried the following:
1: Putting {$hour}
day = "2"
hour = "9"
query = "MATCH p=()-[:Month]->()-[:Day]->(d:Day)-[Hour]->(h:Hour)<-[timerel:CONNECTED_ON]-(n:IP)-[:CONNECT_TO]->(k:IP) WHERE h.hour={$hour} and d.day={$day} RETURN p limit 25
2: Putting {hour}
All the above does not allow me to pass variable into the cypher query statement.
Appreciate it if anyone can guide me on that. Thank you.

To answer my own question for the sharing purpose, below 2 urls serve to address the method on how to pass variable in using Rneo4j
https://rdrr.io/cran/RNeo4j/man/cypher.html
https://github.com/nicolewhite/RNeo4j/blob/master/man/cypher.Rd

The query argument of cypher() is expecting a string. Use glue to compose the string.

Related

Gremlin - Expecting valueMap values are as a string instead of array

g.V(VertexId).valueMap("tags") returns
{'tags': ['My Last Day', 'Poor Connection > Netenter code herework Issue', 'Or Hello > Equals Hi > Last Message ', 'Network Issue', 'Last Message ']}
g.V(VertexId).valueMap("tags").by(unfold()) returns
{'tags': 'My Last Day'}
Expecting like this:
{'tags': "My Last Day, Poor Connection > Netenter code herework Issue, Or Hello > Equals Hi > Last Message, Network Issue, Last Message"}
If I understand correctly, you want to return a set of multi-properties for "tags" as a single string. So the question really is how to concatenate each multi-property together to a single string in Gremlin.
The answer at this time is that you can't. There are no string concatenation options in Gremlin at this time, unless you can use a lambda (which won't work for Neptune and are generally discouraged for a number of reasons anyway). You will need to return all the multi-properties and then concatenate them in your application. It is possible that that we will see string concatenation functions for TinkerPop 3.7.0.

How to send query with variables from Rstudio to Mongo database?

I'm trying to send query to my Mongo database depending on a variable producted in my R code.
Unfortunately, Mongo query needs double quotes and the function "paste" in R doesn't support it.
Here my variable is equal to "test01" but it will change at each iteration.
I tried to build a great character chain named "req" thanks to some "paste" functions with returns:
req
> "'{\"id\":\"test01\"}'"
cat(req)
> '{"id":"test01"}'
Which seems to be as a query but when I try :
connection_db$count(jsonlite::toJSON(req))
It returns [0] whereas if if try :
connection_db$count('{"id":"test01"}')
It returns [1].
Can you help me please?

RODBC gives proper row count but yields empty query

Using R-3.5.0 and RODBC v. 1.3-15 on Windows.
I am trying to query data from a remote database. I can connect fine and if I do a query to count the rows, the answer comes out correctly. But if I try to remove the count statement select count(*) and actually get the data via select *, I yield an empty query (with some rather strange headers). Only two of the column names come out correctly and the rest are question marks and a number (as shown below). I can using sql developer to query the data no problem.
I include the simplest version of the code below but I get the same results if I try to limit to just a few rows or certain conditions, etc. Sorry I cannot create a reproducible example but as this is a remote db and I have no idea what the problem is, I'm not sure how I could even do that.
I can query other tables from different schemas within the same odbc connection, so I don't think it is that. I have tried with and without the believeNRows and the rows_at_time.
Thank you for any thoughts.
channel <- odbcConnect("mydb", uid="myuser", pwd="mypass", believeNRows=FALSE,rows_at_time = 1)
myquery <- paste("select count(*) from MYSCHEMA.MYTABLE")
sqlQuery(channel, myquery)
COUNT(*)
1 149712361
myquery <- paste("select * from MYSCHEMA.MYTABLE")
sqlQuery(channel, myquery)
[1] ID FMC_IN_ID ? ?.1 ?.2 ?.3 ?.4 ?.5 ?.6 ?.7 ?.8 ?.9 ?.10 ?.11 ?.12 ?.13 ?.14 ?.15
<0 rows> (or 0-length row.names)
I would try the following:
add a simple limit 100 to your query to see if you can get some data back
add the believeNRows option to the sqlQuery call -- in my experience it is needed at that level
In case it helps others, the problem was that the database contained an Oracle spatial field (MDSYS.SDO_GEOMETRY). R did not know what to do with it. I assumed it would just convert it to a character but instead it just got confused. By omitting the spatial field, the query worked fine.

Searching elasticsearch from R using URI string with #timestamp range

I am trying to modify working URI search from R to retrieve data from ElasticSearch using time range on #timestamp es variable:
here is the uri string:
myuri01 <- paste0( 'myserver01:9201/filebeat-*/_search?pretty=true&size=9000&sort:#timestamp&q=%2BSESTYPE:APPTYPE001%20 2Bhost:mytesthost%20%2B#timestamp>"2016-10-25T18:59:23.250Z"')
retset = getURL(myuri01 )
but I get no data, no errors ... Also tried without double quote around time stamp - same result. If I remove the last timestamp part then I do get the data as expected.
Any help would be appreciated.
Thanks

Proper way to pass parameters to query in R DBI

In perl/python DBI APIs have a mechanism to safely interpolate in parameters to an sql query. For example in python I would do:
cursor.execute("SELECT * FROM table WHERE value > ?", (5,))
Where the second parameter to the execute method is a tuple of parameters to add into the sql query
Is there a similar mechanism for R's DBI compliant APIs? The examples I've seen never show parameters passed to the query. If not, what is the safest way to interpolate in parameters to a query? I'm specifically looking at using RPostgresSQL.
Just for completeness, I'll add an answer based on Hadley's comment. The DBI package now has the function sqlInterpolate which can also perform this. It requires a list of function arguments to be named in the sql query that all must start with a ?. Excerpt from the DBI manual below
sql <- "SELECT * FROM X WHERE name = ?name"
sqlInterpolate(ANSI(), sql, name = "Hadley")
# This is safe because the single quote has been double escaped
sqlInterpolate(ANSI(), sql, name = "H'); DROP TABLE--;")
Indeed the use of bind variables is not really well documented. Anyway the ODBC commands in R work differently for different databases. One possibility for postgres would be like this:
res <- postgresqlExecStatement(con, "SELECT * FROM table WHERE value > $1", c(5))
postgresqlFetch(res)
postgresqlCloseResult(res)
Hope it helps.

Resources