Embrace Technology


  • 首页

  • 关于

  • 标签

  • 分类

  • 归档

  • 搜索

mysql数据表设计

发表于 2018-10-30 | 分类于 mysql | | 阅读次数:

为什么字段尽可能用NOT NULL,而不是NULL

NULL为什么多人用?

1、NULL是创建数据表时默认的,初级或不知情的或怕麻烦的程序员不会注意这点。

2、很多人员都以为not null 需要更多空间,其实这不是重点。

3、重点是很多程序员觉得NULL在开发不用去判断插入数据,写sql语句的时候更方便快捷。

网上很多资料都有写:

Mysql官网文档:

“NULL columns require additional space in the rowto record whether their values are NULL. For MyISAM tables, each NULL columntakes one bit extra, rounded up to the nearest byte.”

-———————————————-

Mysql难以优化引用可空列查询,它会使索引、索引统计和值更加复杂。可空列需要更多的存储空间,还需要mysql内部进行特殊处理。可空列被索引后,每条记录都需要一个额外的字节,还能导致MYisam 中固定大小的索引变成可变大小的索引

——–这也是《高性能mysql第二版》介绍的

解读:

“可空列需要更多的存储空间”:需要一个额外字节作为判断是否为NULL的标志位

“需要mysql内部进行特殊处理”: 这是mysql索引统计,里面有介绍mysql怎么处理NULL。

注意:但把NULL列改为NOT NULL带来的性能提示很小,除非确定它带来了问题,否则不要把它当成优先的优化措施,最重要的是使用的列的类型的适当性

参考:http://blogread.cn/it/article/5967?f=hot3

spring boot 集成 swagger2

发表于 2018-10-10 | 分类于 spring boot | | 阅读次数:

环境说明

spring boot: 1.5.16.RELEASE

swagger2: 2.2.0

jdk:1.8 _152

spring boot 集成 swagger2

添加Swagger2依赖

在pom.xml中加入Swagger2的依赖

1
2
3
4
5
6
7
8
9
10
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.2.2</version>
</dependency>

创建Swagger2配置类

在Application.java同级创建Swagger2的配置类Swagger2。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package com.linezone.biwork;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class Swagger2 {

@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.linezone.biwork.web"))
.paths(PathSelectors.any())
.build();
}

private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Spring Boot中使用Swagger2构建RESTful APIs")
.description("Spring Boot Swagger2构建")
.termsOfServiceUrl("https://wylgeek.github.io/")
.contact("tomnic.wang")
.version("1.0")
.build();
}

}

添加文档内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import com.linezone.biwork.web.model.User;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping(value="/users") // 通过这里配置使下面的映射都在/users下
public class UserController {

// 创建线程安全的Map
static Map<Long, User> users = Collections.synchronizedMap(new HashMap<Long, User>());

@ApiOperation(value="获取用户列表", notes="")
@RequestMapping(value="/", method= RequestMethod.GET)
public List<User> getUserList() {
// 处理"/users/"的GET请求,用来获取用户列表
// 还可以通过@RequestParam从页面中传递参数来进行查询条件或者翻页信息的传递
List<User> r = new ArrayList<User>(users.values());
return r;
}

@ApiOperation(value="创建用户", notes="根据User对象创建用户")
@ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User")
@RequestMapping(value="/", method=RequestMethod.POST)
public String postUser(@ModelAttribute User user) {
// 处理"/users/"的POST请求,用来创建User
// 除了@ModelAttribute绑定参数之外,还可以通过@RequestParam从页面中传递参数
users.put(user.getId(), user);
return "success";
}

@ApiOperation(value="获取用户详细信息", notes="根据url的id来获取用户详细信息")
@ApiImplicitParam(name = "id", value = "用户ID", required = true, paramType="path",dataType = "Long")
@RequestMapping(value="/{id}", method=RequestMethod.GET)
public User getUser(@PathVariable Long id) {
// 处理"/users/{id}"的GET请求,用来获取url中id值的User信息
// url中的id可通过@PathVariable绑定到函数的参数中
return users.get(id);
}

@ApiOperation(value="更新用户详细信息", notes="根据url的id来指定更新对象,并根据传过来的user信息来更新用户详细信息")
@ApiImplicitParams({
@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long"),
@ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User")
})
@RequestMapping(value="/{id}", method=RequestMethod.PUT)
public String putUser(@PathVariable Long id, @ModelAttribute User user) {
// 处理"/users/{id}"的PUT请求,用来更新User信息
User u = users.get(id);
u.setName(user.getName());
u.setAge(user.getAge());
users.put(id, u);
return "success";
}

@ApiOperation(value="删除用户", notes="根据url的id来指定删除对象")
@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long")
@RequestMapping(value="/{id}", method=RequestMethod.DELETE)
public String deleteUser(@PathVariable Long id) {
// 处理"/users/{id}"的DELETE请求,用来删除User
users.remove(id);
return "success";
}

}

完成上述代码添加上,启动Spring Boot程序,访问:http://localhost:port/swagger-ui.html
。就能看到前文所展示的RESTful API的页面。我们可以再点开具体的API请求,以POST类型的/users请求为例,可找到上述代码中我们配置的Notes信息以及参数user的描述信息,如下图所示。

swagger2注解说明

介绍

