N1QL REST queries from R

#1

Hello,

I am an R user. Since there is no R SDK, I am looking at ways of using CB through the N1QL REST API.

From R, I could make a SELECT query to work using the R package called httr (a wrapper for curl):

library(httr)
qry = "statement=SELECT name,type FROM \`beer-sample\` where type = 'brewery' LIMIT 1"
b = httr::POST(url = 'http://localhost:8093/query/service', content_type("application/x-www-form-urlencoded"),body=qry, verbose())
c=content(b,as = "parsed")
(c$results)

[[1]]
[[1]]$name
[1] “357”

[[1]]$type
[1] “brewery”

To INSERT a new document, I am trying to run the following query (which works fine in Query Workbench Developer)

INSERT INTO `beer-sample` (KEY, VALUE) VALUES ("odwalla-juice1", 
      { "productId": "odwalla-juice1", "unitPrice": 4.40, "type": "product", "color":"green"})

However when I try to INSERT a document, I can’t seem to make it work.

qry = 'INSERT INTO \`beer-sample\` (KEY, VALUE) VALUES ("odwalla-juice1",{"productId": "odwalla-juice1", "unitPrice": 4.40, "type": "product", "color":"green"})'
b = httr::POST(url = 'http://localhost:8093/query/service', content_type("application/x-www-form-urlencoded"),body=qry, verbose())
c=content(b,as = "parsed")
(c$results)

I get

{
“requestID”: “641afff5-e131-43a8-ab21-12d7c2a73fb3”,
“errors”: [
{
“code”: 1065,
“msg”: "Unrecognized parameter in request: INSERT INTO beer-sample (KEY, VALUE) VALUES (“odwalla-juice1”,{“productId”: "odwalla…
}
],
“status”: “fatal”,

Anybodody can help me on that? How to make this work? Or is there a better way? I am a beginner and do not know how to go beyond this point

thanks a lot from advance
Servet

#2

Hello,

Yes, you will be able to make this work. I would suggest you first get it working via curl, and then migrate that to httr.

The INSERT should be provided via the “statement” parameter. Here is the N1QL REST API spec: http://goo.gl/ezpmVx

Gerald