hive load data:how to specify file column separator and dynamic partition columns? - hadoop

well I had some question on loading mysql data into hive2, and don't know how to specify the separator, I tried for serval times but got nothing.
Here below is the hive table,id is the partition column,
0: jdbc:hive2://localhost/> desc test;
+-----------+------------+----------+
| col_name | data_type | comment |
+-----------+------------+----------+
| a | string | |
| id | int | |
+-----------+------------+----------+
When i execute
load data local inpath 'file:///root/test' into table test partition (id=1);
it says:
Invalid path ''file:///root/test'': No files matching path file
but it do exists.
I wish to dynamic partitioned by the specified file,so i add the very column into the file like this:
root#<namenode|~>:#cat /root/test
a,1
b,2
but it also failed,the docs say nothing about this,i guess it doesn't support right now.
dose anyone got some idea in it? any help will be appreciated!

If you want to specify column sperators it uses the command;
ROW FORMAT DELIMITED
FIELDS TERMINATED BY ','
Replace the ',' with your separator
Also if you want to partition a Hive table you specify the column which you want to terminate on using;
CREATE TABLE Foo (bar int )
PARTITIONED BY (testpartition string)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY ','

Related

Oracle to psql inquiry using shell script

Can someone help me convert this to psql? i'm using shell script for this.
options (silent = (header,feedback), errors = 999999999)
load data
infile "stat.dat"
"str '\n'"
append into table DT.DETAIL_STAT
fields terminated by '|'
trailing nullcols
(
SCAN_DT "TO_DATE(:SCAN_DT, 'mm/dd/yyyy')",
SCAN_TM "TO_DATE(:SCAN_TM, 'HH24:MI:SS ')",
SCAN_NBR,
SCANNED_NBR_TYP_CD,
UPC_ID,
PROMO_IND_CD,
EXPRESS_PROC_IND,
SCAN_TYP_CD,
USER_INITIAL_ID,
RLSE_DTE "TO_DATE(:RLSE_DTE, 'mm/dd/yyyy')",
RLSE_TM "TO_DATE(:RLSE_TM, 'HH24:MI:SS')"
)
I've tried to use copy but having error on the ones with date and time
here is the content of the file
09/21/2020|10:58:34|88570294 | |0| | |O| |09/21/2020|11.06.31
09/21/2020|10:58:34|88630062 | |0| | |O| |09/21/2020|11.06.31
09/21/2020|10:58:34|001853724148 |U|1853724148|N|Y|O| |09/21/2020|11.06.31

Export hql output to csv in beeline

I am trying to export my hql output to csv in beeline using below command :
beeline -u "jdbc:hive2://****/;serviceDiscoveryMode=zooKeeper;zooKeeperNamespace=hiveserver2"?tez.queue.name=devices-jobs --outputformat=csv2 -e "use schema_name; select * from table_name where open_time_new>= '2020-07-13' and open_time_new < '2020-07-22'" > filename.csv
The problem is that some column values in the table contains commas which pushes the data of same column to the next column value.
For eg:
| abcd | as per data,outage fault,xxxx.
| xyz |as per the source,ghfg,hjhjg.
The above data will get saved as 4 column instead of 2.
Need help!
Try the approach with local directory:
insert overwrite local directory '/tmp/local_csv_report'
row format delimited fields terminated by "," escaped by '\\'
select *
from table_name
where open_time_new >= '2020-07-13'
and open_time_new < '2020-07-22'
This will create several csv files under your local /tmp/local_csv_report directory, so using simple cat after that will merge the results into a single file.

How to export a Hive table into a CSV file including header?

I used this Hive query to export a table into a CSV file.
hive -f mysql.sql
row format delimited fields terminated by ','
select * from Mydatabase,Mytable limit 100"
cat /LocalPath/* > /LocalPath/table.csv
However, it does not include table column names.
How to export in csv the column names ?
show tablename ?
You should add set hive.cli.print.header=true; before your select query to get column names as the first row of your output. The output would look as Mytable.col1, Mytable.col2 ....
If you don't want the table name with the column names, use set hive.resultset.use.unique.column.names=false;. The first row of your output would then look like col1, col2 ...
Invoking hive command-line with the parameters suggested in the other answer here works for a plain select. So, you can extract the column names and create the csv to start with, as follows:
hive -S --hiveconf hive.cli.print.header=true --hiveconf hive.resultset.use.unique.column.names=false --database Mydatabase -e 'select * from Mytable limit 0;' > /LocalPath/table.csv
Post which you can have the actual data extraction part run, except this time, remember to append to the csv:
cat /LocalPath/* >> /LocalPath/table.csv ## From your question with >> for append

Hive change column name without knowing column data type

I want to change the column name of a Hive table without changing it's datatype.
I tried below query but it requires datatype which I don't know.
ALTER TABLE test CHANGE a a1 INT;
I would like to prefix SALES_ before all my columns irrespective of their column types.
Input Table
emp_id(int) emp_name(string) salary(double)
Output Table
sales_emp_id(int) sales_emp_name(string) sales_salary(double)
Thanks in advance.
Well, altering the column name in hive using alter table command require its datatype.
For this purpose you may perform the below commands,
1)Create a new table with the your new column names)
create table newTable (sales_emp_id int ,sales_emp_name string, sales_salary double) ;
2)Insert into new table from old table
insert into newTable select * from oldtable;
3)Now,you may drop your old table.
drop table oldtable;
The above code may be used if creating a new table sounds ok for you.
Well if you use a shell script , something like below:
while read line;do
SOURCE_TABLENAME= `echo $line| awk -F" " '{print $1}'`
TARGET_TABLENAME= `echo $line| awk -F" " '{print $2}'`
LOC=`echo "$line"| awk -F" " '{print $3}'`
PREFIX="emp_"
S=`hive -e "desc $SOURCE_TABLENAME"`
VAL=echo $S |sed 's/\(\(\w\w*\W*\)\{2\}\)/\1\n/g' | sed 's/$/,/g' | sed -e 's/^/$PREFIX/'
STATEMENT="CREATE TABLE $SOURCE_TABLENAME (`echo $VAL) as select * from $SOURCE_TABLENAME LOCATION $LOC`"
hive -e "drop table $SOURCE_TABLENAME"
done < INPUT_FILE.txt
INPUT_FILE.txt
source_table target_table location (all inputs separated by space)
Without creating new table, you can use the REPLACE function in hive to change all the column names. The command looks like this
ALTER TABLE table_name REPLACE COLUMNS (sales_emp_id INT,sales_emp_name STRING,sales_salary DOUBLE);
Now you can use the describe command to check the column names
describe table_name;

Hive table creation error through Bash Shell

Can anyone give me why I am getting error while creating partitioed table from bash shell.
[cloudera#localhost ~]$ hive -e "create table peoplecountry (
name1 string,
name2 string,
salary int,
country string
)
partitioned by (country string)
row format delimited
column terminated by '\n'";
Logging initialized using configuration in jar:file:/usr/lib/hive/lib/hive-common-0.10.0-cdh4.7.0.jar!/hive-log4j.properties
Hive history file=/tmp/cloudera/hive_job_log_0fdf7083-8ab4-499f-8048-a85f162d1357_376056456.txt
FAILED: ParseException line 8:0 missing EOF at 'column' near 'delimited'
If you meant newline at end of each row of your data then you need to use:
line terminated by '\n'
instead of column terminated by ,
In case you meant each column in the row to separated by a delimiter , then specify as
fields terminated by '\n'
refer :
https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DDL

Resources