Creating databases and tables is an essential knowledge of learning big data, so how do databases and tables be created? What's the difference between creating a database and a table using Impala and hive?
Databases and tables are created and managed through HIVEQL or Impalasql DDL statements, very similar to standard SQL DDL. But they have their own different syntax, and this difference will be put forward in the following, if not mentioned is the two interlinked.
First, create a database
Hive and Impala databases are simple namespaces that are used to unify the management of a subset of tables and views. The database in HDFs is a directory that is built on a table.
(1) Create a new database:
650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M00/8C/67/wKiom1hrT0niH41fAAAolhxGPII677.png-wh_500x0-wm_3 -wmp_4-s_3507321855.png "title=" 11.png "alt=" Wkiom1hrt0nih41faaaolhxgpii677.png-wh_50 "/>
This is a command that communicates with all relational databases, and doing so adds a database definition to the metabase and creates a storage directory in HDFs, such as:/user/hive/warehouse/loudacre.db
(2) You can create a new database with conditions
650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M00/8C/63/wKioL1hrT1TwSlsqAAAIC3atPFM513.png-wh_500x0-wm_3 -wmp_4-s_1531164893.png "title=" 22.png "alt=" Wkiol1hrt1twslsqaaaic3atpfm513.png-wh_50 "/>
This is a friendly way of writing, that is, when you want to put the creation of the database into the script to execute, the premise must ensure that the script can be repeated execution, there will be no failure, so you can add if not EXISTS. When this database does not exist, the statement can run normally, to create a database, and if it exists, proceed to the next step. Executed in a script without worrying that execution will fail, at least not in this step of execution.
(3) Removing the database
Remove the database and create a similar, just replace create for drop
650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M01/8C/63/wKioL1hrT2LDBkeqAAANZTFzgi0430.png-wh_500x0-wm_3 -wmp_4-s_3495999214.png "title=" 33.png "alt=" Wkiol1hrt2ldbkeqaaanztfzgi0430.png-wh_50 "/>
However, if the database contains some tables, the execution of the above command will fail. Of course there are cases where we need to execute this command directly, and then let it all delete all the table and structure information in the database, then we execute the following command:
650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M02/8C/64/wKioL1hrT22zDpP5AABH2Drgmf4369.png-wh_500x0-wm_3 -wmp_4-s_1446318434.png "title=" 44.png "alt=" Wkiol1hrt22zdpp5aabh2drgmf4369.png-wh_50 "/>
Second, create a table
(1) Data type:
When we go to create a table, we need to specify the column, then this column also has its own specific data type, when we create the table need to specify the data type, the non-conforming type of data will return NULL. Common data types:
650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M00/8C/67/wKiom1hrT37Szje-AABB8xBhzL8363.png-wh_500x0-wm_3 -wmp_4-s_862942174.png "title=" 55.png "alt=" Wkiom1hrt37szje-aabb8xbhzl8363.png-wh_50 "/>
The emphasis here is that hive support supports complex data types such as maps and arrays, and Impala differs depending on the version.
(2) Basic syntax:
650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M02/8C/67/wKiom1hrT43C6rCVAAAUPs_brlo456.png-wh_500x0-wm_3 -wmp_4-s_1131471422.png "title=" 66.png "alt=" Wkiom1hrt43c6rcvaaaups_brlo456.png-wh_50 "/>
When we do such an operation, it creates a subdirectory in the HDFS Data Warehouse directory. If you do not specify a database name, it will have a directory of tablename such a table under/user/hive/warehouse/, the default data warehouse directory; But if you create a db, you will create subdirectories below the db. Specific as follows:
Default database:
/user/hive/warehouse/tablename
Named database:
/user/hive/warehouse/dbname.db/tablename
(3) Create a table type:
650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M00/8C/64/wKioL1hrT57BPu0wAACuXdTE3-w755.png-wh_500x0-wm_3 -wmp_4-s_1944673252.png "title=" 77.png "alt=" Wkiol1hrt57bpu0waacuxdte3-w755.png-wh_50 "/>
What is the data that is judged in hive? It is the separation of the specified rows or columns to judge our data there are several, there are a few fields, so we must specify the row or column separation
650) this.width=650; "Src=" Http://s4.51cto.com/wyfs02/M01/8C/64/wKioL1hrT6zzOF_QAAEFM-ILm-M988.png-wh_500x0-wm_3 -wmp_4-s_3352509552.png "title=" 88.png "alt=" Wkiol1hrt6zzof_qaaefm-ilm-m988.png-wh_50 "/>
(4) Data separation
650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M00/8C/64/wKioL1hrT8OxLIvqAADeS3saDmQ699.png-wh_500x0-wm_3 -wmp_4-s_2740670406.png "title=" 99.png "alt=" Wkiol1hrt8oxlivqaades3sadmq699.png-wh_50 "/>
(5) Data storage
650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M01/8C/67/wKiom1hrT9GxL4TdAAEh76Djesw043.png-wh_500x0-wm_3 -wmp_4-s_225667741.png "title=" 100.png "alt=" Wkiom1hrt9gxl4tdaaeh76djesw043.png-wh_50 "/>
The above is based on their own experience to share the table and database creation process, if there is doubt can focus on the "Big Data CN" Public number to ask questions.
This article is from the "11872756" blog, please be sure to keep this source http://11882756.blog.51cto.com/11872756/1888588
Creation of a classic decryption database and tables