development

Hive 내부 테이블과 외부 테이블의 차이점은 무엇입니까?

big-blog 2020. 8. 13. 07:45
반응형

Hive 내부 테이블과 외부 테이블의 차이점은 무엇입니까?


누구든지 Hive의 외부 테이블과 내부 테이블의 차이점을 말할 수 있습니까? 테이블을 떨어 뜨릴 때 차이가 있다는 것을 알고 있습니다. 데이터와 메타 데이터가 내부에서 삭제되고 외부 테이블에서 메타 데이터 만 삭제된다는 의미를 이해하지 못합니다. 누구든지 노드 측면에서 나를 설명해 주시겠습니까?


Hive는 상태를 추적하는 데 사용하는 마스터 노드에 관계형 데이터베이스를 가지고 있습니다. 예를 들어, CREATE TABLE FOO(foo string) LOCATION 'hdfs://tmp/';이 테이블 스키마는 데이터베이스에 저장됩니다.

파티션 된 테이블이있는 경우 파티션은 데이터베이스에 저장됩니다 (이를 통해 하이브는 파일 시스템으로 이동하여 파티션을 찾지 않고도 파티션 목록을 사용할 수 있습니다). 이러한 것들이 '메타 데이터'입니다.

내부 테이블을 삭제하면 데이터가 삭제되고 메타 데이터도 삭제됩니다.

외부 테이블을 삭제하면 메타 데이터 만 삭제됩니다. 즉, 하이브는 이제 해당 데이터를 알지 못합니다. 데이터 자체를 건드리지 않습니다.


Hive 테이블은 EXTERNAL 또는 INTERNAL로 만들 수 있습니다. 이것은 데이터가로드, 제어 및 관리되는 방식에 영향을주는 선택입니다.

다음과 같은 경우 EXTERNAL 테이블을 사용합니다.

  1. 데이터는 Hive 외부에서도 사용됩니다. 예를 들어, 데이터 파일은 파일을 잠그지 않는 기존 프로그램에서 읽고 처리합니다.
  2. 데이터는 DROP TABLE 후에도 기본 위치에 남아 있어야합니다. 이는 단일 데이터 세트에서 여러 스키마 (테이블 또는 뷰)를 가리 키거나 가능한 다양한 스키마를 반복하는 경우에 적용될 수 있습니다.
  3. ASV와 같은 사용자 지정 위치를 사용하려고합니다.
  4. Hive는 데이터 및 제어 설정, 디렉토리 등을 소유해서는 안되며 이러한 작업을 수행 할 다른 프로그램이나 프로세스가 있습니다.
  5. 기존 테이블 (AS SELECT)을 기반으로 테이블을 작성하지 않습니다.

다음과 같은 경우 내부 테이블을 사용합니다.

데이터는 일시적입니다.

Hive가 테이블 및 데이터의 수명주기를 완전히 관리하기를 원합니다.


질문에 대답하려면 :

외부 테이블의 경우 Hive는 데이터를웨어 하우스 디렉터리로 이동하지 않습니다. 외부 테이블이 삭제되면 테이블 메타 데이터는 삭제되지만 데이터는 삭제되지 않습니다.

내부 테이블의 경우 Hive는 데이터를웨어 하우스 디렉터리로 이동합니다. 테이블이 삭제되면 테이블 메타 데이터와 데이터가 삭제됩니다.


참고로

내부 테이블과 외부 테이블의 차이점 :

