Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.46 KB | None | 0 0
  1. -- Hive is a data warehouse infrastructure tool
  2. -- Hive process structured data in Hadoop
  3. -- It resides on top of Hadoop to summarize Big Data, and makes querying and analyzing easy.
  4. -- It is a platform used to develop SQL type scripts to do MapReduce operations.
  5. -- Designed for OLAP not OnLine Transaction Processing (OLTP)
  6.  
  7. -- Create a database
  8. CREATE DATABASE IF NOT EXISTS userdb;
  9. CREATE SCHEMA userdb;
  10.  
  11.  
  12. CREATE TABLE IF NOT EXISTS students ( id int, name String,
  13. mark Float, city String)
  14. ROW FORMAT DELIMITED
  15. FIELDS TERMINATED BY ","
  16. LINES TERMINATED BY "\n"
  17. STORED AS TEXTFILE;
  18.  
  19.  
  20.  
  21. check where it is stored
  22. describe formatted students;
  23.  
  24. -----------------------------Inserting Data---------------------------------------------------
  25.  
  26. -- While inserting data into Hive, it is better to use LOAD DATA to store bulk records.
  27. -- There are two ways to load data: one is from local file system and second is from Hadoop file system.
  28. LOAD DATA [LOCAL] INPATH 'filepath' [OVERWRITE] INTO TABLE tablename
  29. [PARTITION (partcol1=val1, partcol2=val2 ...)]
  30.  
  31. -- LOCAL is identifier to specify the local path. It is optional.
  32. -- OVERWRITE is optional to overwrite the data in the table.
  33. -- PARTITION is optional.
  34.  
  35.  
  36. LOAD DATA LOCAL INPATH '/home/hadoop/students.csv'
  37. OVERWRITE INTO TABLE students3;
  38.  
  39.  
  40. -- Loading Data way 1
  41. CREATE external TABLE IF NOT EXISTS students2 ( id int, name String,
  42. mark Float, city String)
  43. ROW FORMAT DELIMITED
  44. FIELDS TERMINATED BY ","
  45. LINES TERMINATED BY "\n"
  46. STORED AS TEXTFILE;
  47.  
  48.  
  49. LOAD DATA LOCAL INPATH '/home/hadoop/students.csv'
  50. OVERWRITE INTO TABLE students2;
  51.  
  52.  
  53.  
  54. -- Loading Data way 2
  55. CREATE external TABLE IF NOT EXISTS students4 ( id int, name String,
  56. mark Float, city String)
  57. ROW FORMAT DELIMITED
  58. FIELDS TERMINATED BY ","
  59. LINES TERMINATED BY "\n"
  60. LOCATION '/user/studentdata';
  61.  
  62.  
  63.  
  64.  
  65.  
  66. -----------------------------Internal and External Tables --------------------------------------------------
  67.  
  68. -- Hive Internal / Managed Tables-
  69. When we create a table in Hive, it by default manages the data.
  70. We want Hive to completely manage the lifecycle of the data and table.
  71. Data is temporary
  72.  
  73. -- Hive External Tables
  74. Data is used outside of Hive.
  75. For example, the data files are read and processed by an existing program that does not lock the files.
  76. We are not creating a table based on the existing table.
  77. We need data to remain in the underlying location even after a DROP TABLE.
  78. The hive shouldn’t own data and control settings, directories etc.,
  79. we may have another program or process that will do these things.
  80.  
  81.  
  82.  
  83. -- Internal Table mentioning location to store
  84.  
  85. CREATE TABLE IF NOT EXISTS students5 ( id int, name String,
  86. mark Float, city String)
  87. ROW FORMAT DELIMITED
  88. FIELDS TERMINATED BY ","
  89. LINES TERMINATED BY "\n"
  90. STORED AS TEXTFILE
  91. LOCATION '/students5';
  92.  
  93. LOAD DATA LOCAL INPATH '/home/hadoop/students.csv'
  94. OVERWRITE INTO TABLE students5;
  95.  
  96. -- Internal Table without mentioning location to store
  97. -- will store in warehouse
  98.  
  99. CREATE TABLE IF NOT EXISTS students6 ( id int, name String,
  100. mark Float, city String)
  101. ROW FORMAT DELIMITED
  102. FIELDS TERMINATED BY ","
  103. LINES TERMINATED BY "\n"
  104. STORED AS TEXTFILE;
  105.  
  106. LOAD DATA LOCAL INPATH '/home/hadoop/students.csv'
  107. OVERWRITE INTO TABLE students6;
  108.  
  109.  
  110. -- Extrnal Table without mentioning location to store
  111. -- will store in warehouse
  112.  
  113. CREATE EXTERNAL TABLE IF NOT EXISTS students7 ( id int, name String,
  114. mark Float, city String)
  115. ROW FORMAT DELIMITED
  116. FIELDS TERMINATED BY ","
  117. LINES TERMINATED BY "\n"
  118. STORED AS TEXTFILE;
  119.  
  120. LOAD DATA LOCAL INPATH '/home/hadoop/students.csv'
  121. OVERWRITE INTO TABLE students7;
  122.  
  123.  
  124.  
  125. -----------------------------HQL Queries---------------------------------------------------
  126.  
  127. ALTER TABLE students RENAME TO student;
  128.  
  129. ALTER TABLE students CHANGE name studentname String;
  130. ALTER TABLE student RENAME TO students;
  131.  
  132. ALTER TABLE students CHANGE mark mark Double;
  133. ALTER TABLE students ADD COLUMNS (class STRING);
  134.  
  135. ALTER TABLE students CHANGE studentname name String;
  136.  
  137.  
  138. --replace all the columns
  139. ALTER TABLE students REPLACE COLUMNS (marks Double mark Float,studentname STRING name String);
  140. DROP TABLE IF EXISTS students;
  141.  
  142.  
  143. SELECT [ALL | DISTINCT] select_expr, select_expr, ...
  144. FROM table_reference
  145. [WHERE where_condition]
  146. [GROUP BY col_list]
  147. [HAVING having_condition]
  148. [ORDER BY col_list]]
  149. [LIMIT number];
  150.  
  151.  
  152. SELECT * FROM students WHERE name="Ram";
  153. SELECT * FROM students WHERE marks>90;
  154.  
  155. SELECT * FROM students s JOIN class c ON (s.class = c.id);
  156.  
  157.  
  158. CREATE VIEW topstudents AS
  159. SELECT * FROM students4 WHERE mark>90;
  160.  
  161.  
  162.  
  163.  
  164. -----------------------------PARTITIONING---------------------------------------------------
  165.  
  166.  
  167. CREATE TABLE IF NOT EXISTS student_partitioned (id int, name String,marks Float)
  168. PARTITIONED BY (city string);
  169.  
  170. set hive.exec.dynamic.partition.mode=nonstrict;
  171.  
  172.  
  173. INSERT OVERWRITE TABLE student_partitioned PARTITION(city)
  174. SELECT id,name,mark,city from students4;
  175.  
  176.  
  177.  
  178. --------static partitioning-----
  179.  
  180. Partitions are created when data is inserted into table.
  181. Usually when loading files (big files) into Hive tables static partitions are preferred.
  182. That saves your time in loading data compared to dynamic partition.
  183. You "statically" add a partition in table and move the file into the partition of the table.
  184. You can get the partition column value form the filename, day of date etc without reading the whole big file.
  185.  
  186. in static partitioning we need to specify the partition column value in each and every LOAD statement.
  187.  
  188. suppose we are having partition on column country for table t1(userid, name,occupation, country), so each time we need to provide country value
  189.  
  190. hive>LOAD DATA INPATH '/hdfs path of the file' INTO TABLE t1 PARTITION(country="US")
  191. hive>LOAD DATA INPATH '/hdfs path of the file' INTO TABLE t1 PARTITION(country="UK")
  192.  
  193. --------daynamic partitioning-----------
  194.  
  195. Incase of dynamic partition whole big file i.e. every row of the data is read and data is partitioned through a MR job into the destination tables depending on certain field in file. So usually dynamic partition are useful when you are doing sort of a ETL flow in your data pipeline.
  196.  
  197. dynamic partition allow us not to specify partition column value each time. the approach we follows is as below:
  198.  
  199. create a non-partitioned table t2 and insert data into it.
  200. now create a table t1 partitioned on intended column(say country).
  201. load data in t1 from t2 as below:
  202.  
  203. hive> INSERT INTO TABLE t2 PARTITION(country) SELECT * from T1;
  204. make sure that partitioned column is always the last one in non partitioned table(as we are having country column in t2)
  205.  
  206.  
  207.  
  208. You can perform dynamic partition on hive external table and managed table If you want to use Dynamic partition in hive then mode is in nonstrict mode Here is hive dynamic partition properties you should allow
  209.  
  210. SET hive.exec.dynamic.partition = true;
  211.  
  212. SET hive.exec.dynamic.partition.mode = nonstrict;
  213.  
  214.  
  215.  
  216. -----------------------------Bucketing---------------------------------------------------
  217.  
  218.  
  219. Buckets in hive is used in segregating of hive table-data into multiple files or directories. it is used for efficient querying.
  220.  
  221. The data i.e. present in that partitions can be divided further into Buckets
  222. The division is performed based on Hash of particular columns that we selected in the table.
  223. Buckets use some form of Hashing algorithm at back end to read each record and place it into buckets
  224. In Hive, we have to enable buckets by using the set.hive.enforce.bucketing=true;
  225.  
  226. CREATE TABLE IF NOT EXISTS student_bucketed (id int, name String,marks Float,city String)
  227. CLUSTERED BY (city) INTO 3 BUCKETS;
  228.  
  229.  
  230. INSERT OVERWRITE TABLE student_bucketed
  231. SELECT id,name,mark,city from students4;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement