load data infile parameterized - linux

i need to load data from flat file into mariaDB on linux environment.
i've plan to put mariaDB script on shell file. then call shell from cron.
mariadb script shown as follow:
set #path = (select path_file from param);
set #tbl = (select table_name from param);
set #x = concat(
'LOAD DATA LOCAL INFILE ',
#path,
' INTO TABLE ', #tbl,
' (#row) set id = trim(substr(#row,1,2)), name = trim(substr(#row,3,19)), address= trim(substr(#row,22,20))'
);
prepare y from #x;
execute y;
deallocate prepare y;
when i execute the script directly on heidisql,
error shown:
this command is not supported in the prepared statement protocol yet
does any one have better way to load data from flat file into MariaDB on linux environment regularly (scheduled) without using any ETL tools?
Thanks.

One option you can try is (adjust as needed):
File: load_data.sh
path=$(mysql -u ${mysql_user} -p${mysql_password} -s -N <<GET_PATH
SELECT '/path/to/file/data.csv';
GET_PATH
)
tbl=$(mysql -u ${mysql_user} -p${mysql_password} -s -N <<GET_TABLE
SELECT 'table';
GET_TABLE
)
# mysql -u ${mysql_user} -p${mysql_password} -s -N <<LOAD_DATA
# LOAD DATA LOCAL INFILE '${path}'
# INTO TABLE \`${tbl}\` ...
# LOAD_DATA
# TEST
cat <<LOAD_DATA
LOAD DATA LOCAL INFILE '${path}'
INTO TABLE \`${tbl}\` ...
LOAD_DATA
Command line:
$ ls -l
-r-x------ load_data.sh
$ ./load_data.sh
LOAD DATA LOCAL INFILE '/path/to/file/data.csv'
INTO TABLE `table` ...

For clarity, write as much of the SQL into a STORED PROCEDURE. Then use bash to call that SP.

Related

Datastax Bulk Loader can't find my SSL certificate

On my windows machine I have CQLSH working and using a .cert file
Now I am starting to use DSBulk, but can't get the command line to know where to find my certificate.
I have a cert file here: C:\myfolder\mycert.cer
Here is a sample of my command line:
dsbulk count --ssl -u "myusername" -p "mypassword" -h "123.12.123.12" -k "mykeyspace" -query "select count(*) from mytable;"
the error message:
Operation failed: Expecting long or short option, got: 'myusername'
I suspect that I need to modify my command parameters to reference the cert file.
Any advice would be greatly appreciated!
#John O'Sullivan
According to the documentation idea shared above by Alex, you need to feed a file to the dsbulk command:
dsbulk {
connector.name = "csv"
connector.csv.delimiter = "|"
schema.keyspace = "myKeyspace"
schema.table = "myTable"
schema.mapping = "0=name, 1=age, 2=email"
}
datastax-java-driver {
advanced {
ssl-engine-factory {
keystore-password = "cassandra"
keystore-path = "/Users/myaccount/tmp/ssl/keystore.node0"
class = DefaultSslEngineFactory
truststore-password = "dse#r0cks!"
truststore-path = "/Users/myaccount/tmp/ssl/truststore.node0"
}
}
}
Then the command line references the file:
dsbulk load -f my-application.conf -url file1.csv -k ks1 -t table1
The specific page you need to reference is:
https://docs.datastax.com/en/dsbulk/doc/dsbulk/dsbulkUseSsl.html

SQLPLUS embedded in linux script does not work as expected

I have the following script segment in a Linux script:
sqlplus /
<<QUERY_1
UPDATE BATCH_FILE SET BATCH_ID = 0 WHERE BATCH_ID = -1;
COMMIT;
exit
QUERY_1
I am expecting the update to occur and the script to exit sqlplus
What actually happens is the query is not executed, and the script exits leaving sqlplus logged into my database with a SQL> prompt. I can execute the statements from the prompt, but of course, that is not what I want to do.
My current version of Oracle is 12.2.0.1
The output of the HERE-document is intended for the std input of sqlplus, but for the shell a command should be on a single line. Adding a backslash will make the shell ignore the line-end, combining the two physical lines into one logical line:
sqlplus / \
<<QUERY_1
UPDATE BATCH_FILE SET BATCH_ID = 0 WHERE BATCH_ID = -1;
COMMIT;
exit
QUERY_1
Or just:
sqlplus / <<QUERY_1
UPDATE BATCH_FILE SET BATCH_ID = 0 WHERE BATCH_ID = -1;
COMMIT;
exit
QUERY_1

PostgreSQL - Inserting into a columns whose name is a keyword using shell script

I have a table named "myTable" in my PostgreSQL database which has 4 columns - id, non_keyword_columnA, non_keyword_columnB and group.
Structure of the table is as follows:
Column | Type | Modifiers
--------------------+------------+------------------------------------------
id | integer | not null default nextval('myTable_id_seq'::regclass)
non_keyword_columnA | integer |
non_keyword_columnB | integer |
group | integer | not null
Foreign-key constraints:
"tablename_group_fkey" FOREIGN KEY ("group") REFERENCES groups(id)
I want to insert data into this table using shell and i am using following code to do it:
sudo /usr/sbin/chroot environment_path_here su - username -c "psql -A -z -c \"INSERT INTO myTable (non_keyword_columnA ,non_keyword_columnB ,"group") VALUES (1,2,(SELECT id from groups WHERE name='someGroupName'));\""
I am not an expert in Databases but I understand that group is a keyword which can be used in psql queries if used with double quotes which i did in above script.
But receive error as
ERROR: syntax error at or near "group"
LINE 1: ...RT INTO myTable(entity,machine,group) VAL...
^`
If I enter the environment manually and then execute the psql query then query executes successfully and row gets inserted but nothing is working through shell script.
I have tried various permutations and combinations to try as escape group keyword by using these combinations:
sudo /usr/sbin/chroot environment_path_here su - username -c "psql -A -z -c \"INSERT INTO myTable (non_keyword_columnA ,non_keyword_columnB ,\""group"\") VALUES (231,3355,(SELECT id from groups WHERE name='releventGroupName'));\""
sudo /usr/sbin/chroot environment_path_here su - username -c "psql -A -z -c \"INSERT INTO myTable (non_keyword_columnA ,non_keyword_columnB ,"\"group\"") VALUES (231,3355,(SELECT id from groups WHERE name='releventGroupName'));\""
But none of them have worked till now. I am not an expert in shell either so it may be possible that I might be making some really silly mistake here. Any help would be appreciated.
Use a here-document as standard input for your psql.
(Untested:)
#!/bin/sh
(sudo /usr/sbin/chroot environment_path_here su - username -c 'psql -A -z' ) <<OMG
INSERT INTO myTable (non_keyword_columnA ,non_keyword_columnB ,"group")
SELECT 1,2,id
FROM groups
WHERE name = 'someGroupName'
;
OMG
see also...

