@Test public void testGetSchemaFromStringWithTopLevelNonJsonObject() { String fields = "[{'name': 'MyName', 'type': 'STRING'}," + "foo," + "{'name': 'Number', 'type': 'INTEGER', 'mode': 'sample'}]"; assertThrows(IllegalArgumentException.class, () -> BigQueryUtils.getSchemaFromString(fields)); }
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; }