LOAD DATA

Description

LOAD DATA statement loads the data into a Hive serde table from the user specified directory or file. If a directory is specified then all the files from the directory are loaded. If a file is specified then only the single file is loaded. Additionally the LOAD DATA statement takes an optional partition specification. When a partition is specified, the data files (when input source is a directory) or the single file (when input source is a file) are loaded into the partition of the target table.

If the table is cached, the command clears cached data of the table and all its dependents that refer to it. The cache will be lazily filled when the next time the table or the dependents are accessed.

Syntax

  1. LOAD DATA [ LOCAL ] INPATH path [ OVERWRITE ] INTO TABLE table_identifier [ partition_spec ]

Parameters

  • path

    Path of the file system. It can be either an absolute or a relative path.

  • table_identifier

    Specifies a table name, which may be optionally qualified with a database name.

    Syntax: [ database_name. ] table_name

  • partition_spec

    An optional parameter that specifies a comma separated list of key and value pairs for partitions.

    Syntax: PARTITION ( partition_col_name = partition_col_val [ , ... ] )

  • LOCAL

    If specified, it causes the INPATH to be resolved against the local file system, instead of the default file system, which is typically a distributed storage.

  • OVERWRITE

    By default, new data is appended to the table. If OVERWRITE is used, the table is instead overwritten with new data.

Examples

  1. -- Example without partition specification.
  2. -- Assuming the students table has already been created and populated.
  3. SELECT * FROM students;
  4. +---------+----------------------+----------+
  5. | name| address|student_id|
  6. +---------+----------------------+----------+
  7. |Amy Smith|123 Park Ave, San Jose| 111111|
  8. +---------+----------------------+----------+
  9. CREATE TABLE test_load (name VARCHAR(64), address VARCHAR(64), student_id INT) USING HIVE;
  10. -- Assuming the students table is in '/user/hive/warehouse/'
  11. LOAD DATA LOCAL INPATH '/user/hive/warehouse/students' OVERWRITE INTO TABLE test_load;
  12. SELECT * FROM test_load;
  13. +---------+----------------------+----------+
  14. | name| address|student_id|
  15. +---------+----------------------+----------+
  16. |Amy Smith|123 Park Ave, San Jose| 111111|
  17. +---------+----------------------+----------+
  18. -- Example with partition specification.
  19. CREATE TABLE test_partition (c1 INT, c2 INT, c3 INT) PARTITIONED BY (c2, c3);
  20. INSERT INTO test_partition PARTITION (c2 = 2, c3 = 3) VALUES (1);
  21. INSERT INTO test_partition PARTITION (c2 = 5, c3 = 6) VALUES (4);
  22. INSERT INTO test_partition PARTITION (c2 = 8, c3 = 9) VALUES (7);
  23. SELECT * FROM test_partition;
  24. +---+---+---+
  25. | c1| c2| c3|
  26. +---+---+---+
  27. | 1| 2| 3|
  28. | 4| 5| 6|
  29. | 7| 8| 9|
  30. +---+---+---+
  31. CREATE TABLE test_load_partition (c1 INT, c2 INT, c3 INT) USING HIVE PARTITIONED BY (c2, c3);
  32. -- Assuming the test_partition table is in '/user/hive/warehouse/'
  33. LOAD DATA LOCAL INPATH '/user/hive/warehouse/test_partition/c2=2/c3=3'
  34. OVERWRITE INTO TABLE test_load_partition PARTITION (c2=2, c3=3);
  35. SELECT * FROM test_load_partition;
  36. +---+---+---+
  37. | c1| c2| c3|
  38. +---+---+---+
  39. | 1| 2| 3|
  40. +---+---+---+