How to call the PLAN (created using prepare statement) in the JAVA

  1. Create the Prepare statements using the following command.
    prepare select a.function_section.* from supplier_function a where a.type = $function;

  2. Got the results in JSON. This is my name : “name”: “3f8f2fab-65f1-4a47-a506-833997cbd8f8”,

  3. How can execute this plan in the Java code. ???

# query by PREPARE from client(JAVA)
curl -v http://127.0.0.1:8093/query/service -d 'prepared="3f8f2fab-65f1-4a47-a506-833997cbd8f8"&$funtion="FUNCTION"'

or you can

# Create PREPARE statement with name
curl -v http://127.0.0.1:8093/query/service --data-urlencode 'statement=PREPARE queryAllByType FROM select a.function_section.* from supplier_function a where a.type =$funtion; '
# query by PREPARE name from http client(JAVA)
curl -v http://127.0.0.1:8093/query/service -d 'prepared="queryAllByType"&$funtion="FUNCTION"'

Using Java SDK, you can specify adhoc parameter to be false to exploit prepared statement without having to manage the prepared statement/etc.
SDK handles this and multi-node environment, etc seamlessly.

From: Java SDK and Prepared Statement

N1qlParams.build().consistency(ScanConsistency.REQUEST_PLUS).adhoc(false));

Thanks for your inputs …pls provide me the piece of JAVA code to execute the prepare statement…Thanks

Thanks atom_yang…can you please provide peice of code to execute prepared statements using N1ql …that helps me a lot

any java http client can send http request. it is REST API.

for example with OKHttpClient

OkHttpClient client = new OkHttpClient();

MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
RequestBody body = RequestBody.create(mediaType, "prepared=%22queryAllByType%22&%24funtion=%22FUNCTION%22");
Request request = new Request.Builder()
  .url("http://127.0.0.1:8093/query/service")
  .post(body)
  .addHeader("content-type", "application/x-www-form-urlencoded")
  .addHeader("cache-control", "no-cache")
  .build();

Response response = client.newCall(request).execute();

Thanks for the code… in the above example 22queryAllByType - hope this could be my plan name…

And the URL mentioned above - same URL I have to give or the URL where my couch eb available in our organization…if yes how to find the IP address

And also will don’t want hard code the URL …how can we pass that URL

you can parametric URL and plan name into config file or some other place.
IP address is the query service address here.

Tried with below Code :::

		MediaType mediaType = MediaType
				.parse("application/x-www-form-urlencoded");
		RequestBody body = RequestBody.create(mediaType,
				"prepared=%223f8f2fab-65f1-4a47-a506-833997cbd8f8%22&%24funtion=%22FUNCTION%22");
		Request request = new Request.Builder()
				.url("http://localhost:8095/query/service")
				.post(body)
				.addHeader("content-type", "application/x-www-form-urlencoded")
				.addHeader("cache-control", "no-cache").build();
		System.out.println(request);

Request{method=POST, url=http://localhost:8095/query/service, tag=null}

Response{protocol=http/1.1, code=500, message=Internal Server Error, url=http://localhost:8095/query/service}

Please advice what could be the issue.

default query service post is 8093.
FYI

What could the response format…it will be the JSON with the results…?

yes

Content-Type: application/json; version=1.7.0-N1QL

FYI

With the Java SDK, all you need to do is set it to .adhoc(false). The SDK will automatically prepare the statement and use the prepared statement on your behalf. You won’t need to manually prepare it or use the REST interface!

Not sure I didn’t get you…
1.) Have created plan using the prepared statement.
prepare select a.function_section.* from ace_supplier a where a.type = $function;
2. ) I got the below plan name.
68aad199-f7b5-4b67-867d-bb6398ccbc67
3.) Then I would like to use this prepared statement in my JAVA code without REST API.
4.) Kindly advice me how I can do this using.
It would be really great if you can able to share the code .

Thanks in advance.!

@naganjaneyulu.yazali this is not possible, the SDK is creating the plan for you transparently. You supply the the query (without the PREPARE) and the client in the background will transparently prepare, store the prepared plan and send it over for execution every time you submit the query when .adhoc(false) is set!

So all you do is query with “select a.function_section.* from ace_supplier a where a.type = $function” and .adhoc(false) and it will work. Note that I see $function in here, so just use the ParameterizedQuery with .adhoc(false)

@daschl - Actually I don’t want keep the Queries in my JAVA code. So that I can’t rebuild each time if there is any change in the Queries …Is is possible to use the prepared plan directly in the code wihtout REST call