swagger通过注解表明该接口会生成文档,包括接口名、请求方法、参数、返回信息的等等。

  • @Api()用于类;
    表示标识这个类是swagger的资源
  • @ApiOperation()用于方法;
    表示一个http请求的操作
  • @ApiParam()用于方法,参数,字段说明;
    表示对参数的添加元数据(说明或是否必填等)
  • @ApiModel()用于类
    表示对类进行说明,用于参数用实体类接收
  • @ApiModelProperty()用于方法,字段
    表示对model属性的说明或者数据操作更改
  • @ApiIgnore()用于类,方法,方法参数
    表示这个方法或者类被忽略
  • @ApiImplicitParam() 用于方法
    表示单独的请求参数
  • @ApiImplicitParams() 用于方法,包含多个 @ApiImplicitParam

详细介绍

@Api()
用于类;表示标识这个类是swagger的资源
tags–表示说明
value–也是说明,可以使用tags替代
但是tags如果有多个值,会生成多个list

@ApiOperation() 用于方法;表示一个http请求的操作
value用于方法描述
notes用于提示内容
tags可以重新分组(视情况而用)
@ApiParam() 用于方法,参数,字段说明;表示对参数的添加元数据(说明或是否必填等)
name–参数名
value–参数说明
required–是否必填

@ApiModel()用于类 ;表示对类进行说明,用于参数用实体类接收
value–表示对象名
description–描述
都可省略
@ApiModelProperty()用于方法,字段; 表示对model属性的说明或者数据操作更改
value–字段说明
name–重写属性名字
dataType–重写属性类型
required–是否必填
example–举例说明
hidden–隐藏

@ApiIgnore()用于类或者方法上,可以不被swagger显示在页面上
比较简单, 这里不做举例

@ApiImplicitParam() 用于方法
表示单独的请求参数
@ApiImplicitParams() 用于方法,包含多个 @ApiImplicitParam
name–参数ming
value–参数说明
dataType–数据类型
paramType–参数类型
example–举例说明

swagger2集成问题

问题描述:

1
2
3
4
5
6
7
8
@ApiOperation(value="获取用户详细信息", notes="根据url的id来获取用户详细信息")
@ApiImplicitParam(name = "id", value = "用户ID", required = true, paramType="path",dataType = "Long")
@RequestMapping(value="/{id}", method=RequestMethod.GET)
public User getUser(@PathVariable Long id) {
// 处理"/users/{id}"的GET请求,用来获取url中id值的User信息
// url中的id可通过@PathVariable绑定到函数的参数中
return users.get(id);
}

2018-10-10 13:49:54.478 WARN 2072 — [io-3333-exec-10] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type ‘java.lang.String’ to required type ‘java.lang.Long’; nested exception is java.lang.NumberFormatException: For input string: “{id}”]

解决办法

如果上诉代码没有写paramType = “path”会提示类型转换String convert to Long错误。

参考:

http://blog.didispace.com/springbootswagger2/

https://www.cnblogs.com/fengli9998/p/7921601.html

CDH_Hadoop高可用启用与禁用配置

发表于 2018-09-30 | 分类于 CDH配置 | | 阅读次数:

CDH高可用

环境说明

linux环境:centos7.4

CDH版本:CDH5.12.0

外置数据库(MariaDB)版本:5.5.56-MariaDB

HDFS HA

介绍

在Hadoop 2.0.0之前,NameNode是HDFS集群中的单点故障(SPOF)。每个群集都有一个NameNode,如果该机器或进程变得不可用,整个群集将无法使用,直到NameNode重新启动或在单独的计算机上启动。

阅读全文 »

HDFS数据平衡相关

发表于 2018-09-30 | 分类于 Hadoop相关 | | 阅读次数:

数据平衡

  • DataNode间数据平衡

数据不平衡原因:

  1. 向现有群集添加了新的DataNode
  2. 集群机器磁盘坏死
  3. 集群DataNode下线

解决方案:

HDFS提供了一个平衡器实用程序,可以分析块放置并平衡DataNode上的数据。平衡器移动块直到认为集群是平衡的,这意味着每个DataNode的利用率(节点上已用空间与节点总容量的比率)不同于集群的利用率(使用的空间比率)集群到集群的总容量)不超过给定的阈值百分比。但平衡器不在单个DataNode上的各个卷之间进行平衡。

  • DataNode磁盘数据平衡

    数据不平衡原因:

    当我们往HDFS上写入新的数据块,DataNode 将会使用volume选择策略来为这个块选择存储的地方。目前Hadoop支持两种volume选择策略:round-robin 和 available space(详情参见:HDFS-1804),我们可以通过 dfs.datanode.fsdataset.volume.choosing.policy 参数来设置。默认为循环(round-robin)策略将新块均匀分布在可用磁盘上;而可用空间( available-space )策略优先将数据写入具有最大可用空间的磁盘。

    默认情况下,DataNode 是使用基于round-robin策略来写入新的数据块。然而在一个长时间运行的集群中,由于HDFS中的大规模文件删除或者通过往DataNode 中添加新的磁盘仍然会导致同一个DataNode中的不同磁盘存储的数据很不均衡。即使你使用的是基于可用空间的策略,卷(volume)不平衡仍可导致较低效率的磁盘I/O。比如所有新增的数据块都会往新增的磁盘上写,在此期间,其他的磁盘会处于空闲状态,这样新的磁盘将会是整个系统的瓶颈。

    解决方案:

    1. HDFS-1312 在线磁盘均衡器,旨在根据各种指标重新平衡正在运行DataNode上的磁盘数据;离线的脚本平衡脚本
    2. 升级HDFS版本到3.0使用HDFS自带的磁盘均衡器(diskbalancer)
tomnic.wang

tomnic.wang

learn more,gain more!

4 日志
4 分类
11 标签
RSS
GitHub E-Mail Google Twitter
© 2018 tomnic.wang
由 Hexo 强力驱动
|
主题 — NexT.Gemini v5.1.4