외부 테이블의 경우-

  • 외부 테이블은 HDFS 서버에 파일을 저장하지만 테이블은 소스 파일에 완전히 연결되지 않습니다.

  • 외부 테이블을 삭제해도 파일은 HDFS 서버에 남아 있습니다.

    예를 들어 당신은 만들 경우 외부 테이블 라는 "table_test" HIVE-QL을 사용하여 하이브와 파일에 테이블을 연결 "파일" , 다음 HDFS에서 "파일"을 삭제하지 않습니다 HIVE에서 "table_test"을 삭제 .

  • 외부 테이블 파일은 HDFS 파일 구조에 대한 액세스 권한이있는 모든 사용자가 액세스 할 수 있으므로 HDFS 파일 / 폴더 수준에서 보안을 관리해야합니다.

  • 메타 데이터는 마스터 노드에 유지되며 HIVE에서 외부 테이블을 삭제하면 데이터 / 파일이 아닌 메타 데이터 만 삭제됩니다.


내부 테이블 용-

  • 의 설정에 따라 디렉토리에 저장 hive.metastore.warehouse.dir, 기본 내부 테이블로 저장되어 다음 디렉토리에 "/ 사용자 / 하이브 / 창고" 당신은 설정 파일의 위치를 업데이트하여 변경할 수 있습니다.
  • 테이블을 삭제하면 마스터 노드와 HDFS에서 각각 메타 데이터와 데이터가 삭제됩니다.
  • 내부 테이블 파일 보안은 HIVE를 통해서만 제어됩니다. 보안은 아마도 스키마 수준 (조직에 따라 다름)에서 HIVE 내에서 관리되어야합니다.

Hive에는 내부 또는 외부 테이블이있을 수 있으며 이는 데이터로드, 제어 및 관리 방식에 영향을주는 선택입니다.

다음과 같은 경우 EXTERNAL 테이블을 사용합니다.

  • 데이터는 하이브 사용될 밖에 . 예를 들어, 데이터 파일은 파일을 잠그지 않는 기존 프로그램에서 읽고 처리합니다.
  • 데이터는 DROP TABLE 후에도 기본 위치에 남아 있어야합니다. 이는 단일 데이터 세트에서 여러 스키마 (테이블 또는 뷰)를 가리 키거나 가능한 다양한 스키마를 반복하는 경우에 적용될 수 있습니다.
  • Hive는 데이터 및 제어 설정, 디렉토리 등을 소유해서는 안됩니다 . 이러한 작업을 수행 할 다른 프로그램이나 프로세스가있을 수 있습니다.
  • 기존 테이블 (AS SELECT)을 기반으로 테이블을 작성하지 않습니다.

다음과 같은 경우 내부 테이블을 사용합니다.

  • 데이터는 일시적입니다 .
  • Hive가 테이블 및 데이터의 수명주기를 완전히 관리하기를 원합니다 .

출처 :

HDInsight : Hive 내부 및 외부 테이블 소개

Hadoop- HIVE의 내부 및 외부 테이블


내부 테이블 데이터는웨어 하우스 폴더에 저장되는 반면 외부 테이블 데이터는 테이블 생성에서 언급 한 위치에 저장됩니다.

따라서 내부 테이블을 삭제하면웨어 하우스 폴더 아래의 데이터뿐만 아니라 스키마도 삭제되지만 외부 테이블의 경우 손실되는 스키마 일뿐입니다.

따라서 외부 테이블을 삭제 한 후 다시 되돌리려면 동일한 스키마로 테이블을 다시 만들고 원래 데이터 위치를 가리킬 수 있습니다. 이제 분명해지기를 바랍니다.


외부 테이블에서 드롭하면 테이블의 스키마 만 삭제되며 테이블 데이터는 물리적 위치에 존재합니다. 따라서 데이터를 삭제하려면 hadoop fs-rmr tablename을 사용하십시오. 관리되는 테이블 하이브는 테이블에 대한 모든 권한을 갖습니다. 외부 테이블에서는 사용자가 제어 할 수 있습니다.


The only difference in behaviour (not the intended usage) based on my limited research and testing so far (using Hive 1.1.0 -cdh5.12.0) seems to be that when a table is dropped

  • the data of the Internal (Managed) tables gets deleted from the HDFS file system
  • while the data of the External tables does NOT get deleted from the HDFS file system.

