Impala sql syntax

First, the database-specific statements

1. Create a database

CREATE DATABASE statement to create a new database in the Impala.

CREATE DATABASE IF NOT EXISTS database_name;

Here, IF NOT EXISTS is an optional clause. When an existing database if we use this clause only in the absence of the same name, it will create a database with the given name.

impala impala default user performs an operation, will be reported insufficient permissions problem, the solution:

 

One: to HDFS permissions granted to the specified folder

hadoop fs -chmod -R 777 hdfs://node-1:9000/user/hive

Two: haoop configuration file to set permissions for false hdfs-site.xml

 

Both methods can be.

 

The default will create a new database name in the folder path hive of several warehouse

/user/hive/warehouse/ittest.db

You can also specify hdfs path when creating the database. Note permission of the path.

hadoop fs -mkdir -p /input/impala

hadoop fs -chmod -R 777 /input/impala

create  external table  t3(id int ,name string ,age int )  row  format  delimited fields terminated  by  '\t' location  '/input/impala/external';

 

2, delete the database

Impala's DROP DATABASE statement to delete database from the Impala. Before deleting the database, it is recommended that all removed from the table.

If you use cascading deletes, Impala will remove it before deleting the specified database table.

DROP database sample cascade;

Second, the table-specific statements

 

 

1, the Create  the Table statement

CREATE TABLE desired database in the statement is used in the Impala create a new table. Need to specify the table name and define its columns and column data types.

impala hive supported data types and the like, in addition to sql type, java type supports.

create table IF NOT EXISTS database_name.table_name (

   column1 data_type,

   column2 data_type,

   column3 data_type,

   ………

   columnN data_type

);

CREATE TABLE IF NOT EXISTS my_db.student(name STRING, age INT, contact INT );

 

Data storage path default construction of the table consistent with the hive. You can also specify a specific path through the location at the time of construction of the table, need to pay attention hdfs rights issues.

 

2, INSERT statements

Impala INSERT statement has two clauses: INTO and Overwrite . for inserting into the new data record, overwrite the existing record for covering.

insert into table_name (column1, column2, column3,...columnN)

values ​​(value1, value2, value3, ... valueN);

Insert into table_name values (value1, value2, value2);

Here, column1, column2, ... columnN is the name of the column you want to insert data in the table. Value may also be added without specifying column names, however, ensure the same order as the order of columns in the table values.

for example:

create table employee (Id INT, name STRING, age INT,address STRING, salary BIGINT);

insert into employee VALUES (1, 'Ramesh', 32, 'Ahmedabad', 20000 );

insert into employee values (2, 'Khilan', 25, 'Delhi', 15000 );

Insert into employee values (3, 'kaushik', 23, 'Kota', 30000 );

Insert into employee values (4, 'Chaitali', 25, 'Mumbai', 35000 );

Insert into employee values (5, 'Hardik', 27, 'Bhopal', 40000 );

Insert into employee values (6, 'Komal', 22, 'MP', 32000 );

 

overwrite cover clause overlay table among all records . Records from the table covered permanently deleted.

Insert overwrite employee values (1, 'Ram', 26, 'Vishakhapatnam', 37000 );

 

3, the SELECT statement

Impala the SELECT statement is used to extract data from one or more database tables. This query returns the data as a table.

 

4, DESCRIBE statement

The Impala describe statement is used to provide description of the table. The result of this statement contains information about a table, such as the name and data type column.

Describe table_name;

 

In addition, you can also use the hive lookup table metadata information statement.

desc formatted table_name;

 

​​​​​​​5、alter table

Impala in the Alter table statement for a given table make changes. With this statement, we can add, delete or modify an existing column in a table, you can rename them.

Rename the table:

ALTER TABLE [old_db_name.]old_table_name RENAME TO

 [new_db_name.]new_table_name

Adding to the table columns :

ALTER TABLE name ADD COLUMNS (col_spec[, col_spec ...])

Remove columns from the table:

ALTER TABLE name DROP [COLUMN] column_name

Change the name and type of columns:

ALTER TABLE name CHANGE column_name new_name new_type

 

​​​​​​​6、delete、truncate table

Impala  drop  the Table statement to remove Impala existing table. This statement will also delete files inside HDFS underlying table.

Note: You must be careful when using this command because after you delete the table, all of the information available in the table is lost forever.

DROP table database_name.table_name;

 

The Impala Truncate the Table statement to delete all records from the existing table. Reserved table structure.

You can also use the DROP TABLE command to delete a complete list, but it will delete the complete structure of the table from the database, if you want to store some data, you will need to re-create this table.

truncate table_name;

 

7, View view

View just the query language statements Impala has an associated name stored in the database. It is based on a combination of pre-defined SQL queries in the form of a table.

View all the rows may contain a table or selected rows.

Create View IF NOT EXISTS view_name as Select statement

 

Create a view view, query view view

CREATE VIEW IF NOT EXISTS employee_view AS select name, age from employee;

 

Modify View

ALTER VIEW database_name.view_name for the Select statement

Delete View

DROP VIEW database_name.view_name;

 

8, the Order  by clause

Impala the ORDER BY clause columns in ascending or descending sort the data according to one or more. By default, some database query results in ascending order.

select * from table_name ORDER BY col_name

 [ASC|DESC] [NULLS FIRST|NULLS LAST]

Data list can be respectively ascending or descending row keywords ASC or DESC.

If we use NULLS FIRST, all null values ​​in the table are arranged in a top row; if we use NULLS LAST, contains a null value of the last rows are arranged.

 

9, Group  by clause

Impala the GROUP BY clause of a SELECT statement is used in cooperation, the same data are arranged into groups.

select data from table_name Group BY col_name;

10, the HAVING clause

Impala in Having clause allows you to specify which groups the filter results show that conditions in the final results.

Generally, Having clause with group by clause; it is placed on the set conditions created by the GROUP BY clause.

​​​​​​​11、limit、offset

The Impala limit the number of rows clause to the result set is limited to the number required, i.e., the query result set does not contain more than the specified limit record.

In general, resultset select query in a row to start from zero. Using the offset clause , we can decide where to consider output.

 

12, with clauses

If the query is too complicated, we can define an alias for complex partial, and use them Impala with clause included in the query.

with x as (select 1), y as (select 2) (select * from x union y);

For example: use with clause to display older than 25 employees and a customer's record.

with t1 as (select * from customers where age>25),

   t2 as (select * from employee where age>25)

   (select * from t1 union select * from t2);

​​​​​​​13、distinct

Impala the distinct operator is used to obtain the unique values ​​by eliminating duplicate values.

select distinct columns… from table_name;

 

Published 81 original articles · won praise 21 · views 2233

Guess you like

Origin blog.csdn.net/qq_44065303/article/details/103474425