Executing CQL through Shell Script? - cassandra

I am trying to execute the CQL commands from shell script.
I am able to connect to the cqlsh (CQL version i'm using is 1.1.18) but unable to send the queries to cql.
Any ideas or suggestion how to proceed on this?
Do I need to connect to Cassandra and execute few commands (select/update ) with shell script ??

cqlsh -e "select * from ks.table limit 1;" > ~/output

I'm not sure about Cassandra 1.1.18, but you should be able to accomplish this with the -f flag of cqlsh. Let's say have a file of CQL commands called "commands.cql". I can invoke those commands against my local Cassandra instance like this:
$ cqlsh -f commands.cql -u myusername -p mypassword localhost
If I wanted to invoke that from within a Bash script, the script's code would look something like this:
#!/bin/bash
cqlsh -f commands.cql -u myusername -p mypassword localhost
Save that as an executable file, and run it like any other.

Need to connect to cassandra and execute few commands (select / update ) with shell script
You can execute your commands with shell script in next way:
echo "some QUERY; exit" | cqlsh CASSANDRA_HOST -u 'USER' -p 'PASS'

The "exit" command in the last suggestion is a bit hacky.
I would propose using xargs with cqlsh -e.
echo "some QUERY;" | xargs cqlsh CASSANDRA_HOST -u 'USER' -p 'PASS' -e
I recently had to use this approach when working with docker, because clqsh -f was not an option (too complex to configure access to the file needed).

echo "some QUERY;" | xargs cqlsh CASSANDRA_HOST -u 'USER' -p 'PASS' -e
But what if you Cassandra instance is on a different server to where the shell script is being executed? (Specifically in StreamSets - wouldn't the above require Cassandra installed on the same server such that it has access to the cqlsh lib?)

Related

In Git Bash, how do I fix a "stdin is not a tty" error?

I'm using Git Bash on Windows 10. I would like to import a SQL file to be run in my PostGres 12 local database. I tried the below
$ PGPASSWORD=$DB_PASSWORD psql -U${DB_USER} $DB_NAME < scripts/my-script.sql
stdin is not a tty
When I look in my database, the script hasn't been run, which leads me to believe the error message is telling me why, except I'm not sure what it means or how to fix it.
Don't use redirection on Windows (in general, not just in "git-bash").
Pass the script file using the -f parameter:
PGPASSWORD=$DB_PASSWORD psql -U${DB_USER} -f scripts/my-script.sql $DB_NAME

How to connect to SQLPLUS and run SQL script within ssh unix?

I have the following bash script:
#!/bin/bash
set command "sqlplus -s user/password#tns"
ssh -t test#192.168.94.139 $command
Now, I want to run te following sql script (which is on the other device I'm accessing):
/usr/mikael/myfile.sql
How is the best practice to run the script from that path?
I've seen a suggestion to add "/usr/mikael/myfile.sql" to the end of the command, as :
set command "sqlplus -s user/password#tns /usr/mikael/myfile.sql"
Is that really good? (I'm working on a prod environment and don't want to mess with it).
Thanks.
set does not do what you think it does. Use
command="sqlplus -s user/password#tns #/usr/mikael/myfile.sql"

How to list/kill running commands in cassandra

I want to run a COPY command on my Cassandra cluster (v3.0.9).
In case my shell exits, how can I later on list running commands, get their status and possibly kill/stop it?
Create a cql file with your COPY command
Run it through cqlsh using -f flag (cqlsh -f ) with nohup. Details here.
Then it will be available to ps and kill.

how to ssh run a tail and then send data to a mysql database

This code SSH's and then runs a tail command on a remote hots. I would now like to pass that tailed data into a mysql database using a local script called insertPerfmon.sh.
How do I pass data generated in a ssh session into the local shell script insertPerfmon.sh. The local shell script is going to send data to the database. However, I need to get it there first.
( ssh -nq -o StrictHostKeyChecking=no \
-i $PEM_PATH/$PEM_FILE $USER#${host} -p $REMOTE_PORT \
tail -n 5 $REMOTE_HOME/data/PerfMon* |insertPerfmon.sh)
If insertPerfmon.sh is:
#!/bin/bash
mydata=$(cat)
echo $mydata
# process & send $mydata to database
The following should work:
<your_ssh_command> | bash insertPerfmon.sh

Is it possible to run multiple command with remote command option in putty?

I want to run multiple commands automatically like sudo bash, ssh server01, ls , cd /tmp etc at server login..
I am using Remote command option under SSH in putty.
I tried multiple commands with delimiter && but not working.
There is a some information lacking in your question.
You say you want to run sudo bash, then ssh server01.
Will sudo prompt for a password in your remote server?
Assuming there is no password in sudo, running bash will open another shell waiting for user input. The command ssh server01 will not be run until that bash shell is exited.
If you want to run 2 commands, try first simpler ones like:
ls -l /tmp ; echo "hi there"
or if you prefer:
ls -l /tmp && echo "hi there"
Does this work?
If what you want is to run ssh after running bash, you can try :
sudo bash -c "ssh server01"
That is probably because the command is expected to be a program name followed by parameters, which will be passed directly to the program. In order to get && and other functionality that is provided by a command line interpreter such as bash, try this:
/bin/bash -c "command1 && command2"
I tried what I suggested in my previous answer.
It is possible to run 2 simple commands in putty separated by a semicolon. As in my example I tried with ls and echo. The remote server runs them and then the session closes.
I also tried to ssh to a remote server that is configured for not asking for a password. In that case, it also works, I get connected to the 2nd server and I can run commands on it. Upon exit, the 2 connections are closed.
So please, let us know what you actually need / want.
You can execute two consecutive commands in PuTTY using a regular shell syntax. E.g. using ; or &&.
But you want to execute ssh server01 in sudo bash shell, right?
These are not two consecutive commands, it's ssh server01 command executed within sudo bash.
So you have to use a sudo command-line syntax to execute the ssh server01, like
sudo bash ssh server01

Resources