@Test public void testGetSchemaFromStringNested() { String fields = "[{'name': 'MyName', 'type': 'STRING'}," + "{'name': 'MyNestedField', 'type': 'RECORD', 'mode': 'repeated', 'fields': [" + "{'name': 'field1', 'type': 'INTEGER'}, {'name': 'field2', 'type': 'STRING'}" + "]}]"; List<TableFieldSchema> list = BigQueryUtils.getSchemaFromString(fields); assertThat(list).hasSize(2); assertThat(list.get(0).getName()).isEqualTo("MyName"); assertThat(list.get(0).getType()).isEqualTo("STRING"); assertThat(list.get(1).getName()).isEqualTo("MyNestedField"); assertThat(list.get(1).getType()).isEqualTo("RECORD"); assertThat(list.get(1).getMode()).isEqualTo("repeated"); List<TableFieldSchema> nestedList = list.get(1).getFields(); assertThat(nestedList).isNotNull(); assertThat(nestedList).hasSize(2); assertThat(nestedList.get(0).getName()).isEqualTo("field1"); assertThat(nestedList.get(0).getType()).isEqualTo("INTEGER"); assertThat(nestedList.get(1).getName()).isEqualTo("field2"); assertThat(nestedList.get(1).getType()).isEqualTo("STRING"); }
public static List<TableFieldSchema> getSchemaFromString(String fields) { logger.atFine().log("getSchemaFromString('%s')", fields); JsonParser jsonParser = new JsonParser(); JsonArray json = jsonParser.parse(fields).getAsJsonArray(); List<TableFieldSchema> fieldsList = new ArrayList<>(); for (JsonElement jsonElement : json) { checkArgument( jsonElement.isJsonObject(), "Expected JsonObject for element, got '%s'.", jsonElement); JsonObject jsonObject = jsonElement.getAsJsonObject(); checkArgument( jsonObject.get("name") != null, "Expected non-null entry for key 'name' in JsonObject '%s'", jsonObject); checkArgument( jsonObject.get("type") != null, "Expected non-null entry for key 'type' in JsonObject '%s'", jsonObject); TableFieldSchema fieldDef = new TableFieldSchema(); fieldDef.setName(jsonObject.get("name").getAsString()); fieldDef.setType(jsonObject.get("type").getAsString()); if (jsonObject.get("mode") != null) { fieldDef.setMode(jsonObject.get("mode").getAsString()); } if (jsonObject.get("type").getAsString().equals("RECORD")) { checkArgument( jsonObject.get("fields") != null, "Expected non-null entry for key 'fields' in JsonObject of type RECORD: '%s'", jsonObject); fieldDef.setFields(getSchemaFromString(jsonObject.get("fields").toString())); } fieldsList.add(fieldDef); } return fieldsList; }
BigQueryUtils { public static List<TableFieldSchema> getSchemaFromString(String fields) { logger.atFine().log("getSchemaFromString('%s')", fields); JsonParser jsonParser = new JsonParser(); JsonArray json = jsonParser.parse(fields).getAsJsonArray(); List<TableFieldSchema> fieldsList = new ArrayList<>(); for (JsonElement jsonElement : json) { checkArgument( jsonElement.isJsonObject(), "Expected JsonObject for element, got '%s'.", jsonElement); JsonObject jsonObject = jsonElement.getAsJsonObject(); checkArgument( jsonObject.get("name") != null, "Expected non-null entry for key 'name' in JsonObject '%s'", jsonObject); checkArgument( jsonObject.get("type") != null, "Expected non-null entry for key 'type' in JsonObject '%s'", jsonObject); TableFieldSchema fieldDef = new TableFieldSchema(); fieldDef.setName(jsonObject.get("name").getAsString()); fieldDef.setType(jsonObject.get("type").getAsString()); if (jsonObject.get("mode") != null) { fieldDef.setMode(jsonObject.get("mode").getAsString()); } if (jsonObject.get("type").getAsString().equals("RECORD")) { checkArgument( jsonObject.get("fields") != null, "Expected non-null entry for key 'fields' in JsonObject of type RECORD: '%s'", jsonObject); fieldDef.setFields(getSchemaFromString(jsonObject.get("fields").toString())); } fieldsList.add(fieldDef); } return fieldsList; } }
BigQueryUtils { public static List<TableFieldSchema> getSchemaFromString(String fields) { logger.atFine().log("getSchemaFromString('%s')", fields); JsonParser jsonParser = new JsonParser(); JsonArray json = jsonParser.parse(fields).getAsJsonArray(); List<TableFieldSchema> fieldsList = new ArrayList<>(); for (JsonElement jsonElement : json) { checkArgument( jsonElement.isJsonObject(), "Expected JsonObject for element, got '%s'.", jsonElement); JsonObject jsonObject = jsonElement.getAsJsonObject(); checkArgument( jsonObject.get("name") != null, "Expected non-null entry for key 'name' in JsonObject '%s'", jsonObject); checkArgument( jsonObject.get("type") != null, "Expected non-null entry for key 'type' in JsonObject '%s'", jsonObject); TableFieldSchema fieldDef = new TableFieldSchema(); fieldDef.setName(jsonObject.get("name").getAsString()); fieldDef.setType(jsonObject.get("type").getAsString()); if (jsonObject.get("mode") != null) { fieldDef.setMode(jsonObject.get("mode").getAsString()); } if (jsonObject.get("type").getAsString().equals("RECORD")) { checkArgument( jsonObject.get("fields") != null, "Expected non-null entry for key 'fields' in JsonObject of type RECORD: '%s'", jsonObject); fieldDef.setFields(getSchemaFromString(jsonObject.get("fields").toString())); } fieldsList.add(fieldDef); } return fieldsList; }  }
BigQueryUtils { public static List<TableFieldSchema> getSchemaFromString(String fields) { logger.atFine().log("getSchemaFromString('%s')", fields); JsonParser jsonParser = new JsonParser(); JsonArray json = jsonParser.parse(fields).getAsJsonArray(); List<TableFieldSchema> fieldsList = new ArrayList<>(); for (JsonElement jsonElement : json) { checkArgument( jsonElement.isJsonObject(), "Expected JsonObject for element, got '%s'.", jsonElement); JsonObject jsonObject = jsonElement.getAsJsonObject(); checkArgument( jsonObject.get("name") != null, "Expected non-null entry for key 'name' in JsonObject '%s'", jsonObject); checkArgument( jsonObject.get("type") != null, "Expected non-null entry for key 'type' in JsonObject '%s'", jsonObject); TableFieldSchema fieldDef = new TableFieldSchema(); fieldDef.setName(jsonObject.get("name").getAsString()); fieldDef.setType(jsonObject.get("type").getAsString()); if (jsonObject.get("mode") != null) { fieldDef.setMode(jsonObject.get("mode").getAsString()); } if (jsonObject.get("type").getAsString().equals("RECORD")) { checkArgument( jsonObject.get("fields") != null, "Expected non-null entry for key 'fields' in JsonObject of type RECORD: '%s'", jsonObject); fieldDef.setFields(getSchemaFromString(jsonObject.get("fields").toString())); } fieldsList.add(fieldDef); } return fieldsList; }  static void waitForJobCompletion(
      Bigquery bigquery, String projectId, JobReference jobReference, Progressable progressable); static List<TableFieldSchema> getSchemaFromString(String fields); }