Programmatically update variable values in .tfvars file?

Before I go ahead and create something I wanted to check does anyone know of a CLI or other tool to update the values within a .tfvars file?
Example vars file:
ecs_desired_capacity = 1
ecs_asg_min_size = 1
What I'd like:
./somecommand ./myvars.tfvars --set="ecs_asg_min_size=2,new_var=1"
Result edit file:
ecs_desired_capacity = 1
ecs_asg_min_size = 2
new_var = 1
Thanks in advance.
You can use HCLQ (command line tool for querying and manipulating HashiCorp HCL):
cat myvars.tfvars | hclq set 'ecs_asg_min_size' 2
you can add a new variable and then check its value or update it
echo "new_var = 1" >> myvars.tfvars
cat myvars.tfvars | hclq get 'new_var'
This seems to work
#!/bin/sh
# Example usage:
# ./update_tfvars.sh example.tfvars variable_name_here 123
set -e
tfvars_file=$1
var_name=$2
var_value=$3
sed -r -i -e "s/($var_name\s*=\s*).*/\1$var_value/" $tfvars_file
Doesn't add a new entry if it doesn't exist, which fits my needs.

Shell script to fetch sql query data in csv file

Need to extract the below query data along with header in csv file using shell script.
Below is the query.
SELECT SourceIdentifier,SourceFileName,ProfitCentre2,PlantCode,
tax_retur ReturnPeriod,document_number DocumentNumber,TO_CHAR(invoice_generation_date,'YYYY-MM-DD')
Docume,Original,customer_name CustomerName,NVL(sns_pos,new_state_code)POS,PortCode,NEW_HSN_CODE HSNorSAC,(SGSATE+UTGSATE) Stat,(SGS+UT)StateUT,Userde FROM arbor.INV_REPO_FINA WHERE UPPER(document_type)='INV' AND UPPER(backout_flag)='VALID' AND new_gst_id_new IS NOT NULL AND new_charges<>0 AND taxable_adj=0
UNION
SELECT SourceIdentifier,SourceFileName,ProfitCentre2,PlantCode,
tax_retur ReturnPeriod,document_number DocumentNumber,TO_CHAR(invoice_generation_date,'YYYY-MM-DD')
Docume,Original,customer_name CustomerName,NVL(sns_pos,new_state_code)POS,PortCode, NEW_HSN_CODE HSNorSAC,(SGSATE+UTGSATE) Stat,(SGS+UTG)StateUT,Userde FROM arbor.INV_REPO_FINA WHERE UPPER(document_type)='INV' AND UPPER(backout_flag)='VALID' AND new_gst_id_new IS NOT NULL AND new_charges<>0 AND taxable_adj<>0
Could please let me know if below approach to fetch data using shell script is correct and script is correct.
#!/bin/bash
file="output.csv"
sqlplus -s username/password#Oracle_SID << EOF
SPOOL $file
select 'SourceIdentifier','SourceFileName','ProfitCentre2','PlantCode',
'tax_retur ReturnPeriod','document_number DocumentNumber','TO_CHAR(invoice_generation_date,'YYYY-MM-DD') Docume','Original','customer_name CustomerName','NVL(sns_pos,new_state_code)POS','PortCode','NEW_HSN_CODE HSNorSAC','(SGSATE+UTGSATE) Stat','(SGS+UT)StateUT','Userde' from dual
Union all
select 'TO_CHAR(SourceIdentifier)','TO_CHAR(SourceFileName)','TO_CHAR(ProfitCentre2)','TO_CHAR(PlantCode)',
'TO_CHAR(tax_retur ReturnPeriod)','TO_CHAR(document_number DocumentNumber)','TO_CHAR(invoice_generation_date,'YYYY-MM-DD')
Docume','TO_CHAR(Original)','TO_CHAR(customer_name CustomerName)','TO_CHAR(NVL(sns_pos,new_state_code)POS)','TO_CHAR(PortCode)','TO_CHAR(NEW_HSN_CODE HSNorSAC)','TO_CHAR((SGSATE+UTGSATE) Stat)','TO_CHAR((SGS+UT)StateUT)','TO_CHAR(Userde)' from
(SELECT SourceIdentifier,SourceFileName,ProfitCentre2,PlantCode,
tax_retur ReturnPeriod,document_number DocumentNumber,TO_CHAR(invoice_generation_date,'YYYY-MM-DD')
Docume,Original,customer_name CustomerName,NVL(sns_pos,new_state_code)POS,PortCode,NEW_HSN_CODE HSNorSAC,(SGSATE+UTGSATE) Stat,(SGS+UT)StateUT,Userde FROM arbor.INV_REPO_FINA WHERE UPPER(document_type)='INV' AND UPPER(backout_flag)='VALID' AND new_gst_id_new IS NOT NULL AND new_charges<>0 AND taxable_adj=0
UNION
SELECT SourceIdentifier,SourceFileName,ProfitCentre2,PlantCode,
tax_retur ReturnPeriod,document_number DocumentNumber,TO_CHAR(invoice_generation_date,'YYYY-MM-DD')
Docume,Original,customer_name CustomerName,NVL(sns_pos,new_state_code)POS,PortCode, NEW_HSN_CODE HSNorSAC,(SGSATE+UTGSATE) Stat,(SGS+UTG)StateUT,Userde FROM arbor.INV_REPO_FINA WHERE UPPER(document_type)='INV' AND UPPER(backout_flag)='VALID' AND new_gst_id_new IS NOT NULL AND new_charges<>0 AND taxable_adj<>0)
SPOOL OFF
EXIT
EOF
In short: the ; is missing from the end of the select statement.
Some unrequested advice:
I think spool will put extra stuff into your file (at least some new lines), a redirect is better, further the first line is not db-related:
echo "SourceIdentifier;SourceFileName;ProfitCentre2..." > $file
I recommend to generate the csv format right in the select query, later it will be more headache (you can escape there what you want):
$query = "select SourceIdentifier || ';' || SourceFileName || ';' || ProfitCentre2 ... ;"
So querying the DB (I think capital -S is the right one) plus for the formatting of the records (and maybe you want to format your columns too):
sqlplus -S username/password#Oracle_SID >> $file << EOF
set linesize 32767 pagesize 0 heading off
$query
EOF
For me this one is working but one empty line before first query and second query is coming. Empty line remove using awk command
#!/bin/bash
FILE="A.csv"
$ORACLE_HOME/bin/sqlplus -s username/password#Oracle_SID<<EOF
SET PAGESIZE 50000 COLSEP "," LINESIZE 20000 FEEDBACK OFF HEADING off
SPOOL $FILE
select 'TYPE_OF_CALL_V','SWITCH_CALL_TYPE_V','RECORD_TYPE_V','TARF_TYPE_V' from dual;
SELECT TYPE_OF_CALL_V,SWITCH_CALL_TYPE_V,RECORD_TYPE_V,TARF_TYPE_V FROM TABLE;
SPOOL OFF
EXIT
EOF
awk 'NF > 0' $FILE > out.txt
mv out.txt $FILE

Resources