Using beeline with multi-line statements fails - hive

I'm try to migrate a command from hive cli to beeline:
$ hive -e "USE my_db;ALTER TABLE apache_log ADD PARTITION(year = 2015, month = 3) LOCATION 'hdfs://DB//user/my_db/prod/apache_log/2015/03';"
this works.
But when doing it in beeline it doesn't like the "USE my_db;" part:
$ beeline -u jdbc:hive2://my_server.com -n my_user -p my_password -e "USE my_db;ALTER TABLE apache_log ADD PARTITION(year = 2015, month = 3) LOCATION 'hdfs://DB/user/my_user/prod/apache_log/2015/03';"
Error: Error while compiling statement: FAILED: ParseException line 1:9 missing EOF at ';' near 'my_db' (state=42000,code=40000)
Beeline version 0.12.0-cdh5.1.3 by Apache Hive
Based on the error message (expecting EOF) it seems to me that beeline would not accept multi-statements like hive cli used to do?

I have raised it as a Hive bug and was accepted as a bug and patch provided: https://issues.apache.org/jira/browse/HIVE-9877

It is possible to do this by doing it this way:
(i wrote it on more than one line to make it more readable)
beeline
-u jdbc:hive2://my_server.com
-n my_user -p my_password
-e "USE my_db;"
-e "ALTER TABLE apache_log ADD PARTITION(year = 2015, month = 3) LOCATION 'hdfs://DB/user/my_user/prod/apache_log/2015/03';"
-e "--other nice queries..."

Related

Errors ("invalid command") when opening a .sql file

I am trying to open a random .sql file off the internet using the following command:
psql -h localhost -d database_name -U postgres < file_name.sql
But when I run this command I just get errors like the following:
invalid command 's
invalid command 's
invalid command 'll
invalid command 'Moving
invalid command 's
invalid command "frequently
It just continuously prints out these invalid command error messages. I thought it might be an encoding problem but I confirmed the file is UTF-8 encoded.
Any suggestions on how I can open this file
To expand and clarify on a_horse_with_no_name's comment - the psql command you are running should be run directly in your shell, not inside pgadmin4.
youruser#yourmachine:~$ psql -h localhost -d database_name -U postgres < file_name.sql
That command should load the contents of file_name.sql in to database_name. Once it's complete, you can use pgadmin4 as normal to interact with the database.
One possibility is that the file contains tabulator keys, which are expanded if you read redirect standard input to the SQL script.
Try using the -f option:
psql -h localhost -d database_name -U postgres -f file_name.sql
Apparently the .sql file was generated through a MySQL dump. I thought it would not matter whether I used PostgreSQL or MySQL but it did. Once I installed MySQL my problem got resolved and I now have a Database ready :)

How to restore postgres 12 generated sql file into postgres 9.6 database

I am trying to restore the database. The database sql file is about 4.5 GB so I couldn't edit it on editor. I dump the database using following command in postgres 12;
pg_dump -d postgres > backup.sql
But I need the same database in postgres 9.6. For this purpose, I write the following code to restore it
psql -d postgres < backup.sql
It shows the error like this (Error in creating sequence). But this is not the duplicated one with given question.
error message;
SET
ERROR: unrecognized configuration parameter "default_table_access_method"
CREATE TABLE
ALTER TABLE
ERROR: syntax error at or near "AS"
LINE 2: AS integer
^
ERROR: relation "epicenter.epicenter_gid_seq" does not exist
ERROR: relation "epicenter.epicenter_gid_seq" does not exist
CREATE TABLE
ALTER TABLE
ERROR: syntax error at or near "AS"
LINE 2: AS integer
^
ERROR: relation "public.725_4.5_tur_gid_seq" does not exist
ERROR: relation "public.725_4.5_tur_gid_seq" does not exist
CREATE TABLE
ALTER TABLE
I saw the answer of this question. The answer already said the sql file will not work on older version. But I want to know, Is there any way to restore using this sql file?
Thank you #jjanes and #JGH for your kind cooperation. I found the one solution. First I backup the database using following command;
pg_dump -U postgres -h localhost -p 5432 -W earthquake | gzip -c > backup.gz
Then I create earthquake database manually from the pgadmin 4.
After getting the backup.gz file, I restore it using following command in terminal;
gzip -d -c backup.gz | sed -e '/AS integer/d' | psql -U postgres -h localhost -p 5432 -W earthquake

