How to registerTempTable in SparkSQL - apache-spark-sql

Spark version:2.2.0.cloudera2
Usually, we register a temp table in this way:
dataframe.registerTempTable($table_name)
But if I want to create a table in SQL statement, like this:
CREATE TABLE test_table from select * from table1
Spark will create a permanent table. Is there some way to create a temp table in SparkSQL statement?

You need to add TEMPORARY keyword in the SQL statement which would restrict writing the records to hive metastore for that particular table.
CREATE TEMPORARY TABLE test_table from select * from table1
Refer: https://docs.databricks.com/spark/latest/spark-sql/language-manual/create-table.html

Related

I am not able to create temporary table in Bigquery

got below error while trying to create temporary table in Bigquery .
create or replace temporary table mss.Business.test2 as
select * from mss.Business.registration
Query error: Temporary tables may not be qualified at [2:36]
As mentioned by #Jaytiger names are not included in the CREATE TEMP TABLE statement.
You can create temporary table to store the results of a query as follows:
CREATE OR REPLACE TEMP TABLE <table1> as (SELECT * FROM `<dataset>.<table2>`);
SELECT * from <table1>
You can follow this documentation for further examples on Temp tables.

#temp table equivalent in Postgres?

In Sql Server, we can create temp table in sql editor like :
select * into #temp_sales_data from sales_data; -- this will create a temp table and insert data.
Please let us know what is the equivalent option in Postgres.
You just create one using create temp table
create temp table temp_sales_data
as
select *
from sales_data;

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;

how to create table same as another table using CTAS and location in hive?

I write a query:-
create external table **table** LOCATION '**hdfs_folder**' as
select
column1, cloumn2,...
from **another_table**
where
**where_condition**
how to do that in hive version Hive 0.12.0-cdh5.1.2?
You can use create table like statement to achieve that, e.g.
CREATE EXTERNAL TABLE IF NOT EXISTS db_name.table_name
LIKE existing_table_or_view_name
LOCATION hdfs_path;
Check out the manual for more details

Temporary tables and AS statement usage

If I have a query, say:
SELECT *
FROM ( SELECT fields
FROM tables
WHERE condition
)
AS TEMP_TABLE
are the results of the above query saved in a temporary table called TEMP_TABLE so as I can perform another query on it later? Will the query below be executed successfully when using DB2?
SELECT fields
FROM TEMP_TABLE
WHERE condition
The answer is NO, it's just an alias for a subquery.
If you want to use it later, you have to explicitly create it.
you can create temporary table in following way.
CREATE TEMPORARY TABLE temp_table AS (
SELECT fields
FROM tables
WHERE condition
);
then you can retrieve data from the temp table as below.
SELECT * FROM temp_table