How to create an empty copy of a table in hive - hive

I have a table with lots of columns.
I don't want to write something like
CREATE TABLE IF NOT EXISTS
table1(
col1 int,
col2 String,
etc....)
Is there a fast way to create a table with the same structure, but without any data?

Try this:
CREATE TABLE some_db.T1 LIKE some_db.T2
See this manual: https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DDL#LanguageManualDDL-CreateTableLike

Related

how to create a temp table in Dremio

I would like to create a temporary table as below in dremio
Select ABC into #temp_table
any advise?
I don't think DREMIO explicitly supports temporary tables. However, it does have the ability to create tables on the fly from the results of the query using create table as (CTAS):
create table temp_table as
select ABC;

Hive table creation with a default value

I have a table in RDBMS like so:
create table test (sno number, entry_date date default sysdate).
Now I want to create a table in hive with a structure as adding a default value to a column.
Hive currently doesn't support the feature of adding default value to any column while creating a table.
As a workaround load data into a temporary table and use the insert overwrite table statement to add the current date and time into the main table.
Create a temporary table:
create table test (sno number);
Load data into the table:
Create final table:
create table final_table (sno number, createDate string);
Finally load the data from temp test table to the final table:
insert overwrite table final_table select sno, FROM_UNIXTIME( UNIX_TIMESTAMP(), 'dd/MM/YYYY' ) from test;
Hive doesn't support DEFAULT fields
Doesn't mean you can't do it, though. Just a two step process of creating one "staging" table, then inserting into a second table and selecting that "default" value.
Adding a default value to a column while creating table in hive
Since you mention,
I've table in RDBMS
You could also use your existing table, and use Sqoop to import the data into Hive.

How to copy table by spark-sql

Actually, I want to move one table to another database.
But spark don't permit this.
Then, how to copy table by spark-sql?
I already tried this.
SELECT *
INTO table1 IN new_database
FROM old_database.table1
But it was not working.
maybe try:
CREATE TABLE new_db.new_table AS
SELECT *
FROM old_db.old_table;
To preserve partitioning and storage format do the following-
Get the complete schema of the existing table by running-
show create table db.old_table
The above query will output the table schema which you can just execute after changing the path name and table name.
Then insert all the rows into the new blank table using-
insert into db.new_table select * from db.old_table
The following snippet will create a new table while preserving the definition of the "old" table.
CREATE TABLE db.new_table LIKE db.old_table;
For more info, check the doc's CREATE TABLE.

SAP HANA create table / insert into new table from select

How to create a new table and insert content of another table?
create column table my_new_table as
(select * from my_existing_table)
Another, more SAP HANA specific solution is to use the
CREATE TABLE ... LIKE <TABLE_NAME> WITH [NO] DATA ...
syntax (https://help.sap.com/saphelp_hanaplatform/helpdata/en/20/d58a5f75191014b2fe92141b7df228/content.htm#loio20d58a5f75191014b2fe92141b7df228__sql_create_table_1create_table_like_clause).
This allows more control over the physical properties of the new table.
Like SQL Server you can create a temp table right from your select, the way is a little bit different.
Just execute:
temp_table = select 1 as col1, 'lorem ipsum' as col2 from dummy;
After that, you are able to use this temp table to query data from.
Like so:
select * from :temp_table;
Table Variable Type Definition
Unfortunately, there is some limitations using that. For example, you cannot simply insert new data. For that, exists some tricks.

Create hive table from another existing table without defining schema

I have table Employee in hive which is partitioned.
Now i want to copy all the contents from Employee to another table without defining any schema like:
My first table is like:
create table Employee(Id String,FirstName String,Lastname String);
But i don't want to define the same schema for the NewEmployee table:
create table Newemployee(Id String,FirstName String,LastName String);
Since, you have not mentioned any partitioning details so I am assuming that it does not have any significance. Please correct me, if I am wrong.
The query that you are looking for would be like this:
create table Newemployee as select * from Employee;
You can also use below code:
Create table dbname.tablename LIKE existing_table_or_Viewname LOCATION hdfs-path
CREATE TABLE NewEmployee
[ROW FORMAT SERDE] (if any)
[STORED AS] Format
AS
SELECT * FROM Employee [SORT BY];
Rules while create table as create
1. The target table cannot be a partitioned table.
2. The target table cannot be an external table.
3. The target table cannot be a list bucketing table.