java 读取一个大的json文件,但是懒得这么做,以免使用太多内存。感谢Java 8流,这很有可能
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 读取一个大的json文件,但是懒得这么做,以免使用太多内存。感谢Java 8流,这很有可能相关的知识,希望对你有一定的参考价值。
Located here: https://gist.github.com/smac89/15fc3bffa4f965d18587eec2db4972dd
public class Student {
public final String studentId;
public final String name;
public final Gender gender;
public final String email;
public final Integer numberOfClasses;
public Student(String studentId, String name, Gender gender, String email, Integer numberOfClasses) {
this.studentId = studentId;
this.name = name;
this.gender = gender;
this.email = email;
this.numberOfClasses = numberOfClasses;
}
@Override
public String toString() {
return "Name: " + name + ", ID: " + studentId + ", Gender: " + gender.name() +
", email: " + email + ", Class count: " + String.valueOf(numberOfClasses);
}
public enum Gender {
Male, Female
}
}
import com.google.gson.Gson;
import com.google.gson.stream.JsonReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Type;
import java.net.URL;
import java.util.Iterator;
import java.util.Spliterators;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
public class ReadJsonFile {
private static final class JsonIterator<T> implements Iterator<T> {
private final Gson gson;
private final Type objectType;
private final JsonReader reader;
private JsonIterator(JsonReader reader, Gson gson, Type objectType) {
this.gson = gson;
this.objectType = objectType;
this.reader = reader;
}
@Override
public boolean hasNext() {
try {
return reader.hasNext();
} catch (IOException ioe) {
return false;
}
}
@Override
public T next() {
return gson.fromJson(reader, objectType);
}
}
public static <J> Stream<J> readJsonFromFile(Gson gson, URL jsonFile, Type type) throws IOException {
JsonReader reader = new JsonReader(
new BufferedReader(new InputStreamReader(jsonFile.openStream())));
reader.beginArray();
if (!reader.hasNext()) {
return Stream.empty();
}
return StreamSupport.stream(Spliterators.spliteratorUnknownSize(
new JsonIterator<J>(reader, gson, type), 0), false).onClose(() -> {
try {
reader.close();
} catch (IOException ioe) {
}
});
}
}
import com.google.common.io.Resources;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonObject;
import java.io.IOException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Stream;
public class Main {
public static void main(String args[]) {
URL data = Resources.getResource("data.json");
GsonBuilder builder = new GsonBuilder();
builder.registerTypeAdapter(Student.class, (JsonDeserializer<Student>) (json, typeOfT, context) -> {
JsonObject object = json.getAsJsonObject();
Student.Gender gender = Student.Gender.valueOf(object.get("gender").getAsString());
String firstName = object.get("first_name").getAsString();
String lastName = object.get("last_name").getAsString();
String email = object.get("email").getAsString();
String Id = object.get("id").getAsString();
int numberOfClasses = object.get("class_count").getAsInt();
return new Student(Id, firstName + " " + lastName, gender, email, numberOfClasses);
});
try (Stream<Student> studentStream = ReadJsonFile.readJsonFromFile(builder.create(), data,
Student.class)) {
studentStream.forEach(System.out::println);
} catch (IOException ioe) {
Logger.getAnonymousLogger().log(Level.WARNING, ioe,
() -> "An exception occurred when reading the json");
}
}
}
以上是关于java 读取一个大的json文件,但是懒得这么做,以免使用太多内存。感谢Java 8流,这很有可能的主要内容,如果未能解决你的问题,请参考以下文章
如果要读这么大的BOM,如何做比较合适?
如何读取真正大的 JSON 文件并使用 node.js 将该文件的数据插入 MYSQL 数据库?
Spring整合mybatis框架-Spring框架读取数据库配置文件
NodeJS:读取一个大的 csv 文件
使用 Pyspark 使用 Spark 读取巨大 Json 文件的第一行
绕过内存错误以在 Python 中读取大型 JSON 文件 [关闭]