(NOTE: See Section 'Managed and External Tables' in https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DDL which list some other difference which I did not completely understand)

I believe Hive chooses the location where it needs to create the table based on the following precedence from top to bottom

  1. Location defined during the Table Creation
  2. Location defined in the Database/Schema Creation in which the table is created.
  3. Default Hive Warehouse Directory (Property hive.metastore.warehouse.dir in hive.site.xml)

When the "Location" option is not used during the "creation of a hive table", the above precedence rule is used. This is applicable for both Internal and External tables. This means an Internal table does not necessarily have to reside in the Warehouse directory and can reside anywhere else.

Note: I might have missed some scenarios, but based on my limited exploration, the behaviour of both Internal and Extenal table seems to be the same except for the one difference (data deletion) described above. I tried the following scenarios for both Internal and External tables.

  1. Creating table with and without Location option
  2. Creating table with and without Partition Option
  3. Adding new data using the Hive Load and Insert Statements
  4. Adding data files to the Table location outside of Hive (using HDFS commands) and refreshing the table using the "MSCK REPAIR TABLE command
  5. Dropping the tables

Internal tables are useful if you want Hive to manage the complete lifecycle of your data including the deletion, whereas external tables are useful when the files are being used outside of Hive.


External hive table has advantages that it does not remove files when we drop tables,we can set row formats with different settings , like serde....delimited


Also Keep in mind that Hive is a big data warehouse. When you want to drop a table you dont want to lose Gigabytes or Terabytes of data. Generating, moving and copying data at that scale can be time consuming. When you drop a 'Managed' table hive will also trash its data. When you drop a 'External' table only the schema definition from hive meta-store is removed. The data on the hdfs still remains.


Consider this scenario which best suits for External Table:

A MapReduce (MR) job filters a huge log file to spit out n sub log files (e.g. each sub log file contains a specific message type log) and the output i.e n sub log files are stored in hdfs.

These log files are to be loaded into Hive tables for performing further analytic, in this scenario I would recommend an External Table(s), because the actual log files are generated and owned by an external process i.e. a MR job besides you can avoid an additional step of loading each generated log file into respective Hive table as well.


The best use case for an external table in the hive is when you want to create the table from a file either CSV or text


hive stores only the meta data in metastore and original data in out side of hive when we use external table we can give location' ' by these our original data wont effect when we drop the table


When there is data already in HDFS, an external Hive table can be created to describe the data. It is called EXTERNAL because the data in the external table is specified in the LOCATION properties instead of the default warehouse directory.

When keeping data in the internal tables, Hive fully manages the life cycle of the table and data. This means the data is removed once the internal table is dropped. If the external table is dropped, the table metadata is deleted but the data is kept. Most of the time, an external table is preferred to avoid deleting data along with tables by mistake.


For managed tables, Hive controls the lifecycle of their data. Hive stores the data for managed tables in a sub-directory under the directory defined by hive.metastore.warehouse.dir by default.

When we drop a managed table, Hive deletes the data in the table.But managed tables are less convenient for sharing with other tools. For example, lets say we have data that is created and used primarily by Pig , but we want to run some queries against it, but not give Hive ownership of the data.

At that time, external table is defined that points to that data, but doesn’t take ownership of it.


INTERNAL : Table is created First and Data is loaded later

EXTERNAL : Data is present and Table is created on top of it.


In Hive We can also create an external table. It tells Hive to refer to the data that is at an existing location outside the warehouse directory. Dropping External tables will delete metadata but not the data.


In simple words, there are two things:

Hive can manage things in warehouse i.e. it will not delete data out of warehouse. When we delete table:

1) For internal tables the data is managed internally in warehouse. So will be deleted.

2) For external tables the data is managed eternal from warehouse. So can't be deleted and clients other then hive can also use it.

참고URL : https://stackoverflow.com/questions/17038414/difference-between-hive-internal-tables-and-external-tables

반응형