|
|
@ -10,6 +10,7 @@ import com.qniao.iot.machine.command.MachineOutputCommand; |
|
|
import com.qniao.iot.machine.schema.MachineOutputCommandDeserializationSchema; |
|
|
import com.qniao.iot.machine.schema.MachineOutputCommandDeserializationSchema; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
import org.apache.flink.api.common.state.*; |
|
|
import org.apache.flink.api.common.state.*; |
|
|
|
|
|
import org.apache.flink.api.common.time.Time; |
|
|
import org.apache.flink.api.common.typeinfo.TypeInformation; |
|
|
import org.apache.flink.api.common.typeinfo.TypeInformation; |
|
|
import org.apache.flink.configuration.Configuration; |
|
|
import org.apache.flink.configuration.Configuration; |
|
|
import org.apache.flink.streaming.api.CheckpointingMode; |
|
|
import org.apache.flink.streaming.api.CheckpointingMode; |
|
|
@ -68,8 +69,6 @@ public class IotMonitoringDataJob { |
|
|
return requestConfigBuilder; |
|
|
return requestConfigBuilder; |
|
|
})); |
|
|
})); |
|
|
|
|
|
|
|
|
private static final ReentrantLock lock = new ReentrantLock(true); |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* 当前索引日期后缀 |
|
|
* 当前索引日期后缀 |
|
|
*/ |
|
|
*/ |
|
|
@ -107,9 +106,19 @@ public class IotMonitoringDataJob { |
|
|
@Override |
|
|
@Override |
|
|
public void open(Configuration parameters) { |
|
|
public void open(Configuration parameters) { |
|
|
|
|
|
|
|
|
|
|
|
// 设置10分钟的过期时间 |
|
|
|
|
|
StateTtlConfig ttlConfig = StateTtlConfig.newBuilder(Time.minutes(10)) |
|
|
|
|
|
.setUpdateType(StateTtlConfig.UpdateType.OnCreateAndWrite) |
|
|
|
|
|
.setStateVisibility(StateTtlConfig.StateVisibility.NeverReturnExpired) |
|
|
|
|
|
.build(); |
|
|
|
|
|
|
|
|
|
|
|
ValueStateDescriptor<DeviceTotalData> deviceTotalDataValue = new ValueStateDescriptor<>("deviceTotalData", |
|
|
|
|
|
TypeInformation.of(DeviceTotalData.class)); |
|
|
|
|
|
// 设置状态值的过期时间,为了解决手动插入数据但是状态值不同步的问题 |
|
|
|
|
|
deviceTotalDataValue.enableTimeToLive(ttlConfig); |
|
|
|
|
|
|
|
|
// 必须在 open 生命周期初始化 |
|
|
// 必须在 open 生命周期初始化 |
|
|
deviceTotalDataStat = getRuntimeContext() |
|
|
|
|
|
.getState(new ValueStateDescriptor<>("accJobCountDuration", TypeInformation.of(DeviceTotalData.class))); |
|
|
|
|
|
|
|
|
deviceTotalDataStat = getRuntimeContext().getState(deviceTotalDataValue); |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -448,7 +457,6 @@ public class IotMonitoringDataJob { |
|
|
try { |
|
|
try { |
|
|
boolean exists = restHighLevelClient.indices().exists(exist, RequestOptions.DEFAULT); |
|
|
boolean exists = restHighLevelClient.indices().exists(exist, RequestOptions.DEFAULT); |
|
|
if (!exists) { |
|
|
if (!exists) { |
|
|
lock.lock(); |
|
|
|
|
|
// 创建索引 |
|
|
// 创建索引 |
|
|
CreateIndexRequest request = new CreateIndexRequest(indicesName); |
|
|
CreateIndexRequest request = new CreateIndexRequest(indicesName); |
|
|
// 字段映射 |
|
|
// 字段映射 |
|
|
@ -506,8 +514,6 @@ public class IotMonitoringDataJob { |
|
|
} |
|
|
} |
|
|
} catch (Exception e) { |
|
|
} catch (Exception e) { |
|
|
e.printStackTrace(); |
|
|
e.printStackTrace(); |
|
|
}finally { |
|
|
|
|
|
lock.unlock(); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |