@Override public Schema getWrappedSchema(@NotEmpty final String schemaName) { return getSchema(schemaName); }
/** * It returns a writer with latest schema * @param schemaName Fully qualified schema name * @return An instance of {@link HDFSSchemaServiceWriter} */ public HDFSSchemaServiceWriter getWriter(@NotEmpty final String schemaName) { return new HDFSSchemaServiceWriter(getSchema(schemaName)); }
/** * It returns a reader with latest schema * @param schemaName Fully qualified schema name * @return An instance of {@link HDFSSchemaServiceReader} */ public HDFSSchemaServiceReader getReader(@NotEmpty final String schemaName) { return new HDFSSchemaServiceReader(getSchema(schemaName)); }
@Test(expected = JobRuntimeException.class) public void testSchemaNotFound() throws Exception { final HDFSSchemaService ss = getHdfsSchemaService(); ss.getSchema("schemaDNE"); }
@Test public void testSchema() throws Exception { final HDFSSchemaService ss = getHdfsSchemaService(); final Schema schema1 = ss.getSchema(SCHEMA_NAME, 1); final GenericRecord data1 = new GenericRecordBuilder(schema1).set("firstName", "Eric").set("lastName", "Sayle").build(); final byte[] bytes1 = ss.getWriter(SCHEMA_NAME, 1).write(data1); final GenericRecord output1 = ss.getReader(SCHEMA_NAME, 1).read(bytes1); Assert.assertEquals(output1.get("firstName").toString(), "Eric"); Assert.assertEquals(output1.get("lastName").toString(), "Sayle"); final Schema schema2 = ss.getSchema(SCHEMA_NAME); final GenericRecord data2 = new GenericRecordBuilder(schema2).set("firstName", "Eason").set("lastName", "Lu").set("middleName", "Fitzgerald").build(); final byte[] bytes2 = ss.getWriter(SCHEMA_NAME).write(data2); final GenericRecord output2 = ss.getReader(SCHEMA_NAME).read(bytes2); Assert.assertEquals(output2.get("firstName").toString(), "Eason"); Assert.assertEquals(output2.get("lastName").toString(), "Lu"); Assert.assertEquals(output2.get("middleName").toString(), "Fitzgerald"); }
@Test(expected = InvalidDataException.class) public void testInvalidGR() throws Exception { final HDFSSchemaService ss = getHdfsSchemaService(); final Schema wrongSchema = ss.getSchema("wrongSchema"); final GenericRecord data = new GenericRecordBuilder(wrongSchema).set("foo", "boo").build(); ss.getWriter(SCHEMA_NAME, 1).write(data); }
@Test public void testInvalidBytes() throws Exception { final HDFSSchemaService ss = getHdfsSchemaService(); final Schema wrongSchema = ss.getSchema("wrongSchema"); final GenericRecord data = new GenericRecordBuilder(wrongSchema).set("foo", "boo").build(); final byte[] bytes = ss.getWriter("wrongSchema", 1).write(data); try { ss.getReader(SCHEMA_NAME, 1).read(bytes); Assert.fail("Didn't throw error trying to read data"); } catch (InvalidDataException e) { // pass } }