一 数据库与表

数据库基本操作命令

1 选择数据库命令

  • Mysql:
  1. 登录方式:
  2. #直接本地登录 root:123456
  3. #mysql -u root -p
  4. #远程登录 192.168.1.178 chu888chu888:skybar
  5. #mysql -h 192.168.1.178 -u chu888chu888 -p
  6. mysql> show databases;
  7. +--------------------+
  8. | Database |
  9. +--------------------+
  10. | information_schema |
  11. | Northwind |
  12. | Pubs |
  13. | ReportServer |
  14. | hive |
  15. | hive_hadoop |
  16. | hivetestdb |
  17. | mysql |
  18. | performance_schema |
  19. +--------------------+
  20. 9 rows in set (0.10 sec)
  21. mysql> use Pubs;
  22. Reading table information for completion of table and column names
  23. You can turn off this feature to get a quicker startup with -A
  24. Database changed
  25. mysql> show tables;
  26. +----------------+
  27. | Tables_in_Pubs |
  28. +----------------+
  29. | authors |
  30. | discounts |
  31. | employee |
  32. | jobs |
  33. | pub_info |
  34. | publishers |
  35. | roysched |
  36. | sales |
  37. | stores |
  38. | titleauthor |
  39. | titles |
  40. +----------------+
  41. 11 rows in set (0.00 sec)
  42. mysql> show columns from jobs;
  43. +----------+---------------------+------+-----+-----------------------------------------+----------------+
  44. | Field | Type | Null | Key | Default | Extra |
  45. +----------+---------------------+------+-----+-----------------------------------------+----------------+
  46. | job_id | smallint(6) | NO | PRI | NULL | auto_increment |
  47. | job_desc | varchar(50) | NO | | New Position - title not formalized yet | |
  48. | min_lvl | tinyint(3) unsigned | NO | | NULL | |
  49. | max_lvl | tinyint(3) unsigned | NO | | NULL | |
  50. +----------+---------------------+------+-----+-----------------------------------------+----------------+
  51. 4 rows in set (0.00 sec)
  52. mysql> show status;
  53. +------------------------------------------+-------------+
  54. | Variable_name | Value |
  55. +------------------------------------------+-------------+
  56. | Aborted_clients | 0 |
  57. | Aborted_connects | 0 |
  58. | Binlog_cache_disk_use | 0 |
  59. | Binlog_cache_use | 0 |
  60. | Binlog_stmt_cache_disk_use | 0 |
  61. | Binlog_stmt_cache_use | 0 |
  62. ----------------------------------------------------------
  • Oracle:
    等待补充

  • DB2:
    等待补充

  • Inceptor:

  1. [root@dhc-1 ~]# beeline -u jdbc:hive2://192.168.1.70:10000/
  2. scan complete in 2ms
  3. Connecting to jdbc:hive2://192.168.1.70:10000/
  4. 2016-03-22 08:33:48,094 INFO jdbc.Utils: Supplied authorities: 192.168.1.70:10000
  5. 2016-03-22 08:33:48,094 INFO jdbc.Utils: Resolved authority: 192.168.1.70:10000
  6. Connected to: Apache Hive (version 0.12.0-transwarp-tdh40)
  7. Driver: Hive JDBC (version 0.12.0-transwarp-tdh40)
  8. Transaction isolation: TRANSACTION_REPEATABLE_READ
  9. Beeline version 0.12.0-transwarp-tdh40 by Apache Hive
  10. 0: jdbc:hive2://192.168.1.70:10000/> show databases;
  11. +----------------+
  12. | database_name |
  13. +----------------+
  14. | default |
  15. +----------------+
  16. 1 row selected (2.282 seconds)
  17. 0: jdbc:hive2://192.168.1.70:10000/>
  18. 1 row selected (2.282 seconds)
  19. 0: jdbc:hive2://192.168.1.70:10000/> use default;
  20. No rows affected (0.068 seconds)
  21. 0: jdbc:hive2://192.168.1.70:10000/> show tables;
  22. +-----------+
  23. | tab_name |
  24. +-----------+
  25. +-----------+
  26. No rows selected (0.08 seconds)
  27. 0: jdbc:hive2://192.168.1.70:10000/>