BigQueryUtils { public static List<TableFieldSchema> getSchemaFromString(String fields) { logger.atFine().log("getSchemaFromString('%s')", fields); JsonParser jsonParser = new JsonParser(); JsonArray json = jsonParser.parse(fields).getAsJsonArray(); List<TableFieldSchema> fieldsList = new ArrayList<>(); for (JsonElement jsonElement : json) { checkArgument( jsonElement.isJsonObject(), "Expected JsonObject for element, got '%s'.", jsonElement); JsonObject jsonObject = jsonElement.getAsJsonObject(); checkArgument( jsonObject.get("name") != null, "Expected non-null entry for key 'name' in JsonObject '%s'", jsonObject); checkArgument( jsonObject.get("type") != null, "Expected non-null entry for key 'type' in JsonObject '%s'", jsonObject); TableFieldSchema fieldDef = new TableFieldSchema(); fieldDef.setName(jsonObject.get("name").getAsString()); fieldDef.setType(jsonObject.get("type").getAsString()); if (jsonObject.get("mode") != null) { fieldDef.setMode(jsonObject.get("mode").getAsString()); } if (jsonObject.get("type").getAsString().equals("RECORD")) { checkArgument( jsonObject.get("fields") != null, "Expected non-null entry for key 'fields' in JsonObject of type RECORD: '%s'", jsonObject); fieldDef.setFields(getSchemaFromString(jsonObject.get("fields").toString())); } fieldsList.add(fieldDef); } return fieldsList; }  static void waitForJobCompletion(
      Bigquery bigquery, String projectId, JobReference jobReference, Progressable progressable); static List<TableFieldSchema> getSchemaFromString(String fields); static final int POLL_WAIT_INITIAL_MILLIS; static final int POLL_WAIT_INTERVAL_MAX_MILLIS; static final int POLL_WAIT_MAX_ELAPSED_MILLIS; }