# bigdata-practice **Repository Path**: an-jun/bigdata-practice ## Basic Information - **Project Name**: bigdata-practice - **Description**: 大数据开发课程 笔记 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2022-11-28 - **Last Updated**: 2022-12-08 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 大数据开发-第1课 网络配置 https://www.toutiao.com/article/7171060477597286952/ 1. 命令行运行 ipconfig 找到 类似 '无线局域网适配器 WLAN' 的文字 记下ip 比如 我的 192.168.2.184 2. 配置centos 静态ip 3. 配置虚拟机网络类型为 nat,子网为 192.168.2.0 ,route # 大数据开发-第2课环境准备 https://www.toutiao.com/article/7168021780177715715/ # 大数据开发-第3课 hive 环境的搭建 https://www.toutiao.com/article/7169582474337534503/ # 大数据开发第4课 hive 初始基本操作 https://www.toutiao.com/article/7170179324556198435/ https://www.toutiao.com/article/7169915496362934791/ ### 1.介绍 | | Hive | RDBMS | | ------------ | ------------------- | ---------------------- | | 查询语言 | HQL | SQL | | 数据存储 | HDFS | raw device or local FS | | 执行 | mapReduce,tez,spark | excutor | | 延时 | 高 | 低 | | 处理数据规模 | 大 | 小 | | 索引 | 0.8版后加入位图索引 | 有复杂索引 | #### 1.1 hive DDL:数据定义言 hive语句注意事项 * Sql语言大小写不敏感 * sql可以写一行或多行 * 关键字不能缩写,不能分行 * 各子句一般要分行 * -- 为注释 ##### 1.1.1 创建删除数据库 * 创建数据库 ``` create database myhive; ``` * 查看数据库 ``` show databases ; ``` * 删除数据库 ``` drop database if exists myhive; drop database myhive cascade; ``` * 进入数据 ``` use myhive; ``` ##### 1.1.2创建表--类型https://cwiki.apache.org/confluence/display/Hive/LanguageManual+Types * 数字类型 * tinyint * smarllint * int * bigint * float * double * decimal 长度不定 * 时间时间 * TIMESTAMP * DATE * INTERVAL * 字符串 * string * varchar 长度不定 * char * misc * boolean * binary * 复合类型 * array * map * struct * uniontype ### 1.2 hive建表https://cwiki.apache.org/confluence/display/Hive/LanguageManual+DDL#LanguageManualDDL-CreateTable * like 充许复制现有表结构,不复制数据 | | 内部表 | 外部表 | | -------- | ------------------------ |------------------------------------| | 数据管理 | hive管理 | HDFS管理 | | 存储位置 | /usr/hive/warehouse | LOCATION指定,默认位置/usr/hive/warehouse | | 删除表 | 直接删除元数据及存储数据 | 只删除元数据 | | 修改表 | 修改直接同步给元数据 | 需要修复(msck repair table table_name) | ``` create database myhive; create table test_create_table(id int,name string); desc test_create_table; show create table test_create_table; -- open http://192.168.2.10:50070/ insert into test_create_table values (1,'test'); select * from test_create_table; create external table test_extern(id int ,name string); create external table test_extern_1(id int ,name string) location '/datas/test_extern_1'; insert into test_extern_1 values (1,'test'); select * from test_extern_1; -- like 建表 create table test_like like test_create_table; desc test_like; select * from test_like; -- comment create table test_com(id int comment '学生ID',name string comment '学生姓名') comment '学生表'; show create table test_com; -- as 查询建表 create table bySelect as select * from test_create_table; desc bySelect; select * from bySelect; ``` big data tool 连接hdfs问题 解决办法 https://cwiki.apache.org/confluence/display/HADOOP2/WindowsProblems github仓库 https://github.com/cdarlint/winutils/tree/master/ # 大数据开发第5课 hive分隔符 ### 分隔符类型 * 字段分隔符 * array 类型成员分隔符 * 行分隔符 放最后 只支持 回车换换 # 大数据开发第6课 分区表创建 https://www.toutiao.com/article/7170248496510468643/ 1) 分区表技术与意义 * 避免hive全表扫描,提升查询效率 * 减少数据冗余进而提高特定(分区)查询分析的效率 * 在逻辑上分区表与未分区表没有区别,在物理上分区表将数据按分区键分区的列值存储在表目录的子目录中,目录名为"分区键=键值" * 查询时尽量利用分区字段,如果不用分区字段,会全部扫描 2) 分区表类型 * 静态分区表 * 动态分区表 ```aidl use myhive1; -- 创建分区表 create table test_partition1( sku_id string comment '商品id', sku_name string comment '商品名称' ) partitioned by (sku_class string); desc test_partition1; -- 建分区表后,些时没数据,也没分区,需要建分区 alter table test_partition1 add partition (sku_class='xiaomi'); -- 查看表现有分区 show partitions test_partition1; --静态分区添加数据 insert into table test_partition1 partition (sku_class='xiaomi') values ('001','xiaomi'); insert into table test_partition1 partition (sku_class='xiaomi') select sku_id,sku_name from sales_info; select * from test_partition1; --本地文件加载 --load data local inpath '/xxx' into table test_partition1 partition(sku_class='xiaomi'); --严格模式下,对分区表,要加where过滤分区字段 set hive.mapred.mode=strict; select * from test_partition1 where sku_class='xiaomi'; set hive.mapred.mode=nonstrict; select * from test_partition1; --动态分区添加数据 set hive.exec.dynamic.partition=true; set hive.exec.dynamic.partition.mode=nonstrict; insert into table test_partition1 partition (sku_class)values ('001','xiaomi2','xiaomi'); insert into table test_partition1 partition (sku_class)values ('002','huawei2','huawei'); select * from test_partition1; --多字段分区 create table test_partition_mul( sku_id string comment '商品id', sku_name string comment '商品名称') partitioned by (sku_class string,sku_label string); drop table test_partition_mul; alter table test_partition_mul add partition (sku_class='xiaomi',sku_label='dianzi'); --查看现有分区 show partitions test_partition_mul; --静态分区添加数据 insert into table test_partition_mul partition (sku_class='xiaomi',sku_label='dianzi') values ('001','xiaomi1'); insert into table test_partition_mul partition (sku_class='xiaomi',sku_label='dianzi') select sku_id ,sku_name from sales_info; select * from test_partition_mul; -- 动态分区 添加数据 insert into table test_partition_mul partition (sku_class,sku_label) values ('001','xiaomi1','xiaomi1','dianzi1'); select * from test_partition_mul; -- 删除分区及数据 alter table test_partition1 drop partition (sku_class='xiaomi1'); alter table test_partition_mul drop partition (sku_class='xiaomi',sku_label='dianzi'); alter table test_partition_mul drop partition (sku_class='xiaomi1'); ``` # 大数据开发第7课 分桶表 https://www.toutiao.com/article/7170254832958538274/ ``` use class5; --创建分桶表 create table test_buckets( sku_id string comment '商品id', sku_name string comment '商品名称') clustered by (sku_id) into 3 buckets ; --设置自动分桶开关 set hive.enforce.bucketing=true; select * from sales_info; --为分桶表加数据 insert into test_buckets select sku_id,sku_name from sales_info; select * from test_buckets; ``` 电商用户下单业务 hive -f xxx.sql 加载数据不支持动态分区 # 大数据开发第8课 分布式大数据环境 https://www.toutiao.com/article/7169582405920244262/ ``` #上传文件 hdfs dfs -put '/datas/product_info.txt' '/datas' # 加载hdfs 数据到hive load data inpath '/datas/product_info.txt' into product_info; /etc/profile export HDFS_NAMENODE_USER=root export HDFS_DATANODE_USER=root export HDFS_SECONDARYNAMENODE_USER=root export YARN_RESOURCEMANAGER_USER=root export YARN_NODEMANAGER_USER=root - 启动 hadoop: hadoop.sh start - 启动 kafka: kafka.sh start - 启动 zookeeper: zk start - 启动 hbase: start-hbase.sh hbase shell create 'a' ,'info' list put 'a','1001','info:name','lisi' scan 'a' -- kafka.sh start mysql -uroot -paaaaaa # hive 1. nohup hive --service metastore 2>&1 & nohup hive --service hiveserver2 2>&1 & !connect jdbc:hive2://hadoop162:10000 root/aaaaaa # phoenix cd /opt/module/phoenix-5.0.0 bin/sqlline.py hadoop162:2181 !ta create table preson(id varchar primary key,name varchar); upsert into person values('a','zs'); #redis redis-server /etc/redis.conf redis-client ``` # 大数据开发第9课 hive DML https://www.toutiao.com/article/7171083238067618356/ ## 导出数据 --导出到本地 insert overwrite local directory '文件夹路径' row format delimited fields terminated by '字段分隔符' 查询语句 ``` --导出到hdfs insert overwrite directory '/datas/' select * from sales_info; --导出到 本地 insert overwrite local directory '/datas/sales_info' select * from sales_info; ``` ## 删除表所有数据 ``` -- 清空表内容,不记日志,无事务,快 truncat table byselect; -- shell命令删除外部表,假设外部表 目录 /datas/text_external hdfs dfs rm -rf /datas/text_external/* ``` ## 删除表部分数据 有partition表 ``` alter table table_name drop partition(partion_name='value') ``` 无partition表 insert overwrite table 表名 select * from 表名 where 条件; 删除整个表 drop table 表名 # 大数据开发第10课 hive DQL 数据查询语言 https://www.toutiao.com/article/7171817953145471503/ * 内置运算符&内置函数 * 关系运算符 * 等值比较: = 、== * 不等值比较: <> 、!= * 小于比较: < * 小于等于比较: <= * 大于比较: > * 大于等于比较: >= * 空值判断: IS NULL * 非空判断: IS NOT NULL * IKE比较: LIKE * AVA的LIKE操作: RLIKE * REGEXP操作: REGEXP * 算术运算符 * 加法操作: + * 减法操作: - * 乘法操作: * * 除法操作: / * 取整操作: div * 取余操作: % * 位与操作: & * 位或操作: | * 位异或操作: ^ * 位取反操作: ~ * 逻辑运算符 * 与操作: A AND B * 或操作: A OR B * 非操作: NOT A 、!A * 在:A IN (val1, val2, ...) * 不在:A NOT IN (val1, val2, ...) * 逻辑是否存在: [NOT] EXISTS (subquery) 4. SELECT [ALL | DISTINCT] select_expr, select_expr, ... FROM table_reference [WHERE where_condition] [GROUP BY col_list] [HAVING having_condition] [CLUSTER BY col_list | [DISTRIBUTE BY col_list] [SORT BY col_list]] [LIMIT number]; 5. hive语句执行顺序 ** from --> where --> select --> group by -->聚合函数-->having -->order by -->limit # 大数据开发第11课 hive DQL数据查询语言-数组查询 https://www.toutiao.com/article/7172545715292373511/ 1. hive 严格模式 ``` --设置非严格模式(默认) set hive.mapred.mode=nonstrict; --设置严格模式 set hive.mapred.mode=strict; ``` | 如果为strict 会对三种情况在compile环节作过滤 | |-----| | 1. 读取partitioned table ,但没指定partition | | 2. order by 排序,必须加limit语句 | | 3. 限制笛卡尔积的查询(表关联join不写关联条件) | hive3.0 在subquery和views中没有limit的order by 会被optimizer移除 -- 不移除,及不优化 set hive.remove.orderby.in.subquery=false # 大数据开发第12课 hive DQL数据查询语言-Map查询 https://www.toutiao.com/article/7172545715292373511/ # 大数据开发第13课-hive HQL where-group-by https://www.toutiao.com/article/7173256636222505487/ * where * group by | where | hiving | |---------|-------------------| | 作用在列 | 对查询结果作用 | | 不能写分组函数 | 写分组函数 | | 不受限制 | 只用group by 分组统计语句 | # 大数据开发第14课-hive HQL 排序 https://www.toutiao.com/article/7173264575209980451/ # 大数据开发第15课-hive 关联查询 https://www.toutiao.com/article/7173650113834041856/ # 大数据开发第16课 hive 视图及窗口函数 https://www.toutiao.com/article/7174008891259224576/ # 大数据开发第17课-hive子查询-抽样查询-优化