I use beeline from the command line quite a lot, so I would like to create a bash alias for the beeline command that takes care of all the boilerplate for me and does some argument parsing and things. Specifically, one thing I would like to do is create a showtables command which takes a single argument, the name of a database, and invokes the beeline command with the boiler plate and passes the -e argument along with the appropriate SQL, i.e. - showtables db1 should invoke /usr/bin/beeline -u $config -e "SHOW TABLES IN db1". My source file looks like this :
/usr/bin/beeline --showHeader=False --outputformat=tsv2 -u $config -e \"SHOW TABLES IN $1\"
but the output is
...
Error: Error while compiling statement: FAILED: ParseException line 1:4 cannot recognize input near 'SHOW' '<EOF>' '<EOF>' in ddl statement (state=42000,code=40000)
Error: Error while compiling statement: FAILED: ParseException line 1:0 cannot recognize input near 'TABLES' '<EOF>' '<EOF>' (state=42000,code=40000)
Error: Error while compiling statement: FAILED: ParseException line 1:0 cannot recognize input near 'IN' '<EOF>' '<EOF>' (state=42000,code=40000)
Error: Error while compiling statement: FAILED: ParseException line 1:0 cannot recognize input near 'db1' '<EOF>' '<EOF>' (state=42000,code=40000)
...
I've verified that just doing
/usr/bin/beeline --showHeader=False --outputformat=tsv2 -u $config -e "SHOW TABLES IN db1"
works as expected. I don't understand why my executable isn't working though.
The backslashes before your quotes make them behave like regular characters from the perspective of parsing, and not like quotes anymore. Thus, when you put backslashes in front of the quotes, SHOW TABLES IN db1 is no longer a string, but is four separate strings: "SHOW, TABLES, IN, and db1".
These words are not valid queries when run on their own -- hence your error. Take out the backslashes and the issue goes away.
Related
I am running the below commands in Hive and have already imported the table 'sales_withcomma' however still now working
SELECT * FROM 'default'.'sales_withcomma'
ALTER TABLE sales_withcomma SET SERDE 'com.bizo.hive.serde.csv.CSVSerde'
Use double quotes, not single quotes around tables/schemas. But using quotes is a bad practice for tables (check this question for an explanation why that is the case). This should work:
SELECT * FROM default.sales_withcomma;
This question already has answers here:
When to wrap quotes around a shell variable?
(5 answers)
Closed 2 years ago.
I'm trying to do a script that runs queries in PostgreSQL. I'm struggling with something that seems pretty simple (but does not work...).
I have a set of queries stored as an array of strings, lets' say
query1="SELECT COUNT(id) FROM nodes WHERE label='KeywordList';"
query2="SELECT COUNT(id) FROM nodes WHERE label='AuthorList';"
queries=($query1 $query2)
Then, I want to run this query in PSQL through a loop:
for query in "${queries[#]}"
do
psql $db_name -c "$query"
done
This gives me the following error:
ERROR: syntax error at or near "COUNT"
LINE 1: COUNT(id)
^
ERROR: syntax error at or near "FROM"
LINE 1: FROM
^
ERROR: syntax error at or near "nodes"
LINE 1: nodes
^
ERROR: syntax error at or near "WHERE"
LINE 1: WHERE
^
ERROR: syntax error at or near "label"
LINE 1: label='KeywordList';
But this works when I give to PSQL the string itself: psql $db_name -c "$query1" (this returns me 10).
I think that this is a quoting problem, or maybe Bash tries to understand what is in the string... I can not resolve this on my own after many tries with double, single and back quotes.
Help will be really appreciated, Nelly.
As is:
$ query1="SELECT COUNT(id) FROM nodes WHERE label='KeywordList';"
$ query2="SELECT COUNT(id) FROM nodes WHERE label='AuthorList';"
$ queries=($query1 $query2)
$ typeset -p queries
declare -a queries=([0]="SELECT" [1]="COUNT(id)" [2]="FROM" [3]="nodes" [4]="WHERE" [5]="label='KeywordList';" [6]="SELECT" [7]="COUNT(id)" [8]="FROM" [9]="nodes" [10]="WHERE" [11]="label='AuthorList';")
Notice the queries have been broken into separate tokens and stored as separate array entries.
As with your psql example that works, you need to quote the query variables when referencing them ... to include when populating the array, eg:
$ queries=("$query1" "$query2")
$ typeset -p queries
declare -a queries=([0]="SELECT COUNT(id) FROM nodes WHERE label='KeywordList';" [1]="SELECT COUNT(id) FROM nodes WHERE label='AuthorList';")
Trying to declare a variable in Hive using Hue online. Using the following code:
SET hivevar:TABLE1=location.tablename;
I am getting the following error message:
Error while compiling statement: FAILED: ParseException line 1:12 missing KW_ROLE at 'hivevar' near 'hivevar' line 1:19 missing EOF at ':' near 'hivevar'.
Can anyone tell me what this error message means or even what the KW_ROLE statement means?
Do you by any chance have a comment above that instruction ? Are you running that line and that line only ?
For example, the following will raise a similar Exception :
--This is a comment
SET hivevar:TABLE1=location.tablename;
But it works fine without the comment.
I guess you are making changes in MAC/Windows and moving the script to the server, Double dash "--" in MAC is a different from double dash "--" on Linux server, make changes on server itself and run the script...
I have 2 scripts:
/home/bin/test.sh
#!/bin/bash
. /home/bin/test_functions.sh
test
/home/bin/test_functions.sh
#!/bin/sh
test()
{
echo "this is a test"
}
I wanted to call the function from the external script and execute it in the main script. Yet I've been receiving these errors:
'home/bin/test_functions.sh: line 2: syntax error near unexpected token `
'home/bin/test_functions.sh: line 2: `test()
What could be wrong with what I'm doing?
It appears that test_functions.sh is in DOS format and bash is choking on the \r\n line endings. Use dos2unix to convert it to UNIX line endings.
You can tell because what bash is trying to output is this:
/home/bin/test_functions.sh: line 2: syntax error near unexpected token `\r'
/home/bin/test_functions.sh: line 2: `test()\r'
But the carriage returns \r cause the single quotes to end up at the beginning of the error messages, overwriting the leading /.
The following script is intended to run the program "senna" on all files in a directory and write the output for each file (preserving the input file name) into another directory
for file in ./Data/in/*;
do
./senna -iobtags -usrtokens -posvbs -srl < $file > ./Data/out/$file
done
On trying to execute the script, the following error arises.
-bash-4.0$ sh run.s
'un.s: line 1: syntax error near unexpected token `
'un.s: line 1: `for file in ./Data/in/*;
The script has the lines of code exactly as above and there is no `. Perhaps it implies something else. Help with error resolution would be appreciated.
The line endings in the script are wrong. Pass it through dos2unix to eliminate the CRs.