3

I want to create table from some metadata of another table in hive. I'm using hive.

I know from this question that the metatdata can be retrieved from the table by INFORMATION_SCHEMA.COLUMNS in sql:

Does HIVE have a similar access to metadata of a table to allow me to create a table using the columns of another table? Essentially, I'm copying a table without all of the tuples.

This is the best thing I have so far:

create table <table_name>( (select <table_name> from INFORMATION_SCHEMA.COLUMNS)) row format delimited fields by '|';

Community
  • 1
  • 1
makansij
  • 9,303
  • 37
  • 105
  • 183

3 Answers3

20

You can use LIKE so the new table gets the structure but not the data.

Hive documentation

CREATE TABLE yourtable
LIKE table2;
Vamsi Prabhala
  • 48,685
  • 4
  • 36
  • 58
10

You can try CTAS syntax:

CREATE TABLE new_table_name
AS
SELECT *
FROM old_table_name
WHERE 1 = 2;
Lukasz Szozda
  • 162,964
  • 23
  • 234
  • 275
0

I believe this will do the trick:

DROP TABLE IF EXISTS schema.New_Table;    
CREATE TABLE New_Table    
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\034' LINES TERMINATED BY '\012'
STORED AS TEXTFILE location '/IC/New_Table'   
AS   
select *   
from Old_Table;
o-90
  • 17,045
  • 10
  • 39
  • 63
Aaron Faltesek
  • 319
  • 2
  • 11
  • What does the '\034' do? – makansij Nov 13 '15 at 20:36
  • It specifies the delimiter. '\034' is the serde code for '\x1c' I believe. I think if you take out that portion it just defaults to the default delim. Add the where 1=2 if you don't want to bring in any data. Run the following to see what the old_table uses. "show create table Old_Table". This will display what the old table uses as it's delimiters and you can use those for your new table also. – Aaron Faltesek Nov 13 '15 at 20:40
  • also to prevent from going through each record do this: "select * from old_table limit 1;" – Aaron Faltesek Nov 13 '15 at 20:50