嗨,我是kafka的新手,我正在用kafka流编写一个Java项目,该项目可以检测设备是否出现在用户定义的范围内。
我在内存状态中使用2个,其中一个称为记录,另一个称为GPS-info
builder.addStateStore(Stores.keyValueStoreBuilder(
Stores.inMemoryKeyValueStore("records"),
Serdes.String(),
Serdes.Integer()),
"Process");
builder.addStateStore(Stores.keyValueStoreBuilder(
Stores.inMemoryKeyValueStore("GPS"),
Serdes.String(),
Serdes.String()),
"Process");
功能是,如果用户输入的是GPS配置,它将更新为GPS;如果用户输入的是GPS坐标,它将首先找到GPS配置,然后检测设备是否在范围内。
@Override
@SuppressWarnings("unchecked")
public void init(final ProcessorContext context) {
this.kvStore = (KeyValueStore<String, Integer>) context.getStateStore("records");
this.GPSStore = (KeyValueStore<String, String>) context.getStateStore("GPS");
this.context = context;
});
}
@Override
public void process(String dummy, String line) {
String[] words = line.toLowerCase(Locale.getDefault()).split(" ");
String Device_ID = words[0];
if(words.length == 3){
String GPS_info = this.GPSStore.get(Device_ID);
System.out.println("Device_ID " + Device_ID + " GPS_info " + GPS_info);
if(GPS_info == null){
System.out.println("Haven't find the GPS information of this Device");
}else{
String[] GPS_data = GPS_info.split(" ");
Range range = new Range(Double.parseDouble(GPS_data[0]), Double.parseDouble(GPS_data[1]), Double.parseDouble(GPS_data[2]));
Integer oldValue = this.kvStore.get(Device_ID);
System.out.println("DeviceID: " + Device_ID + " POS_X: " + words[1] + " POX_Y: " + words[2]);
double distance = range.getDistance(Double.parseDouble(words[1]), Double.parseDouble(words[2]));
int isInside = range.isAppear(distance) ? 0 : 1;
if (oldValue == null) {
System.out.println("Create new states Device_ID: " + Device_ID + " " + isInside);
this.kvStore.put(Device_ID, isInside);
} else {
System.out.println("Found States " + Device_ID + " " + isInside);
if(isInside != oldValue){
this.kvStore.put(Device_ID, isInside);
System.out.println("Found States " + Device_ID + "changed to" + isInside);
}else{
System.out.println("Found States " + Device_ID + "not changed");
}
}
}
}else if(words.length == 4){
System.out.println("Now add GPS information");
if(Integer.parseInt(words[3]) < 0){
System.out.println("Range cannot be negative");
}else{
String value = words[1] + " " + words[2] + " " + words[3];
this.GPSStore.put(Device_ID, value);
System.out.println("DeviceID: " + Device_ID + " POS_X: " + words[1] + " POX_Y: " + words[2] + " Range: " + words[3] + " add to GPSStore");
this.printStateStore(this.GPSStore);
}
}else{
System.out.println("Input is not vaild");
}
context.commit();
}
在这里我遇到了2个问题。
首先,我首先将设备配置添加到GPS状态存储中,然后输入表示未找到GPS信息的坐标,然后将所有记录打印到状态存储中,然后找到它。
Device_ID 2 GPS_info null
Haven't find the GPS information of this Device
Now add GPS information
DeviceID: 3 POS_X: 0 POX_Y: 0 Range: 10 add to GPSStore
[2, 0 0 10]
[3, 0 0 10]
Device_ID 1 GPS_info 0 0 10
DeviceID: 1 POS_X: 1 POX_Y: 1
Found States 1 0
Found States 1not changed
Device_ID 3 GPS_info null
Haven't find the GPS information of this Device
就像设备ID 2和3一样,它可以在状态存储中打印,但系统找不到它
第二,我可以找到Device_ID 1的GPS信息,但是无法在状态存储中打印
public void printStateStore(KeyValueStore<String, String> statestore){
KeyValueIterator<String, String> iter = statestore.all();
while (iter.hasNext()){
KeyValue<String, String> entry = iter.next();
System.out.println("[" + entry.key + ", " + entry.value + "]");
}
}