@Override public String toString() { return getJSONSchema(); }
public static void setSchema(Job job, Schema schema) { job.getConfiguration().set(PinotOutputFormat.SCHEMA, schema.getJSONSchema()); }
@GET @Produces(MediaType.APPLICATION_JSON) @Path("/tables/{tableName}/schema") @ApiOperation(value = "Get table schema", notes = "Read table schema") @ApiResponses(value = {@ApiResponse(code = 200, message = "Success"), @ApiResponse(code = 404, message = "Table not found")}) public String getTableSchema( @ApiParam(value = "Table name (without type)", required = true) @PathParam("tableName") String tableName) { Schema schema = pinotHelixResourceManager.getTableSchema(tableName); if (schema != null) { return schema.getJSONSchema(); } throw new ControllerApplicationException(LOGGER, "Schema not found for table: " + tableName, Response.Status.NOT_FOUND); } }
@GET @Produces(MediaType.APPLICATION_JSON) @Path("/schemas/{schemaName}") @ApiOperation(value = "Get a schema", notes = "Gets a schema by name") @ApiResponses(value = {@ApiResponse(code = 200, message = "Success"), @ApiResponse(code = 404, message = "Schema not found"), @ApiResponse(code = 500, message = "Internal error")}) public String getSchema( @ApiParam(value = "Schema name", required = true) @PathParam("schemaName") String schemaName) { LOGGER.info("looking for schema {}", schemaName); Schema schema = _pinotHelixResourceManager.getSchema(schemaName); if (schema == null) { throw new ControllerApplicationException(LOGGER, "Schema not found", Response.Status.NOT_FOUND); } // We need to return schema.getJSONSchema(). Returning schema ends up with many extra fields, "jsonSchema" being one of them, // Others like fieldSpecMap, etc., serialzing the entire Schema object. return schema.getJSONSchema(); }
@POST @Produces(MediaType.APPLICATION_JSON) @Path("/schemas/validate") @ApiOperation(value = "Validate schema", notes = "This API returns the schema that matches the one you get " + "from 'GET /schema/{schemaName}'. This allows us to validate schema before apply.") @ApiResponses(value = {@ApiResponse(code = 200, message = "Successfully validated schema"), @ApiResponse(code = 400, message = "Missing or invalid request body"), @ApiResponse(code = 500, message = "Internal error")}) public String validateSchema(FormDataMultiPart multiPart) { Schema schema = getSchemaFromMultiPart(multiPart); if (!schema.validate(LOGGER)) { throw new ControllerApplicationException(LOGGER, "Invalid schema. Check controller logs", Response.Status.BAD_REQUEST); } return schema.getJSONSchema(); }
/** * Wrap {@link Schema} into a {@link ZNRecord}. */ public static ZNRecord toZNRecord(@Nonnull Schema schema) { ZNRecord record = new ZNRecord(schema.getSchemaName()); record.setSimpleField("schemaJSON", schema.getJSONSchema()); return record; }
@Test public void testSerializeDeserialize() throws Exception { URL resourceUrl = getClass().getClassLoader().getResource("schemaTest.schema"); Assert.assertNotNull(resourceUrl); Schema schema = Schema.fromFile(new File(resourceUrl.getFile())); Schema schemaToCompare = Schema.fromString(schema.getJSONSchema()); Assert.assertEquals(schemaToCompare, schema); Assert.assertEquals(schemaToCompare.hashCode(), schema.hashCode()); schemaToCompare = SchemaUtils.fromZNRecord(SchemaUtils.toZNRecord(schema)); Assert.assertEquals(schemaToCompare, schema); Assert.assertEquals(schemaToCompare.hashCode(), schema.hashCode()); // When setting new fields, schema string should be updated String JSONSchema = schemaToCompare.getJSONSchema(); schemaToCompare.setSchemaName("newSchema"); String JSONSchemaToCompare = schemaToCompare.getJSONSchema(); Assert.assertFalse(JSONSchema.equals(JSONSchemaToCompare)); }
protected void addDummySchema(String tableName) throws IOException { addSchema(createDummySchema(tableName).getJSONSchema()); }
@Test public void testBadContentType() { Schema schema = createDummySchema("testSchema"); try { sendPostRequest(_controllerRequestURLBuilder.forSchemaCreate(), schema.getJSONSchema()); } catch (IOException e) { // TODO The Jersey API returns 400, so we need to check return code here not a string. // Assert.assertTrue(e.getMessage().startsWith("Server returned HTTP response code: 415"), e.getMessage()); return; } // should not reach here Assert.fail("Should have caught an exception"); }
@Test public void testSimpleDateFormat() throws Exception { TimeGranularitySpec incomingTimeGranularitySpec = new TimeGranularitySpec(FieldSpec.DataType.STRING, 1, TimeUnit.DAYS, TimeFormat.SIMPLE_DATE_FORMAT + ":yyyyMMdd", "Date"); TimeGranularitySpec outgoingTimeGranularitySpec = new TimeGranularitySpec(FieldSpec.DataType.STRING, 1, TimeUnit.DAYS, TimeFormat.SIMPLE_DATE_FORMAT + ":yyyyMMdd", "Date"); Schema schema = new Schema.SchemaBuilder().setSchemaName("testSchema") .addTime(incomingTimeGranularitySpec, outgoingTimeGranularitySpec).build(); String jsonSchema = schema.getJSONSchema(); Schema schemaFromJson = Schema.fromString(jsonSchema); Assert.assertEquals(schemaFromJson, schema); Assert.assertEquals(schemaFromJson.hashCode(), schema.hashCode()); }
@Test public void testByteType() throws DecoderException, IOException { Schema expectedSchema = new Schema(); byte[] expectedEmptyDefault = new byte[0]; byte[] expectedNonEmptyDefault = Hex.decodeHex("abcd1234".toCharArray()); expectedSchema.setSchemaName("test"); expectedSchema.addField(new MetricFieldSpec("noDefault", FieldSpec.DataType.BYTES)); expectedSchema.addField(new MetricFieldSpec("emptyDefault", FieldSpec.DataType.BYTES, expectedEmptyDefault)); expectedSchema.addField(new MetricFieldSpec("nonEmptyDefault", FieldSpec.DataType.BYTES, expectedNonEmptyDefault)); // Ensure that schema can be serialized and de-serialized (ie byte[] converted to String and back). String jsonSchema = expectedSchema.getJSONSchema(); Schema actualSchema = Schema.fromString(jsonSchema); Assert.assertEquals(actualSchema.getFieldSpecFor("noDefault").getDefaultNullValue(), expectedEmptyDefault); Assert.assertEquals(actualSchema.getFieldSpecFor("emptyDefault").getDefaultNullValue(), expectedEmptyDefault); Assert.assertEquals(actualSchema.getFieldSpecFor("nonEmptyDefault").getDefaultNullValue(), expectedNonEmptyDefault); Assert.assertEquals(actualSchema, expectedSchema); Assert.assertEquals(actualSchema.hashCode(), expectedSchema.hashCode()); } }