实验准备数据一 Pubs数据库

为了能在Inceptor中实现兼容性测试,我们必须去移植一下我们样例数据库中的数据(来之微软的Pubs数据库)

1 载入Inceptor

  1. --登录Inceptor server节点
  2. beeline -u jdbc:hive2://192.168.1.70:10000/

2 使用Sqoop将MYSQL数据库导入HDFS

  1. 在Inceptor metastore节点服务器上安装sqoop服务
    1. yum install sqoop
  2. 由于Inceptor-SQL中metastore中已经安装了mysql,就不需要安装mysql了

  3. 将mysql-connector-java-5.1.38tar.gz驱动包先解压

    1. tar -zxvf mysql-connector-java-5.1.38tar.gz
  4. cd进刚刚解压后的目录,将里面的mysql-connector-java-5.1.38-bin.jar包copy到/usr/lib/sqoop/lib本地目录下

  5. 从mysql————>HDFS上(import,将mysql中的db1数据库里面的表导入到/user/datadir,这里的datadir目录一定不要事先创建,不然会报错,语句执行的时候会自动创建目录的!最后一行的-m表示map成4个文件)

  1. sqoop import \
  2. --username chu888chu888 \
  3. --password skybar \
  4. --connect jdbc:mysql://192.168.1.178:3306/Pubs \
  5. --table titleauthor \
  6. --target-dir /user/chu888chu888/data/titleauthor -m 4
  7. sqoop import \
  8. --username chu888chu888 \
  9. --password skybar \
  10. --connect jdbc:mysql://192.168.1.178:3306/Pubs \
  11. --table authors \
  12. --target-dir /user/chu888chu888/data/authors -m 4
  13. sqoop import \
  14. --username chu888chu888 \
  15. --password skybar \
  16. --connect jdbc:mysql://192.168.1.178:3306/Pubs \
  17. --table authors \
  18. --target-dir /user/chu888chu888/data/employee -m 4
  19. sqoop import \
  20. --username chu888chu888 \
  21. --password skybar \
  22. --connect jdbc:mysql://192.168.1.178:3306/Pubs \
  23. --table discounts \
  24. --target-dir /user/chu888chu888/data/discounts -m 4
  25. sqoop import \
  26. --username chu888chu888 \
  27. --password skybar \
  28. --connect jdbc:mysql://192.168.1.178:3306/Pubs \
  29. --table jobs \
  30. --target-dir /user/chu888chu888/data/jobs -m 4
  31. sqoop import \
  32. --username chu888chu888 \
  33. --password skybar \
  34. --connect jdbc:mysql://192.168.1.178:3306/Pubs \
  35. --table pub_info \
  36. --target-dir /user/chu888chu888/data/pub_info -m 4
  37. sqoop import \
  38. --username chu888chu888 \
  39. --password skybar \
  40. --connect jdbc:mysql://192.168.1.178:3306/Pubs \
  41. --table publishers \
  42. --target-dir /user/chu888chu888/data/publishers -m 4
  43. 有一个问题如果表没有主键的话,就会导入不了.
  44. alter table roysched add roysched_id int unsigned not Null auto_increment primary key;
  45. sqoop import \
  46. --username chu888chu888 \
  47. --password skybar \
  48. --connect jdbc:mysql://192.168.1.178:3306/Pubs \
  49. --table roysched \
  50. --target-dir /user/chu888chu888/data/roysched -m 4
  51. sqoop import \
  52. --username chu888chu888 \
  53. --password skybar \
  54. --connect jdbc:mysql://192.168.1.178:3306/Pubs \
  55. --table sales \
  56. --target-dir /user/chu888chu888/data/sales -m 4
  57. sqoop import \
  58. --username chu888chu888 \
  59. --password skybar \
  60. --connect jdbc:mysql://192.168.1.178:3306/Pubs \
  61. --table stores \
  62. --target-dir /user/chu888chu888/data/stores -m 4
  63. sqoop import \
  64. --username chu888chu888 \
  65. --password skybar \
  66. --connect jdbc:mysql://192.168.1.178:3306/Pubs \
  67. --table titles \
  68. --target-dir /user/chu888chu888/data/titles -m 4

