0%

问题

// 默认情况,java客户端连接服务端等待数据返回的时间很短,如果超时就会报以下错误
org.influxdb.InfluxDBIOException: java.net.SocketTimeoutException: timeout

解决办法

  • java
OkHttpClient.Builder client = new OkHttpClient.Builder()
        .connectTimeout(1, TimeUnit.MINUTES)
        .readTimeout(1, TimeUnit.MINUTES)
        .writeTimeout(1, TimeUnit.MINUTES)
        .retryOnConnectionFailure(true);
InfluxDB influxdb = InfluxDBFactory.connect("http://localhost:8086", client);
  • Scala
val client = OkHttpClient.Builder()
        .connectTimeout(1, TimeUnit.MINUTES)
        .readTimeout(1, TimeUnit.MINUTES)
        .writeTimeout(1, TimeUnit.MINUTES)
        .retryOnConnectionFailure(true)
val influxConnection = InfluxDBFactory.connect("http://localhost:8086", client)

参考链接

准备测试数据

public static void main(String[] args) {
        InfluxDB influxDB = InfluxDBFactory.connect("http://192.168.41.128:8086", "root", "root");
        String dbName = "mydb";
        influxDB.query(new Query("CREATE DATABASE " + dbName));
        long time = 1600327592000L;
        // 每张表内5w条数据
        int count = 50000;
        while (count > 0) {
            System.out.println(count);
            count--;
            // 每条数据间隔5秒
            time = time + 5000;
            BatchPoints batchPoints = BatchPoints
                    .database(dbName)
                    .tag("async", "true")
                    .consistency(InfluxDB.ConsistencyLevel.ALL)
                    .build();
            for (int i = 0; i < 10000; i++) {
                // 表名node-0 ~ node-9999,共10000张表,数据随机生成
                Point point1 = Point.measurement("node-" + i)
                        .time(time, TimeUnit.MILLISECONDS)
                        .addField("idle", new Random().nextInt(100))
                        .addField("user", new Random().nextInt(100))
                        .addField("system", new Random().nextInt(100))
                        .build();
                batchPoints.point(point1);
            }
            influxDB.write(batchPoints);
        }
        influxDB.close();
    }

使用正则表达式查询测试

目前influxdb支持在以下场景中使用正则表达式:

  • field keys and tag keys in the SELECT clause
  • measurements in the FROM clause
  • tag values and string field values in the WHERE clause.
  • tag keys in the GROUP BY clause
-- 查询格式
-- Supported operators
-- =~ matches against !~ doesn’t match against
SELECT /<regular_expression_field_key>/ FROM /<regular_expression_measurement>/ WHERE [<tag_key> <operator> /<regular_expression_tag_value>/ | <field_key> <operator> /<regular_expression_field_value>/] GROUP BY /<regular_expression_tag_key>/
-- 测试用例
SELECT /l/ FROM "h2o_feet" LIMIT 1
SELECT MEAN("degrees") FROM /temperature/
SELECT MEAN(water_level) FROM "h2o_feet" WHERE "location" =~ /[m]/ AND "water_level" > 3
SELECT * FROM "h2o_feet" WHERE "location" !~ /./
SELECT MEAN("water_level") FROM "h2o_feet" WHERE "location" =~ /./
SELECT MEAN("water_level") FROM "h2o_feet" WHERE "location" = 'santa_monica' AND "level description" =~ /between/
SELECT FIRST("index") FROM "h2o_quality" GROUP BY /l/

测试使用正则表达式进行切面查询

-- 查询某个时间点,所有表的数据,切面查询
SELECT * FROM /node-/ WHERE time='2020-09-18T01:43:27Z'
-- 查询node-1001,node-1002,node-1003,node-1004,node-1005数据
SELECT * FROM /(node-100)[1-5]/ WHERE time='2020-09-18T01:43:27Z'

参考链接

步骤

  1. 导出配置
docker run --rm influxdb influxd config > influxdb.conf
  1. 启动
