2121
2222import org .apache .iotdb .db .protocol .mqtt .Message ;
2323import org .apache .iotdb .db .protocol .mqtt .PayloadFormatter ;
24- import org .apache .iotdb .db .protocol .mqtt .TreeMessage ;
24+ import org .apache .iotdb .db .protocol .mqtt .TableMessage ;
2525
26+ import com .google .common .collect .Lists ;
27+ import com .google .gson .Gson ;
28+ import com .google .gson .GsonBuilder ;
29+ import com .google .gson .JsonArray ;
30+ import com .google .gson .JsonElement ;
31+ import com .google .gson .JsonObject ;
32+ import com .google .gson .JsonParseException ;
2633import io .netty .buffer .ByteBuf ;
34+ import org .apache .tsfile .enums .TSDataType ;
2735import org .apache .tsfile .external .commons .lang3 .NotImplementedException ;
2836
37+ import java .nio .charset .StandardCharsets ;
2938import java .util .ArrayList ;
3039import java .util .Arrays ;
31- import java .util .Collections ;
3240import java .util .List ;
3341
42+ /**
43+ * The Customized JSON payload formatter. one json format supported: { "time":1586076045523,
44+ * "deviceID":"car_1", "deviceType":"new energy vehicle", "point":"velocity", "value":80.0 }
45+ */
3446public class CustomizedJsonPayloadFormatter implements PayloadFormatter {
47+ private static final String JSON_KEY_TIME = "time" ;
48+ private static final String JSON_KEY_DEVICEID = "deviceID" ;
49+ private static final String JSON_KEY_DEVICETYPE = "deviceType" ;
50+ private static final String JSON_KEY_POINT = "point" ;
51+ private static final String JSON_KEY_VALUE = "value" ;
52+ private static final Gson GSON = new GsonBuilder ().create ();
3553
3654 @ Override
3755 public List <Message > format (String topic , ByteBuf payload ) {
38- // Suppose the payload is a json format
3956 if (payload == null ) {
40- return Collections . emptyList ();
57+ return new ArrayList <> ();
4158 }
42-
43- // parse data from the json and generate Messages and put them into List<Message> ret
44- List <Message > ret = new ArrayList <>();
45- // this is just an example, so we just generate some Messages directly
46- for (int i = 0 ; i < 2 ; i ++) {
47- long ts = i ;
48- TreeMessage message = new TreeMessage ();
49- message .setDevice ("d" + i );
50- message .setTimestamp (ts );
51- message .setMeasurements (Arrays .asList ("s1" , "s2" ));
52- message .setValues (Arrays .asList ("4.0" + i , "5.0" + i ));
53- ret .add (message );
59+ String txt = payload .toString (StandardCharsets .UTF_8 );
60+ JsonElement jsonElement = GSON .fromJson (txt , JsonElement .class );
61+ if (jsonElement .isJsonObject ()) {
62+ JsonObject jsonObject = jsonElement .getAsJsonObject ();
63+ return formatTableRow (topic , jsonObject );
64+ } else if (jsonElement .isJsonArray ()) {
65+ JsonArray jsonArray = jsonElement .getAsJsonArray ();
66+ List <Message > messages = new ArrayList <>();
67+ for (JsonElement element : jsonArray ) {
68+ JsonObject jsonObject = element .getAsJsonObject ();
69+ messages .addAll (formatTableRow (topic , jsonObject ));
70+ }
71+ return messages ;
5472 }
55- return ret ;
73+ throw new JsonParseException ( "payload is invalidate" ) ;
5674 }
5775
5876 @ Override
@@ -61,14 +79,54 @@ public List<Message> format(ByteBuf payload) {
6179 throw new NotImplementedException ();
6280 }
6381
82+ private List <Message > formatTableRow (String topic , JsonObject jsonObject ) {
83+ TableMessage message = new TableMessage ();
84+ String database = !topic .contains ("/" ) ? topic : topic .substring (0 , topic .indexOf ("/" ));
85+ String table = "test_table" ;
86+
87+ // Parsing Database Name
88+ message .setDatabase ((database ));
89+
90+ // Parsing Table Name
91+ message .setTable (table );
92+
93+ // Parsing Tags
94+ List <String > tagKeys = new ArrayList <>();
95+ tagKeys .add (JSON_KEY_DEVICEID );
96+ List <Object > tagValues = new ArrayList <>();
97+ tagValues .add (jsonObject .get (JSON_KEY_DEVICEID ).getAsString ());
98+ message .setTagKeys (tagKeys );
99+ message .setTagValues (tagValues );
100+
101+ // Parsing Attributes
102+ List <String > attributeKeys = new ArrayList <>();
103+ List <Object > attributeValues = new ArrayList <>();
104+ attributeKeys .add (JSON_KEY_DEVICETYPE );
105+ attributeValues .add (jsonObject .get (JSON_KEY_DEVICETYPE ).getAsString ());
106+ message .setAttributeKeys (attributeKeys );
107+ message .setAttributeValues (attributeValues );
108+
109+ // Parsing Fields
110+ List <String > fields = Arrays .asList (JSON_KEY_POINT );
111+ List <TSDataType > dataTypes = Arrays .asList (TSDataType .FLOAT );
112+ List <Object > values = Arrays .asList (jsonObject .get (JSON_KEY_VALUE ).getAsFloat ());
113+ message .setFields (fields );
114+ message .setDataTypes (dataTypes );
115+ message .setValues (values );
116+
117+ // Parsing timestamp
118+ message .setTimestamp (jsonObject .get (JSON_KEY_TIME ).getAsLong ());
119+ return Lists .newArrayList (message );
120+ }
121+
64122 @ Override
65123 public String getName () {
66124 // set the value of mqtt_payload_formatter in iotdb-common.properties as the following string:
67- return "CustomizedJson " ;
125+ return "CustomizedJson2Table " ;
68126 }
69127
70128 @ Override
71129 public String getType () {
72- return PayloadFormatter .TREE_TYPE ;
130+ return PayloadFormatter .TABLE_TYPE ;
73131 }
74132}
0 commit comments