6 SQL SERVER导入的问题

  1. sqoop import \
  2. --table address \
  3. --connect "jdbc:sqlserver://192.168.1.139:1433;database=AdventureWorks" \
  4. --username=sa \
  5. --password=123456 \
  6. --hive-drop-import-delims \
  7. --null-string '\\N' \
  8. --null-non-string '\\N' \
  9. --fields-terminated-by '\001' \
  10. --target-dir /user/test/address1 -m 1

3 在Inceptor中建立外表结构

  1. mysql> desc authors;
  2. +----------+-------------+------+-----+---------+-------+
  3. | Field | Type | Null | Key | Default | Extra |
  4. +----------+-------------+------+-----+---------+-------+
  5. | au_id | varchar(11) | NO | PRI | NULL | |
  6. | au_lname | varchar(40) | NO | MUL | NULL | |
  7. | au_fname | varchar(20) | NO | | NULL | |
  8. | phone | varchar(12) | NO | | UNKNOWN | |
  9. | address | varchar(40) | YES | | NULL | |
  10. | city | varchar(20) | YES | | NULL | |
  11. | state | varchar(2) | YES | | NULL | |
  12. | zip | varchar(5) | YES | | NULL | |
  13. | contract | bit(1) | NO | | NULL | |
  14. +----------+-------------+------+-----+---------+-------+
  15. 9 rows in set (0.00 sec)
  16. create external table authors
  17. (
  18. au_id STRING,
  19. au_lname STRING,
  20. au_fname STRING,
  21. phone STRING,
  22. address STRING,
  23. city STRING,
  24. state STRING,
  25. zip STRING,
  26. contract STRING
  27. )row format delimited fields terminated by ',' location '/user/chu888chu888/data/authors';
  28. mysql> desc discounts;
  29. +--------------+--------------+------+-----+---------+-------+
  30. | Field | Type | Null | Key | Default | Extra |
  31. +--------------+--------------+------+-----+---------+-------+
  32. | discounttype | varchar(40) | NO | | NULL | |
  33. | stor_id | varchar(4) | YES | | NULL | |
  34. | lowqty | smallint(6) | YES | | NULL | |
  35. | highqty | smallint(6) | YES | | NULL | |
  36. | discount | decimal(6,2) | NO | | NULL | |
  37. +--------------+--------------+------+-----+---------+-------+
  38. 5 rows in set (0.00 sec)
  39. create external table discounts
  40. (
  41. discounttype STRING,
  42. stor_id STRING,
  43. lowqty STRING,
  44. highqty STRING,
  45. discount STRING,
  46. discount_id STRING
  47. )row format delimited fields terminated by ',' location '/user/chu888chu888/data/discounts';
  48. mysql> desc employee;
  49. +-----------+---------------------+------+-----+---------+-------+
  50. | Field | Type | Null | Key | Default | Extra |
  51. +-----------+---------------------+------+-----+---------+-------+
  52. | emp_id | varchar(9) | NO | PRI | NULL | |
  53. | fname | varchar(20) | NO | | NULL | |
  54. | minit | varchar(1) | YES | | NULL | |
  55. | lname | varchar(30) | NO | MUL | NULL | |
  56. | job_id | smallint(6) | NO | | 1 | |
  57. | job_lvl | tinyint(3) unsigned | YES | | 10 | |
  58. | pub_id | varchar(4) | NO | | 9952 | |
  59. | hire_date | date | YES | | NULL | |
  60. +-----------+---------------------+------+-----+---------+-------+
  61. 8 rows in set (0.00 sec)
  62. create external table employee
  63. (
  64. emp_id STRING,
  65. fname STRING,
  66. minit STRING,
  67. lname STRING,
  68. job_id STRING,
  69. job_lvl STRING,
  70. pub_id STRING,
  71. hire_date STRING
  72. )row format delimited fields terminated by ',' location '/user/chu888chu888/data/employee';
  73. mysql> desc jobs;
  74. +----------+---------------------+------+-----+-----------------------------------------+----------------+
  75. | Field | Type | Null | Key | Default | Extra |
  76. +----------+---------------------+------+-----+-----------------------------------------+----------------+
  77. | job_id | smallint(6) | NO | PRI | NULL | auto_increment |
  78. | job_desc | varchar(50) | NO | | New Position - title not formalized yet | |
  79. | min_lvl | tinyint(3) unsigned | NO | | NULL | |
  80. | max_lvl | tinyint(3) unsigned | NO | | NULL | |
  81. +----------+---------------------+------+-----+-----------------------------------------+----------------+
  82. 4 rows in set (0.00 sec)
  83. create external table jobs
  84. (
  85. job_id STRING,
  86. job_desc STRING,
  87. min_lvl STRING,
  88. max_lvl STRING
  89. )row format delimited fields terminated by ',' location '/user/chu888chu888/data/jobs';
  90. mysql> desc pub_info;
  91. +---------+------------+------+-----+---------+-------+
  92. | Field | Type | Null | Key | Default | Extra |
  93. +---------+------------+------+-----+---------+-------+
  94. | pub_id | varchar(4) | NO | PRI | NULL | |
  95. | logo | longblob | YES | | NULL | |
  96. | pr_info | longtext | YES | | NULL | |
  97. +---------+------------+------+-----+---------+-------+
  98. 3 rows in set (0.00 sec)
  99. create external table pub_info
  100. (
  101. pub_id STRING,
  102. logo STRING,
  103. pr_info STRING
  104. )row format delimited fields terminated by ',' location '/user/chu888chu888/data/pub_info';
  105. mysql> desc publishers;
  106. +----------+-------------+------+-----+---------+-------+
  107. | Field | Type | Null | Key | Default | Extra |
  108. +----------+-------------+------+-----+---------+-------+
  109. | pub_id | varchar(4) | NO | PRI | NULL | |
  110. | pub_name | varchar(40) | YES | | NULL | |
  111. | city | varchar(20) | YES | | NULL | |
  112. | state | varchar(2) | YES | | NULL | |
  113. | country | varchar(30) | YES | | USA | |
  114. +----------+-------------+------+-----+---------+-------+
  115. 5 rows in set (0.00 sec)
  116. create external table publishers
  117. (
  118. pub_id STRING,
  119. pub_name STRING,
  120. city STRING,
  121. state STRING,
  122. country STRING
  123. )row format delimited fields terminated by ',' location '/user/chu888chu888/data/publishers';
  124. mysql> desc roysched;
  125. +----------+------------+------+-----+---------+-------+
  126. | Field | Type | Null | Key | Default | Extra |
  127. +----------+------------+------+-----+---------+-------+
  128. | title_id | varchar(6) | NO | MUL | NULL | |
  129. | lorange | int(11) | YES | | NULL | |
  130. | hirange | int(11) | YES | | NULL | |
  131. | royalty | int(11) | YES | | NULL | |
  132. +----------+------------+------+-----+---------+-------+
  133. 4 rows in set (0.00 sec)
  134. create external table roysched
  135. (
  136. title_id STRING,
  137. lorange STRING,
  138. hirange STRING,
  139. royalty STRING
  140. )row format delimited fields terminated by ',' location '/user/chu888chu888/data/roysched';
  141. mysql> desc sales;
  142. +----------+-------------+------+-----+---------+-------+
  143. | Field | Type | Null | Key | Default | Extra |
  144. +----------+-------------+------+-----+---------+-------+
  145. | stor_id | varchar(4) | NO | PRI | NULL | |
  146. | ord_num | varchar(20) | NO | PRI | NULL | |
  147. | ord_date | date | YES | | NULL | |
  148. | qty | smallint(6) | NO | | NULL | |
  149. | payterms | varchar(12) | NO | | NULL | |
  150. | title_id | varchar(6) | NO | PRI | NULL | |
  151. +----------+-------------+------+-----+---------+-------+
  152. 6 rows in set (0.00 sec)
  153. create external table sales
  154. (
  155. stor_id STRING,
  156. ord_num STRING,
  157. ord_date STRING,
  158. qty STRING,
  159. title_id STRING
  160. )row format delimited fields terminated by ',' location '/user/chu888chu888/data/sales';
  161. mysql> desc stores;
  162. +--------------+-------------+------+-----+---------+-------+
  163. | Field | Type | Null | Key | Default | Extra |
  164. +--------------+-------------+------+-----+---------+-------+
  165. | stor_id | varchar(4) | NO | PRI | NULL | |
  166. | stor_name | varchar(40) | YES | | NULL | |
  167. | stor_address | varchar(40) | YES | | NULL | |
  168. | city | varchar(20) | YES | | NULL | |
  169. | state | varchar(2) | YES | | NULL | |
  170. | zip | varchar(5) | YES | | NULL | |
  171. +--------------+-------------+------+-----+---------+-------+
  172. 6 rows in set (0.01 sec)
  173. create external table stores
  174. (
  175. stor_id STRING,
  176. stor_name STRING,
  177. stor_address STRING,
  178. city STRING,
  179. zip STRING
  180. )row format delimited fields terminated by ',' location '/user/chu888chu888/data/stores';
  181. mysql> desc titleauthor;
  182. +------------+---------------------+------+-----+---------+-------+
  183. | Field | Type | Null | Key | Default | Extra |
  184. +------------+---------------------+------+-----+---------+-------+
  185. | au_id | varchar(11) | NO | PRI | NULL | |
  186. | title_id | varchar(6) | NO | PRI | NULL | |
  187. | au_ord | tinyint(3) unsigned | YES | | NULL | |
  188. | royaltyper | int(11) | YES | | NULL | |
  189. +------------+---------------------+------+-----+---------+-------+
  190. 4 rows in set (0.00 sec)
  191. create external table titleauthor
  192. (
  193. au_id STRING,
  194. title_id STRING,
  195. au_ord TinyInt,
  196. royaltyper INT
  197. )row format delimited fields terminated by ',' location '/user/chu888chu888/data/titleauthor';
  198. mysql> desc titles;
  199. +-----------+---------------+------+-----+-----------+-------+
  200. | Field | Type | Null | Key | Default | Extra |
  201. +-----------+---------------+------+-----+-----------+-------+
  202. | title_id | varchar(6) | NO | PRI | NULL | |
  203. | title | varchar(80) | NO | MUL | NULL | |
  204. | type | varchar(12) | NO | | UNDECIDED | |
  205. | pub_id | varchar(4) | YES | | NULL | |
  206. | price | decimal(19,4) | YES | | NULL | |
  207. | advance | decimal(19,4) | YES | | NULL | |
  208. | royalty | int(11) | YES | | NULL | |
  209. | ytd_sales | int(11) | YES | | NULL | |
  210. | notes | varchar(200) | YES | | NULL | |
  211. | pubdate | datetime | NO | | NULL | |
  212. +-----------+---------------+------+-----+-----------+-------+
  213. 10 rows in set (0.00 sec)
  214. create external table titles
  215. (
  216. title_id STRING,
  217. title STRING,
  218. type STRING,
  219. pub_id STRING,
  220. price STRING,
  221. advance STRING,
  222. royalty STRING,
  223. ytd_sales STRING,
  224. notes STRING,
  225. pubdate STRING
  226. )row format delimited fields terminated by ',' location '/user/chu888chu888/data/titles';