How to parse JSON in Java using a high performance parser? - java

My input is an array with approx. 2,000 elements received every second, sometimes several times per second. Every element of the array has 3 Big Decimals.
Jackson (com.fasterxml) is the slowest part of otherwise sub-millisecond app and takes 15 milliseconds (avg). The slow function is objectMapper.readValue(text, MyDto.class);
When using a custom JSON parsing algorithm based on substring, it takes microseconds. With ObjectMapper it's 15 milliseconds.
Parsing JSON via substring is a bad practice because the code is verbose and prone to bugs.
What would you use for JSON parsing? The requirement is a very fast algorithm.
https://github.com/ngs-doo/dsl-json I found DSL json, but don't know hot to make it parse a String with JSON into my DTO. I haven't found a simple fast algorithm to parse JSON from String into a DTO.
EDIT: The input to be parsed is at:
https://pastebin.com/831YtBdq
Code:
public class BitstampOrderBook {
private long timestamp;
private List<List<BigDecimal>> bids;
private List<List<BigDecimal>> asks;
public BitstampOrderBook() {
}
public BitstampOrderBook(long timestamp, List<List<BigDecimal>> bids, List<List<BigDecimal>> asks) {
this.timestamp = timestamp;
this.bids = bids;
this.asks = asks;
}
public long getTimestamp() {
return timestamp;
}
public List<List<BigDecimal>> getBids() {
return bids;
}
public List<List<BigDecimal>> getAsks() {
return asks;
}
}
public class BitstampOrder {
private BigDecimal price;
private BigDecimal amount;
private String datetime;
private int id;
#SerializedName("order_type")
private int orderType;
public BigDecimal getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
public BigDecimal getAmount() {
return amount;
}
public void setAmount(BigDecimal amount) {
this.amount = amount;
}
public String getDatetime() {
return datetime;
}
public void setDatetime(String datetime) {
this.datetime = datetime;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getOrderType() {
return orderType;
}
public void setOrderType(int orderType) {
this.orderType = orderType;
}
#Override
public String toString() {
return "BitStampOrder{" +
"price=" + price +
", amount=" + amount +
", datetime='" + datetime + '\'' +
", id='" + id + '\'' +
", orderType='" + orderType + '\'' +
'}';
}
}
Main:
BitstampOrderBook orderBook = objectMapper.readValue(jsonString, BitstampOrderBook.class);
JProfiler (Jackson):
https://i.imgur.com/mjdbDQe.png
EDIT 2:
JProfiler (Gson):
https://i.imgur.com/WcHVhhd.png
As can be seen from JProfiler, Gson is several times faster than Jackson. What would be even faster than Gson?

Do not use strings as input, parse buffered bytes that you read from the wire or disks immediately instead.
Do not bind to BigDecimal, use double primitive instead + efficient formatter according to your rules if you will need to serialize its value back to JSON with the same precision.
Use 2-field class for your inner arrays of price/amount pairs.
Switch to Scala and use jsoniter-scala (DECLAIMER: I'm a contributor to this amazing library).
Done all these steps and you will get more than 400Mb per second speed when parsing of your samples in one thread.
See results of GeoJSONReading which parses mostly the same kind of JSON (arrays of 2-value sub-arrays of numbers) here.

Related

org.springframework.dao.IncorrectResultSizeDataAccessException Mongo Limit dosen't work?

GoodEvening
i'm new about mongo, and i choose to use a mix & match of RepositoryPattern and Mongotemplate
Now i find a very strange error when i want to use MongoTemplate to retrive a specific value of an inner object in order to calculate a progressive ID
It seems my code work for 0 or 1 document inside a collection, but when i have equal or more than 2 documents, the method will throw a
org.springframework.dao.IncorrectResultSizeDataAccessException: Query { "$java" : Query: {}, Fields: {}, Sort: {} } returned non unique result.
as if limit dosen't work.
I have just, for now, a rest and a repository pattern, but in the service layer i have another autowire whit mongotemplate class, this template is used by just 1 method.
public DetailedOrder findTheBiggestBy(String byWhat){
Query query = new Query();
query.with(Sort.by(Sort.Direction.DESC,byWhat)).limit(1);
return mongoDb.findOne(query,DetailedOrder.class);
}
As you can see is pretty simple and it actually work, becouse before moving the Template inside Service it actually work whit various documents inside collection, when i autowired it over the rest layer.
There is something i miss and is not rly related to mongo but related to spring autowire?
My detailorderClass is
package com.service.backend.BK.Pojo;
import com.service.backend.BK.Constants.Constant;
import org.bson.codecs.pojo.annotations.BsonId;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;
import org.springframework.data.mongodb.core.mapping.FieldType;
import org.springframework.data.mongodb.core.mapping.MongoId;
import java.util.List;
#Document("Orders")
public class DetailedOrder {
#MongoId(FieldType.OBJECT_ID)
private String id;
#Field
private List<Integer> category;
// 1 junk to 10 Pristine
#Field
private Integer quality;
#Field
private BaseOrder baseOrder;
#Field
private String qualityDescription;
public DetailedOrder(){}
public DetailedOrder(String Description,Double price, List<Integer> category, Integer quality) {
this.category = category;
this.quality = quality;
this.qualityDescription = qualityDescriptionPairFactory(quality);
this.baseOrder=new BaseOrder(Description,price);
}
public List<Integer> getCategory() {
return category;
}
public void setCategory(List<Integer> category) {
this.category = category;
}
public Integer getQuality() {
return quality;
}
public void setQuality(Integer quality) {
this.quality = quality;
}
public String getQualityDescription() {
return qualityDescription;
}
public void setQualityDescription(String qualityDescription) {
this.qualityDescription = qualityDescription;}
private String qualityDescriptionPairFactory(int quality){
switch (quality){
case 1:return Constant.Quality.NOGRAD.label;
case 2:return Constant.Quality.HEAVYDMN.label;
case 3:return Constant.Quality.LOOSE.label;
case 4:return Constant.Quality.POOR.label;
case 5:return Constant.Quality.LIGHTDMN.label;
case 6:return Constant.Quality.GOOD.label;
case 7:return Constant.Quality.EXCELENT.label;
case 8:return Constant.Quality.NEARMINT.label;
case 9:return Constant.Quality.MINT.label;
case 10:return Constant.Quality.NEWUNRL.label;
default:return Constant.Quality.NOGRAD.label; }
}
#Override
public String toString() {
return "DetailedOrder{" +
"id='" + id + '\'' +
", baseOrder='" + getBaseOrder().returnBaseOrder() + '\'' +
", category=" + category +
", quality=" + quality +
", qualityDescription='" + qualityDescription + '\'' +
'}';
}
public BaseOrder getBaseOrder() {
return baseOrder;
}
public void setBaseOrder(BaseOrder baseOrder) {
this.baseOrder = baseOrder;
}
public void setId(String id) {
this.id = id;
}
}
and that lead to baseOrderClass
package com.service.backend.BK.Pojo;
import org.joda.time.DateTime;
import org.springframework.data.mongodb.core.mapping.Document;
public class BaseOrder {
private String id;
private String description;
private Double desideredPrice;
//Pending,active,Rejected,Hault,etc etc
private int status;
protected BaseOrder(){}
protected BaseOrder(String description, Double desideredPrice) {
this.description = description;
this.desideredPrice = desideredPrice;
}
public String returnBaseOrder(){
return "BaseOrder{" +
"id='" + id + '\'' +
", description='" + description + '\'' +
", desideredPrice=" + desideredPrice +
", status=" + status +
'}';
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Double getDesideredPrice() {
return desideredPrice;
}
public void setDesideredPrice(Double desideredPrice) {
this.desideredPrice = desideredPrice;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
//###############################################################################################################//
}
The OPs question related to a spring context issue. By just accessing the repository directly the problem could be resolved.
Within the comments an additional question/target has been found, i would like to provide an answer for that.
How to have persistence access within the domain layer?
I have created a repository at GitHub where you can find a working solution. This code is not best practice but will provide a solution that works.
Every constructed domain object needs access to the declared repositories. That can be a problem when using dependency injection. In order to keep entity construction pristine an abstract class has been introduces that takes the task to get the concrete repositories.
The core idea is to make the Spring ApplicationContext accessable statically. Having this context an access to the repositories is just one call away.
Beans having the ApplicationContextAware interface will be called in a very early stage of Spring Boots startup. When using this class to access the context every consumer has to call it after Spring has been loaded. This can be achieved by using #Component, #Configuration, #Bean to run code and there will be no race condition.
Further details are within the repositories readme file.
I hope that this helps you :)

How to deserialize a String to a Date with Morphia

I have a Mongo collection with objects of this format:
{
id: 1,
date: "2020-08-06T12:00:00Z",
...
}
I have Java code that needs to read from this collection but never writes to it. The process that writes to this collection is not owned by me so I can't necessarily change the format of that date string. I initially tried to model my Java Morphia object like this:
#Entity public class MyDocument {
#Id
private Integer id;
private Date date;
...
}
This did not work because Morphia didn't know how to deserialize that date format into a Date object. The solution that I came up with was treating the date as a String on the POJO and then having a getDate() method that did the actual deserialization. I am wondering, is there a better way for me to do this? I know if you're using Jackson you can annotate certain fields with #JsonDeserialize and pass a deserializer so I was wondering if there was something similar for Morphia.
My solution (which feels suboptimal to me):
#Entity public class MyDocument {
#Id
private Integer id;
private String date;
...
private Date getDate() {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
try {
return dateFormat.parse(date);
} catch (Exception ex) {
return null;
}
}
}
You can go ahead and create a simple converter extending the TypeConverter like so:
public class DateConverter extends TypeConverter {
private static final String FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'";
private final SimpleDateFormat simpleDateFormat;
public DateConverter() {
super(Date.class);
this.simpleDateFormat = new SimpleDateFormat(FORMAT);
}
#Override
public Object decode(Class<?> targetClass, Object fromDBObject, MappedField optionalExtraInfo) {
try {
return simpleDateFormat.parse(((String) fromDBObject));
} catch (ParseException e) {
return null;
}
}
}
The go ahead and register your formatter for your document entity like so:
#Entity("Documents")
#Converters(DateConverter.class)
public class Document {
#Id
private Integer id;
public Integer getId() { return id; }
public void setId(Integer id) { this.id = id; }
private Date date;
public Date getDate() { return date; }
public void setDate(Date date) { this.date = date; }
#Override
public String toString() {
return "Document{" +
"id=" + id +
", date=" + date +
'}';
}
}
This will effectively tell Morphia to decode the database incoming values via parsing the string with the desired pattern, resulting directly into a concrete Date object without any additional conversion logic.

Filtering Custom Data Structure In Spark

I am trying to read a csv file into JavaRDD. In order to do that, I wrote the code below:
SparkConf conf = new SparkConf().setAppName("NameOfApp").setMaster("spark://Ip here:7077");
JavaSparkContext sc = new JavaSparkContext(conf);
JavaRDD<CurrencyPair> rdd_records = sc.textFile(System.getProperty("user.dir") + "/data/data.csv", 2).map(
new Function<String, CurrencyPair>() {
public CurrencyPair call(String line) throws Exception {
String[] fields = line.split(",");
CurrencyPair sd = new CurrencyPair(Integer.parseInt(fields[0].trim()), Double.parseDouble(fields[1].trim()),
Double.parseDouble(fields[2].trim()), Double.parseDouble(fields[3]), new Date(fields[4]));
return sd;
}
}
);
My data file looks like this:
1,0.034968,212285,7457.23,"2019-03-08 18:36:18"
Here, in order to check that if my data loaded correctly or not, I tried to print some of them:
System.out.println("Count: " + rdd_records.count());
List<CurrencyPair> list = rdd_records.top(5);
System.out.println(list.toString());
But I had following error at both system out lines. I tried each of them alone as well rather than printing count and list at the same time.
Caused by: java.lang.ClassCastException: cannot assign instance of java.lang.invoke.SerializedLambda to field org.apache.spark.rdd.MapPartitionsRDD.f of type scala.Function3 in instance of org.apache.spark.rdd.MapPartitionsRDD
My custom object looks like this:
public class CurrencyPair implements Serializable {
private int id;
private double value;
private double baseVolume;
private double quoteVolume;
private Date timeStamp;
public CurrencyPair(int id, double value, double baseVolume, double quoteVolume, Date timeStamp) {
this.id = id;
this.value = value;
this.baseVolume = baseVolume;
this.quoteVolume = quoteVolume;
this.timeStamp = timeStamp;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getValue() {
return value;
}
public void setValue(double value) {
this.value = value;
}
public double getBaseVolume() {
return baseVolume;
}
public void setBaseVolume(double baseVolume) {
this.baseVolume = baseVolume;
}
public double getQuoteVolume() {
return quoteVolume;
}
public void setQuoteVolume(double quoteVolume) {
this.quoteVolume = quoteVolume;
}
public Date getTimeStamp() {
return timeStamp;
}
public void setTimeStamp(Date timeStamp) {
this.timeStamp = timeStamp;
}
}
So I could not figured out what is wrong here. What am I doing wrong?
Edit: It works well when I write local instead of my own spark master IP. But I need to run this on my own IP. So what can be wrong with my master node?
The issue is probably the anonymous class definition new Function<String, CurrencyPair>() { which forces Spark to try to serialize the parent class as well. Try a lambda instead:
rdd_records.map(
(Function<String, CurrencyPair>) line -> {
...
Note: You could read the file as a CSV instead and use the dataset API with a bean encoder to skip the manual parsing completely.

Android Room Base64

I am trying to save a base64 value into my sqlite database using Room and for some reason it's not saving. Well, i'm assuming it's not saving because when I try to read the table that has the base64 column, it returns values for all the other columns except the base64 column. What am I doing wrong?
My Entity:
#Entity(tableName = "healthCareWorkerInformation",
foreignKeys = #ForeignKey(
entity = HealthCareWorker.class,
parentColumns = {"id"},
childColumns = {"hcwId"},
onDelete = ForeignKey.CASCADE),
indices = #Index(
value = {"hcwId"}))
public class HealthCareWorkersInformation {
#PrimaryKey(autoGenerate = true)
#ColumnInfo(name = "hcwInfoId")
private long id;
private long hcwId;
//#ColumnInfo(typeAffinity = ColumnInfo.BLOB)
private String base64Image;
private String updatedAt = new SimpleDateFormat("dd MM yyyy, HH:mm",
Locale.getDefault()).format(Calendar.getInstance().getTime());
public HealthCareWorkersInformation() {
}
#Ignore
public HealthCareWorkersInformation(long hcwId) {
this.hcwId = hcwId;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public long getHcwId() {
return hcwId;
}
public void setHcwId(long hcwId) {
this.hcwId = hcwId;
}
public String getBase64Image() {
return base64Image;
}
public void setBase64Image(String base64Image) {
this.base64Image = base64Image;
}
public String getUpdatedAt() {
return updatedAt;
}
public void setUpdatedAt(String updatedAt) {
this.updatedAt = updatedAt;
}
}
My DAO:
#Insert
void insertHealthCareWorkerInformation(HealthCareWorkersInformation healthCareWorkersInformation);
#Query("SELECT * FROM HEALTHCAREWORKERINFORMATION")
LiveData<List<HealthCareWorkerInformation>> getHCWInfo();
Sample data I send through:
{"consentGiven":null,"hcwId":1,"patiendId":1,"name":"Ben","lastName":"Ben","dateOfBirth":"4/9/2019","phoneNumber":"+271234567","base64Image":"data:image/png;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAIBAQIBAQICAgICAgICAwUDAwMDAwYEBAMFBwYHBwcGBwcICQsJCAgKCAcHCg0KCgsMDAwMBwkODw0MDgsMDAz/2wBDAQICAgMDAwYDAwYMCAcIDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAz/wAARCAGQASwDASIAAhEBAxEB/8QAHgAAAgEFAQEBAAAAAAAAAAAAAgMEAAEFBgcICQr/xABSEAACAQIEAwUGAwYDBAYHCAMBAhEDIQAEEjEFQVEGEyJhcQeBkaGx8AgywRQjQtHh8QkVUhYXM2IKGCQlQ4I0NVNjcpKiGSZEVHODlLOjstT/xAAcAQADAAMBAQEAAAAAAAAAAAABAgMABAYFBwj/xAA6EQABAwIEAgYJAgYDAQAAAAABAAIRAyEEEjFBBVEGEyJ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KpqkDSwuLW+/sCGDui6YUsSb3AgiMXZwCzhQ1QgA2gjoPL76YO8IsbLb/NKet+0VdAEgMSREfTyuMC16heZAEg6rrfb12+HwWGWrBckEtYAfwjb5/p"}
Everything else gets saved except for the base64 column. Please assist.
The problem is the semicolon in data:image/png;base64, in combination with " double quotes.
The most easy would be to save the base64 string without that prefix, which breaks the syntax and then assume it is all PNG images (or add a further field, which indicates the encoding of each image).
This problem isn't specific to Room, but specific to Java with SQLite, because that semicolon terminates the statement (which Room will generate). In Java, one can use ' single-quotes only for the primitive data-type char, while the complex data-type String excepts " double-quotes. The only way to get around this limitation, is not trying to save a String containing a ;.
To provide an example of what I mean:
private String base64String = null;
private String base64Type = "png";
public String getBase64Image() {
if(this.base64String != null) {
return "data:image/" + this.base64Type + ";base64," + this.base64String;
} else {
return null;
}
}

Mule JSON data to objects

I have a json object
data = {
'ad': {
"date":"2013-06-05",
"catagory":"6",
"subcatagory":"5",
"text":"John john",
"ssn":"1306743999",
"email":"jonbrynjar#365.is",
"phone":"8612001"
},
'cc-info': {
"amount": "70",
"cardNumber": "4222222222222",
"expiryDate": "1215",
"currency": "ISK"
},
'dates': [
{ 'date': '2013-06-18', 'media': 1 },
{ 'date': '2013-06-19', 'media': 3 }
]
}
Then I have a subflow that takes the "cc-info" part of that json object and uses that data to call a third party service.
To extract the "cc-info" part of the json object I use #JsonAutoDetect class
#JsonAutoDetect
public class Handpoint {
private String amount;
private String cardNumber;
private String expiryDate;
private String currency;
public String getAmount() { return this.amount; }
public void setAmount(String amount) { this.amount = amount; }
public String getCardNumber() { return this.cardNumber; }
public void setCardNumber(String cardNumber) { this.cardNumber = cardNumber; }
public String getExpiryDate() { return this.expiryDate; }
public void setExpiryDate(String expireDate) { this.expiryDate = expireDate; }
public String getCurrency() { return this.currency; }
public void setCurrency(String currency) { this.currency = currency; }
}
When I send in the whole json object I get an error.
The question is: Do I have to put every variable in the json object into my #JsonAutoDetect class ?
Or what would be best practice for this.
I have verified that my code works when I just send in the "cc-info" part of the json objcet.
You don't need that #JsonAutoDetect, it doesn't do anything different from defaults without arguments.
But if your question is whether you can just ignore unknown properties, answer is yes. Here are couple of ways.
For example:
mapper.disable(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES);
would do the trick.
There is an easier way to convert your JSON element to a series of objects. Have you tried the Google GSon library? There is a sample:
import com.google.gson.Gson;
Gson gson = new Gson();
Handpoint testing = gson.fromJson(data, Handpoint.class);
System.out.println("Amount: " + testing.getAmount());
On the other hand, if you want to deserialize the dates, that contain arrays, you'd better take a look here:
Gson Array deserialization

Categories