(1)创建表: create table stu(id int,name string); insert into stu values (1,“zhangsan”); (2)创建表并指定字段之间的分隔符,以textfile形式存储: create table if not exists stu2(id int ,name string) row format delimited fields terminated by ‘\t’ stored as textfile location ‘/user/stu2’; (3)根据查询结果创建表(同时创建数据): create table stu3 as select * from stu2; (4)根据已经存在的表结构创建表: create table stu4 like stu2; (5)根据查询表的类型: desc formatted stu2;
(1)创建外部表: create external table techer (t_id string,t_name string) row format delimited fields terminated by ‘\t’; (2)从本地文件系统向表中加载数据 load data local inpath ‘/export/servers/hivedatas/student.csv’ into table student; (3)加载数据并覆盖已有数据 load data local inpath ‘/export/servers/hivedatas/student.csv’ overwrite into table student; (4)从hdfs文件系统向表中加载数据 load data inpath ‘/hivedatas/techer.csv’ into table techer;
3 分区表:
(1)创建单分区的分区表: create table score(s_id string,c_id string ,s_score int) partitioned by (month string) row format delimited fields terminated by ‘\t’; (2)创建多分区的分区表: create table score2 (s_id string,c_id string, s_score int) partitioned by (year string,month string,day string) row format delimited fields terminated by ‘\t’; (3)加载数据到分区表中 load data local inpath ‘/export/servers/hivedatas/score.csv’ into table score partition (month=‘201806’); (4)加载数据到一个多分区的表中去 load data local inpath ‘/export/servers/hivedatas/score.csv’ into table score2 partition(year=‘2018’,month=‘06’,day=‘01’); (5)多分区联合查询使用union all来实现 select * from score where month = ‘201806’ union all select * from score where month = ‘201806’; (6)查看分区 show partitions score; (7)添加一个分区 alter table score add partition(month=‘201805’); (8)同时添加多个分区 alter table score add partition(month=‘201804’) partition(month = ‘201803’); (9)删除分区 alter table score drop partition(month = ‘201806’
(1)开启hive的桶表功能 set hive.enforce.bucketing=true; (2)设置reduce的个数 set mapreduce.job.reduces=3; (3)创建桶表 create table course (c_id string,c_name string,t_id string) clustered by(c_id) into 3 buckets row format delimited fields terminated by ‘\t’;
(4)桶表的数据加载,由于桶表的数据加载通过hdfs dfs -put文件或者通过load data均不好使,只能创建普通表,并通过insert overwrite的方式将普通表的数据通过查询的方式加载到桶表当中去 a.创建普通表: create table course_common (c_id string,c_name string,t_id string) row format delimited fields terminated by ‘\t’; b.普通表中加载数据 load data local inpath ‘/export/servers/hivedatas/course.csv’ into table course_common; c.通过insert overwrite给桶表中加载数据 insert overwrite table course select * from course_common cluster by(c_id);
4.2.4修改表:
(1)表的重命名: alter table 旧表名 rename to 新表名; (2)添加列: alter table 表名 add columns (列名 类型,列名 类型); (3)修改列 : alter table 表名 change column 旧列名 新列名 类型;
4.2.5删除表: drop table 表名 ;
4.2.6表中加载数据
向分区表中添加数据
a.直接插入:insert into table 表名 partition( ) values();
b.通过load方式加载数据: load data local inpath ‘数据所在的路径’ overwrite into table 表名 partition( );
c.通过查询方式加载数据:insert overwrite table 表名 partition( ) select 字段名,字段名,字段名 from 表名;
d.多插入模式:常用于实际生产环境当中,将一张表拆开成两部分或者多部分 给score表加载数据 load data local inpath ‘/export/servers/hivedatas/score.csv’ overwrite into table score partition(month=‘201806’); 创建第一部分表: create table score_first( s_id string,c_id string) partitioned by (month string) row format delimited fields terminated by ‘\t’ ; 创建第二部分表: create table score_second(c_id string,s_score int) partitioned by (month string) row format delimited fields terminated by ‘\t’; 分别给第一部分与第二部分表加载数据 from score insert overwrite table score_first partition(month=‘201806’) select s_id,c_id insert overwrite table score_second partition(month = ‘201806’) select c_id,s_score;
e.查询语句中创建表并加载数据(as select) create table score5 as select * from score;
4.2.7表中数据导出
1. insert导出
1) 将查询的结果导出到本地 insert overwrite local directory ‘/export/servers/exporthive’ select * from score;
2) 将查询的结果格式化导出到本地 insert overwrite local directory ‘/export/servers/exporthive’ row format delimited fields terminated by ‘\t’ collection items terminated by ‘#’ select * from student;
3) 将查询的结果导出到HDFS上(没有local) insert overwrite directory ‘/export/servers/exporthive’ row format delimited fields terminated by ‘\t’ collection items terminated by ‘#’ select * from score;
4.3.4 列别名:重命名一个列。紧跟列名,也可以在列名和别名之间加入关键字‘AS’ select s_id as myid ,c_id from score;
4.3.5、常用函数
1)求总行数(count): select count(1) from score; 2)求分数的最大值(max): select max(s_score) from score; 3)求分数的最小值(min): select min(s_score) from score; 4)求分数的总和(sum): select sum(s_score) from score; 5)求分数的平均值(avg): select avg(s_score) from score;
4.3.6 LIMIT语句:典型的查询会返回多行数据。LIMIT子句用于限制返回的行数。 select * from score limit 3;
4.3.7 WHERE语句 :使用WHERE 子句,将不满足条件的行过滤掉。
查询出分数大于60的数据 select * from score where s_score > 60;
(1)GROUP BY语句 GROUP BY语句通常会和聚合函数一起使用,按照一个或者多个列队结果进行分组,然后对每个组执行聚合操作。 计算每个学生的平均分数 select s_id ,avg(s_score) from score group by s_id;
(2)HAVING语句 having与where不同点 (1)where针对表中的列发挥作用,查询数据;having针对查询结果中的列发挥作用,筛选数据。 (2)where后面不能写分组函数,而having后面可以使用分组函数。 (3)having只用于group by分组统计语句。 求每个学生平均分数大于85的人 select s_id ,avg(s_score) avgscore from score group by s_id having avgscore > 85;
4.3.11 内连接(INNER JOIN) 内连接:只有进行连接的两个表中都存在与连接条件相匹配的数据才会被保留下来。 select * from techer t inner join course c on t.t_id = c.t_id;
4.3.12、左外连接(LEFT OUTER JOIN) 左外连接:JOIN操作符左边表中符合WHERE子句的所有记录将会被返回。 查询老师对应的课程 select * from techer t left join course c on t.t_id = c.t_id;
4.3.13、右外连接(RIGHT OUTER JOIN) 右外连接:JOIN操作符右边表中符合WHERE子句的所有记录将会被返回。 select * from techer t right join course c on t.t_id = c.t_id;
4.3.14、满外连接(FULL OUTER JOIN) 满外连接:将会返回所有表中符合WHERE语句条件的所有记录。如果任一表的指定字段没有符合条件的值的话,那么就使用NULL值替代。 SELECT * FROM techer t FULL JOIN course c ON t.t_id = c.t_id ;
4.3.15、多表连接
注意:连接 n个表,至少需要n-1个连接条件。例如:连接三个表,至少需要两个连接条件。 多表连接查询,查询老师对应的课程,以及对应的分数,对应的学生 select * from techer t left join course c on t.t_id = c.t_id left join score s on s.c_id = c.c_id left join student stu on s.s_id = stu.s_id; 大多数情况下,Hive会对每对JOIN连接对象启动一个MapReduce任务。本例中会首先启动一个MapReduce job对表techer和表course进行连接操作,然后会再启动一个MapReduce job将第一个MapReduce job的输出和表score;进行连接操作。
设置reduce的个数,将我们对应的s_id划分到对应的reduce当中去 set mapreduce.job.reduces=7; 通过distribute by 进行数据的分区 insert overwrite local directory ‘/export/servers/hivedatas/sort’ select * from score distribute by s_id sort by s_score;
4.3.17 CLUSTER BY 当distribute by和sort by字段相同时,可以使用cluster by方式。 cluster by除了具有distribute by的功能外还兼具sort by的功能。但是排序只能是倒序排序,不能指定排序规则为ASC或者DESC。 1) 以下两种写法等价 select * from score cluster by s_id; select * from score distribute by s_id sort by s_id;
4.3 Hive函数
4.3.1 内置函数
1)查看系统自带的函数 hive> show functions; 2)显示自带的函数的用法 hive> desc function upper; 3)详细显示自带的函数的用法 hive> desc function extended upper;
public class ItcastUDF extends UDF {
public Text evaluate(final Text s) {
if (null == s) {
return null;
}
//返回大写字母
return new Text(s.toString().toUpperCase());
}
}
第三步:将我们的项目打包,并上传到hive的lib目录下
第四步:添加我们的jar包,重命名我们的jar包名称 cd /export/servers/hive-1.1.0-cdh5.14.0/lib mv original-day_06_hive_udf-1.0-SNAPSHOT.jar udf.jar
hive的客户端添加我们的jar包 add jar /export/servers/hive-1.1.0-cdh5.14.0/lib/udf.jar;
第五步:设置函数与我们的自定义函数关联 create temporary function tolowercase as ‘自定义函数全名称’;
本文首先介绍下MongoDB的基本的增删改查操作,然后,详细介绍MongoDB提供的修改器,以完成各种各样的文档更新操作 MongoDB的主要操作
show dbs 显示当前用户能看到哪些数据库
use foobar 将数据库切换到foobar
show collections 显示当前数据库有哪些集合
db.people.update,update不带参数,可
The alert log is a chronological log of messages and errors, and includes the following items:
All internal errors (ORA-00600), block corruption errors (ORA-01578), and deadlock errors (ORA-00060)
由于几年前写了几篇 CAS 系列的文章,之后陆续有人参照文章去实现,可都遇到了各种问题,同时经常或多或少的收到不少人的求助。现在这时特此说明几点:
1. 那些文章发表于好几年前了,CAS 已经更新几个很多版本了,由于近年已经没有做该领域方面的事情,所有文章也没有持续更新。
2. 文章只是提供思路,尽管 CAS 版本已经发生变化,但原理和流程仍然一致。最重要的是明白原理,然后
lesson 课
traffic 交通
matter 要紧;事物
happy 快乐的,幸福的
second 第二的
idea 主意;想法;意见
mean 意味着
important 重要的,重大的
never 从来,决不
afraid 害怕 的
fifth 第五的
hometown 故乡,家乡
discuss 讨论;议论
east 东方的
agree 同意;赞成
bo
这次看下spring中少见的注解@primary注解,例子
@Component
public class MetalSinger implements Singer{
@Override
public String sing(String lyrics) {
return "I am singing with DIO voice