# 设置端口映射
# 设置自动重启
# 挂载配置并使用配置启动
# 挂载数据
docker run -d -p 8086:8086 --restart=always --name influxdb\
      -v $PWD/data:/var/lib/influxdb \
      -v $PWD/influxdb.conf:/etc/influxdb/influxdb.conf:ro \
      influxdb:1.7 -config /etc/influxdb/influxdb.conf

参考

返回文件流

@Service
@Slf4j
public class FileService {

    /**
     * @param fileName 文件名
     */
    public void getFile(String fileName, HttpServletResponse response) {
        try (FileInputStream inputStream = new FileInputStream(new File(fileName));
             OutputStream outputStream = response.getOutputStream();) {
            byte[] data = new byte[inputStream.available()];
            inputStream.read(data);
            // 文件格式未知字节流
            response.setContentType("application/octet-stream");
            response.addHeader("Content-Length", "" + data.length);
            // 设置文件名
            response.setHeader( "Content-Disposition","attachment; filename=" + fileName);
            outputStream.write(data);
            outputStream.flush();
        } catch (Exception e) {
            log.error("get File {} error, {}", fileName, e);
        }
    }
}
@RestController
public class FileController {

    @Autowired
    FileService fileService;

    @GetMapping("/file")
    public void getFile(@RequestParam(value = "fileName") String fileName,
                           HttpServletResponse response) {
        fileService.getFile(fileName, response);
    }
}

命令

crontab [-u username]    //省略用户表表示操作当前用户的crontab
    -e      (编辑工作表)
    -l      (列出工作表里的命令)
    -r      (删除工作作)

任务

# cron表达式说明:
*    *    *    *    *
-    -    -    -    -
|    |    |    |    |
|    |    |    |    +----- 周几 (0 - 7) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
|    |    |    +---------- 月 (1 - 12) OR jan,feb,mar,apr ...
|    |    +--------------- 日 (1 - 31)
|    +-------------------- 小时 (0 - 23)
+------------------------- 分钟 (0 - 59)

示例

# 每月的最后1天
0 0 L * * * echo "last" >> /tmp/test.txt
# 每1分钟执行一次myCommand
* * * * * myCommand
# 每小时的第3和第15分钟执行
3,15 * * * * myCommand
# 在上午8点到11点的第3和第15分钟执行
3,15 8-11 * * * myCommand
# 每星期六的晚上11 : 00 pm重启smb
0 23 * * 6 /etc/init.d/smb restart
# 每一小时重启smb
* */1 * * * /etc/init.d/smb restart

查看CPU

# 最常使用命令
top

# vmstat 命令报告关于线程、虚拟内存、磁盘、陷阱和 CPU 活动的统计信息
vmstat
# 2秒一次刷新5次
vmstat 2 5 -w

查看磁盘

# 安装
yum install sysstat
# r/s 和 w/s 分别是每秒的读操作和写操作,而rKB/s 和wKB/s 列以每秒千字节为单位显示了读和写的数据量
iostat -dx
yum -y install iotop 
# 查看每个进程使用的磁盘IO
iotop
# 可以看出磁盘空间使用情况
yum -y install agedu
# 扫描全盘
agedu -s /
# 生成一个网页可以访问查看,有访问权限要求
[root@node1 ~]# agedu -w --address 192.168.41.128:8071
Using Linux /proc/net magic authentication
URL: http://192.168.41.128:8071/ 
# 可以直接访问
agedu -w --address 192.168.41.128:8072 --auth none
URL: http://192.168.41.128:8072/

查看网络

# 安装
yum -y install epel-release
yum -y install nload
# 查看,命令后面加网卡
nload ens33
# iftop命令
yum install iftop

iftop
iftop -i eth1
# 可以查看每个进程使用的网络带宽
yum install nethogs

nethogs
nethogs ens33
# 安装
yum install -y nmap

# 扫描所有开放的端口
nmap 192.168.41.1
# -p 端口范围 主机IP
nmap -p0-1000 192.168.41.1
# -vv参数设置对结果的详细输出
nmap -p902 -vv 192.168.41.1