Couchbase command scripting error

Hi,

I want to add cbq and cbimport commands in a shell script. Actually I want to export the data using come cbq commands and then import those data to target cluster.
I tried using small script to test. But it is getting stuck every time on the SELECT statement.
Below is the test script of mine.

echo “Hello World”
cbq -u Administrator -p Administrator -e “http://localhost:8093”;
\REDIRECT temp.txt;
SELECT * FROM test.sample where id=“106” --output=“temp.txt”;
cbimport json -c http://{target-cluster}:8091 -u Administrator -p Administrator -b test.sample -d file://C:\Users\myusername\Desktop\temp.txt -f list -g %docId%
\EXIT;

And after making connection, script is getting stuck.

/Desktop
$ ./test.sh
Hello World
Connected to : http://localhost:8093/. Type Ctrl-D or \QUIT to exit.

Path to history file for the shell : C:\Users\testuser.cbq_history

Can someone please suggest something here.

@sgrverma23 It looks like you are entering the interactive version of the cbq shell. See the “–script” flag described here for doing non-interactive scripting:

1 Like

Cbimport not working as expected post described
you will not able to use output from cbq shell into cbimport directly. You need to modify the output of cbq shell format to cbimport expected format.

You can use cbexport/dbimport combinations
You can cbbackupmgr
You can use XDCR

cbexport does not provide the ability to filter the result on basis of some where clause. If there is any alternative to use filter on bucket level, please suggest.

a.txt file content

\set -pretty false;
\set -metrics false;
\set -signature false;
\REDIRECT a.txt.out;
SELECT d.* FROM default AS d;

RUN : cbq -e=127.0.0.1:8093 -u=Administrator -p=password -file a.txt

Content of a.txt.out

SELECT d.* FROM default AS d
{
"requestID": "ecfec711-94c3-4750-b5fb-73864abc6b6a",
"results": [
{"id":"123456","name":"abc","skills":[{"level":"expert","skill_id":"SK01","skill_name":"Java"},{"level":"intermediate","skill_id":"SK01","skill_name":"Java"},{"level":"intermediate","skill_id":"SK02","skill_name":"c++"}]},
{"id":"554432","name":"cde","skills":[{"level":"expert","skill_id":"SK01","skill_name":"Java"},{"level":"intermediate","skill_id":"SK02","skill_name":"c++"},{"level":"intermediate","skill_id":"SK02","skill_name":"c++"}]},
{"id":"6789076","name":"cdef","skills":[{"level":"expert","skill_id":"SK01","skill_name":"Java"},{"level":"intermediate","skill_id":"SK02","skill_name":"c++"},{"level":"intermediate","skill_id":"SK03","skill_name":"php"}]}
],
"status": "success"
}

edit a.txt.out
Replace first 4lines with [
Last 3 lines with ]

 [
{"id":"123456","name":"abc","skills":[{"level":"expert","skill_id":"SK01","skill_name":"Java"},{"level":"intermediate","skill_id":"SK01","skill_name":"Java"},{"level":"intermediate","skill_id":"SK02","skill_name":"c++"}]},
{"id":"554432","name":"cde","skills":[{"level":"expert","skill_id":"SK01","skill_name":"Java"},{"level":"intermediate","skill_id":"SK02","skill_name":"c++"},{"level":"intermediate","skill_id":"SK02","skill_name":"c++"}]},
{"id":"6789076","name":"cdef","skills":[{"level":"expert","skill_id":"SK01","skill_name":"Java"},{"level":"intermediate","skill_id":"SK02","skill_name":"c++"},{"level":"intermediate","skill_id":"SK03","skill_name":"php"}]}
]

now use cbexport with list format cbexport json | Couchbase Docs
cbexport json -c couchbase://127.0.0.1 -u Administrator -p password -b default -o a.txt.out -f list

Hi, thanks for the solution.
Also, using the below commands in script, I was able to produce the desire result here.

cbq -u Administrator -p ${password} -q -e “http://localhost:8093” --script=“select * FROM `com.test.default` where id==”“106"”;" | jq -c ‘.results[0].“com.test.result”’ > ${file}
cbimport json -c http://{target-cluster}:8091 -u Administrator -p ${password} -b com.test.result -d file://${file} -f lines -g %docId% -v