【问题标题】:Dynamically creating number of tags in influxdb-java API point to insert a point in influxDB在influxdb-java API点中动态创建标签数量以在influxDB中插入一个点
【发布时间】:2017-12-06 11:57:10
【问题描述】:

我正在尝试使用 influxdb-java API 将 oracle 数据插入到 influxdb。

我想编写一个通用代码,根据我的 SQL 查询输出字段将数据插入到 influx 中。

目前我正在使用 switch 并根据我从查询中获得的属性数量制作多个案例。 参考代码:

switch (columnLength) {
case 1:
    sotPoint = Point.measurement(measurementName)   
            .time(System.currentTimeMillis(),TimeUnit.MILLISECONDS)
            .tag("source", "SOT")
            .tag(attributes[0],rsObj.getString(attributes[0]))
            .addField("recordcount", 1)
            .build();
    break;
case 2: 
    sotPoint = Point.measurement(measurementName)   
            .time(System.currentTimeMillis(),TimeUnit.MILLISECONDS)
            .tag("source", "SOT")
            .tag(attributes[0],rsObj.getString(attributes[0]))
            .tag(attributes[1],rsObj.getString(attributes[1]))
            .addField("recordcount", 1)
            .build();
    break;
default: System.out.println("Invalid column length");
}

有没有办法单独动态创建标签字段,并在创建点时只在该点添加这些字段?

我使用的是influxdb-java API 2.2版本

<dependency>
<groupId>org.influxdb</groupId>
<artifactId>influxdb-java</artifactId>
<version>2.2</version>
</dependency>

【问题讨论】:

    标签: java influxdb


    【解决方案1】:

    我们可以使用构建器对象实现上述目标:

        Builder builder=Point.measurement(measurementName).time(date,TimeUnit.MILLISECONDS).tag("source", source);
    
        for (Map.Entry<String, String> entry : tagMap.entrySet()) {
            builder.tag(entry.getKey(),entry.getValue());
        }
    
        for (Map.Entry<String, Long> entry : filedMap.entrySet()) {
            builder.addField(entry.getKey(),entry.getValue());
        }
    
        builder.addField("recordcount", 1);
        point=builder.build();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-18
      • 2019-09-22
      • 2020-02-20
      • 2017-05-09
      • 2023-03-19
      相关资源
      最近更新 更多