Run query in beeline from file

I want to run query stored file in beeline. This code works OK in putty.
beeline -u "hiveserver" -n "username" -p "password" --outputformat=csv2 --silent=true -e "select * from table;" >output1.txt
When I save sql command to query.hql or query.sql and upload to server where hadoop is, command does not export anything. I get no error.
beeline -u "hiveserver" -n "username" -p "password" --outputformat=csv2 --silent=true -f query.hql >output1.txt
Query in file works when I run it as !run query.hql directly in beeline.
What is wrong with my query in file approach?
Make sure you have a new line character at the end of the file. Otherwise, beeline will not execute that command rather will just print onto the beeline terminal. Please let me know if that works.
Please, check if below is the case.

How to execute postgres' sql queries from batch file?

I need to execute SQL from batch file.
I am executing following to connect to Postgres and select data from table
C:/pgsql/bin/psql -h %DB_HOST% -p 5432 -U %DB_USER% -d %DB_NAME%
select * from test;
I am able to connect to database, however I'm getting the error
'select' is not recognized as an internal or external command,
operable program or batch file.
Has anyone faced such issue?
This is one of the query i am trying, something similar works in shell script, (please ignore syntax error in the query if there are any)
copy testdata (col1,col2,col3) from '%filepath%/%csv_file%' with csv;
You could pipe it into psql
(
echo select * from test;
) | C:/pgsql/bin/psql -h %DB_HOST% -p 5432 -U %DB_USER% -d %DB_NAME%
When closing parenthesis are part of the SQL query they have to be escaped with three carets.
(
echo insert into testconfig(testid,scenarioid,testname ^^^) values( 1,1,'asdf'^^^);
) | psql -h %DB_HOST% -p 5432 -U %DB_USER% -d %DB_NAME%
Use the -f parameter to pass the batch file name
C:/pgsql/bin/psql -h %DB_HOST% -p 5432 -U %DB_USER% -d %DB_NAME% -f 'sql_batch_file.sql'
http://www.postgresql.org/docs/current/static/app-psql.html
-f filename
--file=filename
Use the file filename as the source of commands instead of reading commands interactively. After the file is processed, psql terminates. This is in many ways equivalent to the meta-command \i.
If filename is - (hyphen), then standard input is read until an EOF indication or \q meta-command. Note however that Readline is not used in this case (much as if -n had been specified).
if running on Linux, this is what worked for me (need to update values below with your user, db name etc)
psql "host=YOUR_HOST port=YOUR_PORT dbname=YOUR_DB_NAME user=YOUR_USER_NAME password=YOUR_PASSWORD" -f "fully_qualified_path_to_your_script.sql"
You cannot put the query on separate line, batch interpreter will assume it's another command instead of a query for psql. I believe you will need to quote it as well.
I agree with Spidey:
1] if you are passing the file with the sql use -f or --file parameter
When you want to execute several commands the best way to do that is to add parameter -f, and after that just type path to your file without any " or ' marks (relative paths works also):
psql -h %host% -p 5432 -U %user% -d %dbname% -f ..\..\folder\Data.txt
It also works in .NET Core. I need it to add basic data to my database after migrations.
Kindly refer to the documentation
1] if you are passing the file with the sql use -f or --file parameter
2] if you are passing individual command use -c or --command parameter
If you are trying the shell script
psql postgresql://$username:$password#$host/$database < /app/sql_script/script.sql

Extracting an existing schema in MySQL, getting an unknown error, need help

Trying to dump the schema for an existing MySQL database. Appears the best way is this command:
CMD:
mysqldump -d -u root -pPASSWORD_REMOVED MyOffice
NOTE: There is no space between "-p" and "PASSWORD_REMOVED", and PASSWORD_REMOVED equals my password; which is working. MyOffice is the database name.
OUTPUT:
mysql> use MyOffice;
Database changed
mysql> mysqldump -d -u root -pPASSWORD_REMOVED MyOffice
-> ;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 'mysql
dump -d -u root -pmysql123 MyOffice' at line 1
GOOGLE:
This is what I've Googled so far, search-1, search-2... no luck
You are executing mysqldump from the mysql prompt which is not correct! This is clear from your output.
You have to execute the mysqldump command from the system command prompt (shell), not from mysql prompt!