forked from MobilityDB/MobilityFlink
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAISDataDeserializationSchema.java
More file actions
81 lines (68 loc) · 3.29 KB
/
Copy pathAISDataDeserializationSchema.java
File metadata and controls
81 lines (68 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*****************************************************************************
*
* This MobilityDB code is provided under The PostgreSQL License.
* Copyright (c) 2020-2026, Université libre de Bruxelles and MobilityDB
* contributors
*
* Permission to use, copy, modify, and distribute this software and its
* documentation for any purpose, without fee, and without a written
* agreement is hereby granted, provided that the above copyright notice and
* this paragraph and the following two paragraphs appear in all copies.
*
* IN NO EVENT SHALL UNIVERSITE LIBRE DE BRUXELLES BE LIABLE TO ANY PARTY FOR
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING
* LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
* EVEN IF UNIVERSITE LIBRE DE BRUXELLES HAS BEEN ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
*
* UNIVERSITE LIBRE DE BRUXELLES SPECIFICALLY DISCLAIMS ANY WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON
* AN "AS IS" BASIS, AND UNIVERSITE LIBRE DE BRUXELLES HAS NO OBLIGATIONS TO
* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
*
*****************************************************************************/
package aisdata;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.DeserializationFeature;
import org.apache.flink.api.common.serialization.DeserializationSchema;
import org.apache.flink.api.common.typeinfo.TypeInformation;
import org.apache.flink.api.java.typeutils.TypeExtractor;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
public class AISDataDeserializationSchema implements DeserializationSchema<AISData> {
private static final ObjectMapper objectMapper = new ObjectMapper();
public AISDataDeserializationSchema() {
objectMapper.configure(JsonParser.Feature.ALLOW_NON_NUMERIC_NUMBERS, true);
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}
@Override
public AISData deserialize(byte[] message) throws IOException {
JsonNode jsonNode = objectMapper.readTree(message);
AISData data = new AISData();
data.setTimestamp(parseTimestamp(jsonNode.get("t").asText()));
data.setMmsi(jsonNode.get("mmsi").asInt());
data.setLon(jsonNode.get("lon").asDouble());
data.setLat(jsonNode.get("lat").asDouble());
data.setSpeed(jsonNode.get("speed").asDouble());
data.setCourse(jsonNode.get("course").asDouble());
return data;
}
private long parseTimestamp(String timestampStr) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
LocalDateTime dateTime = LocalDateTime.parse(timestampStr, formatter);
return dateTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
}
@Override
public boolean isEndOfStream(AISData nextElement) {
return false;
}
@Override
public TypeInformation<AISData> getProducedType() {
return TypeExtractor.getForClass(AISData.class);
}
}