private TableSchema loadSchemaFromJson()

in odps-sdk/odps-sdk-core/src/main/java/com/aliyun/odps/Table.java [1436:1645]


  private TableSchema loadSchemaFromJson(String json) {
    TableSchema s = new TableSchema();
    try {
      JsonObject tree = new JsonParser().parse(json).getAsJsonObject();

      if (tree.has("comment")) {
        model.comment = tree.get("comment").getAsString();
      }

      if (tree.has("owner")) {
        model.owner = tree.get("owner").getAsString();
      }

      if (tree.has("createTime")) {
        model.createdTime = new Date(tree.get("createTime").getAsLong() * 1000);
      }

      if (tree.has("lastModifiedTime")) {
        model.lastModifiedTime = new Date(tree.get("lastModifiedTime").getAsLong() * 1000);
      }

      if (tree.has("lastDDLTime")) {
        model.lastMetaModifiedTime = new Date(tree.get("lastDDLTime").getAsLong() * 1000);
      }

      if (tree.has("lastAccessTime")) {
        long timestamp = tree.get("lastAccessTime").getAsLong() * 1000;
        model.lastAccessTime = timestamp == 0 ? null : new Date(timestamp);
      }

      if (tree.has("isVirtualView")) {
        model.isVirtualView = tree.get("isVirtualView").getAsBoolean();
      }

      if (tree.has("isMaterializedView") && tree.get("isMaterializedView").getAsBoolean()) {
        model.type = TableType.MATERIALIZED_VIEW;
      }

      if (tree.has("isMaterializedViewRewriteEnabled")) {
        model.isMaterializedViewRewriteEnabled =
            tree.get("isMaterializedViewRewriteEnabled").getAsBoolean();
      }

      if (tree.has("IsMaterializedViewOutdated")) {
        model.isMaterializedViewOutdated = tree.get("IsMaterializedViewOutdated").getAsBoolean();
      }

      if (tree.has("isExternal")) {
        model.isExternalTable = tree.get("isExternal").getAsBoolean();
      }

      if (tree.has("isObjectTable")) {
        model.isObjectTable = tree.get("isObjectTable").getAsBoolean();
      }

      if (tree.has("lifecycle")) {
        model.life = tree.get("lifecycle").getAsLong();
      }

      if (tree.has("hubLifecycle")) {
        model.hubLifecycle = tree.get("hubLifecycle").getAsLong();
      }

      if (tree.has("viewText")) {
        model.viewText = tree.get("viewText").getAsString();
      }

      if (tree.has("viewExpandedText")) {
        model.viewExpandedText = tree.get("viewExpandedText").getAsString();
      }

      if (tree.has("size")) {
        model.size = tree.get("size").getAsLong();
      }

      if (tree.has("IsArchived")) {
        model.isArchived = tree.get("IsArchived").getAsBoolean();
      }

      if (tree.has("PhysicalSize")) {
        model.physicalSize = tree.get("PhysicalSize").getAsLong();
      }

      if (tree.has("FileNum")) {
        model.fileNum = tree.get("FileNum").getAsLong();
      }

      if (tree.has("recordNum")) {
        model.recordNum = tree.get("recordNum").getAsLong();
      }

      if (tree.has("storageHandler")) {
        model.storageHandler = tree.get("storageHandler").getAsString();
      }

      if (tree.has("location")) {
        model.location = tree.get("location").getAsString();
      }

      if (tree.has("resources")) {
        model.resources = tree.get("resources").getAsString();
      }

      if (tree.has("serDeProperties")) {
        model.serDeProperties = new GsonBuilder().disableHtmlEscaping().create()
            .fromJson(tree.get("serDeProperties").getAsString(),
                      new TypeToken<Map<String, String>>() {
                      }.getType());
      }

      if (tree.has("shardExist")) {
        boolean shardExist = tree.get("shardExist").getAsBoolean();
        if (shardExist && tree.has("shardInfo")) {
          model.shard = Shard.parseShard(tree.get("shardInfo").getAsJsonObject());
        } else {
          model.shard = null;
        }
      }

      if (tree.has("tableLabel")) {
        model.tableLabel = tree.get("tableLabel").getAsString();
        // Service will return 0 if nothing set
        if (model.tableLabel.equals("0")) {
          model.tableLabel = "";
        }
      }

      if (tree.has("columns") && tree.get("columns") != null) {
        JsonArray columnsNode = tree.get("columns").getAsJsonArray();
        for (int i = 0; i < columnsNode.size(); ++i) {
          JsonObject n = columnsNode.get(i).getAsJsonObject();
          s.addColumn(ColumnUtils.fromJson(n.toString()));
        }
      }

      if (tree.has("extendedLabel")) {
        JsonArray tableExtendedLabels = tree.get("extendedLabel").getAsJsonArray();
        if (tableExtendedLabels.size() != 0) {
          List<String> labelList = new LinkedList<String>();
          for (JsonElement label : tableExtendedLabels) {
            labelList.add(label.getAsString());
          }
          model.tableExtendedLabels = labelList;
        }
      }

      if (tree.has("partitionKeys") && tree.get("partitionKeys") != null) {
        JsonArray columnsNode = tree.get("partitionKeys").getAsJsonArray();
        for (int i = 0; i < columnsNode.size(); ++i) {
          JsonObject n = columnsNode.get(i).getAsJsonObject();
          s.addPartitionColumn(ColumnUtils.fromJson(n.toString()));
        }
      }

      if (tree.has("Reserved")) {
        model.reserved = tree.get("Reserved").getAsString();
        loadReservedJson(model.reserved);
      }

      if (tree.has("props") && tree.get("props") != null) {
        JsonObject props = tree.get("props").getAsJsonObject();
        model.mvProperties = new HashMap<>();
        model.mvProperties.put("enable_auto_refresh",
                               props.has("enable_auto_refresh") ? props.get("enable_auto_refresh")
                                   .getAsString() : "false");

        if (props.has("refresh_interval_minutes")) {
          model.mvProperties.put("refresh_interval_minutes",
                                 props.get("refresh_interval_minutes").getAsString());
        }

        if (props.has("refresh_cron")) {
          model.mvProperties.put("refresh_cron", props.get("refresh_cron").getAsString());
        }

        if (props.has("enable_auto_substitute")) {
          model.mvProperties.put("enable_auto_substitute",
                                 props.get("enable_auto_substitute").getAsString());
        }
      }

      if (tree.has("RefreshHistory")) {
        String refreshHistoryStr = tree.get("RefreshHistory").getAsString();
        JsonArray refreshHistoryList = new JsonParser().parse(refreshHistoryStr).getAsJsonArray();
        model.refreshHistory = new LinkedList<>();
        for (int i = 0; i < refreshHistoryList.size(); i++) {

          JsonObject info = refreshHistoryList.get(i).getAsJsonObject();
          Map<String, String> infoMap = new HashMap<>();
          infoMap.put("InstanceId",
                      info.has("InstanceId") ? info.get("InstanceId").getAsString() : null);
          infoMap.put("Status", info.has("Status") ? info.get("Status").getAsString() : null);
          infoMap.put("StartTime",
                      info.has("StartTime") ? info.get("StartTime").getAsString() : null);
          infoMap.put("EndTime", info.has("EndTime") ? info.get("EndTime").getAsString() : null);

          model.refreshHistory.add(infoMap);

          if (model.refreshHistory.size() >= 10) {
            break;
          }
        }
      }

    } catch (Exception e) {
      throw new RuntimeException(e.getMessage(), e);
    }

    return s;
  }