Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,28 @@ public List<Message> format(ByteBuf payload) {
return ret;
}

@Override
public List<Message> format(ByteBuf payload, String topic) {
// Suppose the payload is a json format
if (payload == null) {
return Collections.emptyList();
}

// parse data from the json and generate Messages and put them into List<Message> ret
List<Message> ret = new ArrayList<>();
// this is just an example, so we just generate some Messages directly
for (int i = 0; i < 2; i++) {
long ts = i;
TreeMessage message = new TreeMessage();
message.setDevice("d" + i);
message.setTimestamp(ts);
message.setMeasurements(Arrays.asList("s1", "s2"));
message.setValues(Arrays.asList("4.0" + i, "5.0" + i));
ret.add(message);
}
return ret;
}

@Override
public String getName() {
// set the value of mqtt_payload_formatter in iotdb-common.properties as the following string:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,38 @@ public List<Message> format(ByteBuf payload) {
throw new JsonParseException("payload is invalidate");
}

@Override
public List<Message> format(ByteBuf payload, String topic) {
if (payload == null) {
return new ArrayList<>();
}
String txt = payload.toString(StandardCharsets.UTF_8);
JsonElement jsonElement = GSON.fromJson(txt, JsonElement.class);
if (jsonElement.isJsonObject()) {
JsonObject jsonObject = jsonElement.getAsJsonObject();
if (jsonObject.get(JSON_KEY_TIMESTAMP) != null) {
return formatJson(jsonObject);
}
if (jsonObject.get(JSON_KEY_TIMESTAMPS) != null) {
return formatBatchJson(jsonObject);
}
} else if (jsonElement.isJsonArray()) {
JsonArray jsonArray = jsonElement.getAsJsonArray();
List<Message> messages = new ArrayList<>();
for (JsonElement element : jsonArray) {
JsonObject jsonObject = element.getAsJsonObject();
if (jsonObject.get(JSON_KEY_TIMESTAMP) != null) {
messages.addAll(formatJson(jsonObject));
}
if (jsonObject.get(JSON_KEY_TIMESTAMPS) != null) {
messages.addAll(formatBatchJson(jsonObject));
}
}
return messages;
}
throw new JsonParseException("payload is invalidate");
}

private List<Message> formatJson(JsonObject jsonObject) {
TreeMessage message = new TreeMessage();
message.setDevice(jsonObject.get(JSON_KEY_DEVICE).getAsString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,65 @@ public List<Message> format(ByteBuf payload) {
return messages;
}

@Override
public List<Message> format(ByteBuf payload, String topic) {
List<Message> messages = new ArrayList<>();
if (payload == null) {
return messages;
}

String txt = payload.toString(StandardCharsets.UTF_8);
String[] lines = txt.split(LINE_BREAK);
for (String line : lines) {
if (line.trim().startsWith(WELL)) {
continue;
}
TableMessage message = new TableMessage();
try {
Matcher matcher = pattern.matcher(line.trim());
if (!matcher.matches()) {
log.warn("Invalid line protocol format ,line is {}", line);
continue;
}

// Parsing Table Names
message.setTable(matcher.group(TABLE));

// Parsing Tags
if (!setTags(matcher, message)) {
log.warn("The tags is error , line is {}", line);
continue;
}

// Parsing Attributes
if (!setAttributes(matcher, message)) {
log.warn("The attributes is error , line is {}", line);
continue;
}

// Parsing Fields
if (!setFields(matcher, message)) {
log.warn("The fields is error , line is {}", line);
continue;
}

// Parsing timestamp
if (!setTimestamp(matcher, message)) {
log.warn("The timestamp is error , line is {}", line);
continue;
}

messages.add(message);
} catch (Exception e) {
log.warn(
"The line pattern parsing fails, and the failed line message is {} ,exception is",
line,
e);
}
}
return messages;
}

private boolean setTags(Matcher matcher, TableMessage message) {
List<String> tagKeys = new ArrayList<>();
List<Object> tagValues = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ public interface PayloadFormatter {
*/
List<Message> format(ByteBuf payload);

/**
* format a payload and a topic to a list of messages
*
* @param payload
* @param topic
* @return
*/
List<Message> format(ByteBuf payload, String topic);

/**
* get the formatter name
*
Expand Down