当前位置:   article > 正文

java解析超大json文件数据_java怎么解析一个超大的json

java怎么解析一个超大的json

        json文件大小为10G左右,对于这种超大的json使用jackson的objectmapper一把映射到java对象是不太可行的,内存一般都会溢出。此时需要使用jackson的JsonParser从文件中逐个token去读取。一般大json中都会存在某个数组中有超多的数据记录,我们需要解决的就是记录当前token路径,在读取到超大json数组时,再利用逐条数据读取mapper.readTree(jsonParser)逐条读取数据,利用数组缓存一定量的数据后,写入数据库后继续读取,知道json数组数据读取结束。

        这里关键的是记录当前json的token的路径,以方便地利用mapper.readTree(jsonParser)读取任意的子节点数据。

  1. import com.fasterxml.jackson.core.JsonFactory;
  2. import com.fasterxml.jackson.core.JsonParser;
  3. import com.fasterxml.jackson.core.JsonToken;
  4. import com.fasterxml.jackson.databind.JsonNode;
  5. import com.fasterxml.jackson.databind.ObjectMapper;
  6. import sarifreport.Result;
  7. import java.io.File;
  8. import java.io.IOException;
  9. public class Main {
  10. public static void main(String [] args){
  11. long start = System.currentTimeMillis();
  12. try {
  13. ObjectMapper mapper = new ObjectMapper();
  14. JsonFactory jsonFactory = new JsonFactory();
  15. JsonParser jsonParser = jsonFactory.createParser(
  16. new File("E:/result2.json"));
  17. JsonPath path = new JsonPath();
  18. while (jsonParser.nextToken() != null) {
  19. traverse(jsonParser, 0,path,mapper);
  20. }
  21. jsonParser.close();
  22. System.out.println((System.currentTimeMillis()-start)/1000+"秒");
  23. } catch (IOException e) {
  24. e.printStackTrace();
  25. }
  26. }
  27. private static void traverse(JsonParser jsonParser, int depth, JsonPath path, ObjectMapper mapper) throws IOException {
  28. JsonToken token = jsonParser.getCurrentToken();
  29. if (token == null) {
  30. return;
  31. }
  32. setPath(jsonParser,depth,path);
  33. if (path.toString().equals("root.runs[i].results[i]")) {
  34. while (jsonParser.nextToken() == JsonToken.START_OBJECT) {
  35. JsonNode jsonNode = mapper.readTree(jsonParser);
  36. // 此处可以读取一定数量的数据后入库
  37. System.out.println(jsonNode.toPrettyString());
  38. }
  39. token = jsonParser.getCurrentToken();
  40. }
  41. // Recursive call for nested structures
  42. if (token == JsonToken.START_OBJECT || token == JsonToken.START_ARRAY) {
  43. while (jsonParser.nextToken() != null && jsonParser.getCurrentToken() != JsonToken.END_OBJECT
  44. && jsonParser.getCurrentToken() != JsonToken.END_ARRAY) {
  45. traverse(jsonParser, depth + 1,path,mapper);
  46. }
  47. }
  48. }
  49. /**
  50. * 同一个depth只记录一个node
  51. * @param jsonParser
  52. * @param depth
  53. * @param path
  54. * @throws IOException
  55. */
  56. private static void setPath(JsonParser jsonParser, int depth, JsonPath path) throws IOException {
  57. JsonToken token = jsonParser.getCurrentToken();
  58. if (path.nodeLink.size() > depth+1) {
  59. path.nodeLink.subList(depth+1,path.nodeLink.size()).clear();
  60. }
  61. JsonPath.Node prefixNode = path.nodeLink.size()>=(depth+1)?path.nodeLink.get(depth):null;
  62. switch (token) {
  63. case FIELD_NAME:
  64. if (prefixNode != null) {
  65. prefixNode.nodeType = JsonPath.NODE_FIELD;
  66. prefixNode.nodeName = jsonParser.getCurrentName();
  67. } else {
  68. JsonPath.Node node = new JsonPath.Node();
  69. node.nodeType = JsonPath.NODE_FIELD;
  70. node.nodeName = jsonParser.getCurrentName();
  71. path.nodeLink.add(node);
  72. }
  73. break;
  74. case START_ARRAY:
  75. if (prefixNode != null && JsonPath.NODE_FIELD.equals(prefixNode.nodeType)) {
  76. prefixNode.nodeName = prefixNode.nodeName+"[i]";
  77. } else {
  78. JsonPath.Node node = new JsonPath.Node();
  79. node.nodeType = JsonPath.NODE_ARRAY;
  80. path.nodeLink.add(node);
  81. }
  82. break;
  83. case START_OBJECT:
  84. if (prefixNode != null && JsonPath.NODE_FIELD.equals(prefixNode.nodeType)) {
  85. prefixNode.nodeName = prefixNode.nodeName+".";
  86. } else {
  87. JsonPath.Node node = new JsonPath.Node();
  88. node.nodeType = JsonPath.NODE_OBJECT;
  89. path.nodeLink.add(node);
  90. }
  91. break;
  92. }
  93. }
  94. }
  1. import java.util.LinkedList;
  2. public class JsonPath {
  3. public static String NODE_ARRAY = "array";
  4. public static String NODE_OBJECT = "object";
  5. public static String NODE_FIELD = "field";
  6. public LinkedList<Node> nodeLink = new LinkedList<>();
  7. public static class Node{
  8. public String nodeName;
  9. public String nodeType;
  10. }
  11. @Override
  12. public String toString() {
  13. if (this.nodeLink == null || nodeLink.isEmpty()) {
  14. return "";
  15. }
  16. StringBuilder path = new StringBuilder();
  17. for (int i = 0; i < nodeLink.size(); i++ ) {
  18. Node node = this.nodeLink.get(i);
  19. if (node == null) {
  20. continue;
  21. }
  22. if (i == 0) {
  23. if (NODE_ARRAY.equals(node.nodeType)) {
  24. path.append("root[i]");
  25. } else {
  26. path.append("root");
  27. }
  28. }
  29. if (NODE_ARRAY.equals(node.nodeType)) {
  30. path.append("[i]");
  31. }else if (NODE_OBJECT.equals(node.nodeType)) {
  32. path.append(".");
  33. } else if (NODE_FIELD.equals(node.nodeType)) {
  34. path.append(node.nodeName);
  35. }
  36. }
  37. return path.toString();
  38. }
  39. }
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>org.example</groupId>
  7. <artifactId>BigJsonDeal</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <properties>
  10. <maven.compiler.source>19</maven.compiler.source>
  11. <maven.compiler.target>19</maven.compiler.target>
  12. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  13. </properties>
  14. <dependencies>
  15. <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
  16. <dependency>
  17. <groupId>com.fasterxml.jackson.core</groupId>
  18. <artifactId>jackson-databind</artifactId>
  19. <version>2.16.1</version>
  20. </dependency>
  21. </dependencies>
  22. <build>
  23. <plugins>
  24. <plugin>
  25. <groupId>org.jsonschema2pojo</groupId>
  26. <artifactId>jsonschema2pojo-maven-plugin</artifactId>
  27. <version>1.2.1</version>
  28. <configuration>
  29. <sourceDirectory>${basedir}/src/main/resources/schema</sourceDirectory>
  30. <targetProject>${basedir}/src/main/java</targetProject>
  31. <targetPackage>com.example.types</targetPackage>
  32. </configuration>
  33. </plugin>
  34. </plugins>
  35. </build>
  36. </project>

 jsonschema2pojo为一个根据json结构生成java的pojo代码的工具,对于复杂json生成pojo十分省时间。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/351708
推荐阅读
相关标签
  

闽ICP备14008679号