@Test public void testConvertToJavaObject() throws Exception { Object result; context = new RequestContext(); context.setConfig("default"); context.setUser("test-user"); tupleDesc = mock(HBaseTupleDescription.class); PowerMockito.whenNew(HBaseTupleDescription.class).withArguments(context).thenReturn(tupleDesc); HBaseResolver resolver = new HBaseResolver(); resolver.initialize(context); result = resolver.convertToJavaObject(20, "bigint", null); assertNull(result); result = resolver.convertToJavaObject(20, "bigint", "1234".getBytes()); assertEquals(((Long) result).longValue(), 1234L); try { result = resolver.convertToJavaObject(20, "bigint", "not_a_numeral".getBytes()); fail("Supported type, Invalid value should throw an exception"); } catch (BadRecordException e) { assertEquals("Error converting value 'not_a_numeral' to type bigint. (original error: For input string: \"not_a_numeral\")", e.getMessage()); } catch (Exception e) { fail("Supported type, Invalid value expected to catch a BadRecordException, caught Exception"); } try { result = resolver.convertToJavaObject(600, "point", "[1,1]".getBytes()); fail("Unsupported data type should throw exception"); } catch (Exception e) { assertEquals("Unsupported data type point", e.getMessage()); } }
Object convertToJavaObject(int typeCode, String typeName, byte[] val) throws Exception { if (val == null) { return null; } try { switch (DataType.get(typeCode)) { case TEXT: case VARCHAR: case BPCHAR: case NUMERIC: return Bytes.toString(val); case INTEGER: return Integer.parseInt(Bytes.toString(val)); case BIGINT: return Long.parseLong(Bytes.toString(val)); case SMALLINT: return Short.parseShort(Bytes.toString(val)); case REAL: return Float.parseFloat(Bytes.toString(val)); case FLOAT8: return Double.parseDouble(Bytes.toString(val)); case BYTEA: return val; case BOOLEAN: return Boolean.valueOf(Bytes.toString(val)); case TIMESTAMP: return Timestamp.valueOf(Bytes.toString(val)); default: throw new UnsupportedTypeException("Unsupported data type " + typeName); } } catch (NumberFormatException e) { throw new BadRecordException("Error converting value '" + Bytes.toString(val) + "' " + "to type " + typeName + ". " + "(original error: " + e.getMessage() + ")"); } }
HBaseResolver extends BasePlugin implements Resolver { Object convertToJavaObject(int typeCode, String typeName, byte[] val) throws Exception { if (val == null) { return null; } try { switch (DataType.get(typeCode)) { case TEXT: case VARCHAR: case BPCHAR: case NUMERIC: return Bytes.toString(val); case INTEGER: return Integer.parseInt(Bytes.toString(val)); case BIGINT: return Long.parseLong(Bytes.toString(val)); case SMALLINT: return Short.parseShort(Bytes.toString(val)); case REAL: return Float.parseFloat(Bytes.toString(val)); case FLOAT8: return Double.parseDouble(Bytes.toString(val)); case BYTEA: return val; case BOOLEAN: return Boolean.valueOf(Bytes.toString(val)); case TIMESTAMP: return Timestamp.valueOf(Bytes.toString(val)); default: throw new UnsupportedTypeException("Unsupported data type " + typeName); } } catch (NumberFormatException e) { throw new BadRecordException("Error converting value '" + Bytes.toString(val) + "' " + "to type " + typeName + ". " + "(original error: " + e.getMessage() + ")"); } } }
HBaseResolver extends BasePlugin implements Resolver { Object convertToJavaObject(int typeCode, String typeName, byte[] val) throws Exception { if (val == null) { return null; } try { switch (DataType.get(typeCode)) { case TEXT: case VARCHAR: case BPCHAR: case NUMERIC: return Bytes.toString(val); case INTEGER: return Integer.parseInt(Bytes.toString(val)); case BIGINT: return Long.parseLong(Bytes.toString(val)); case SMALLINT: return Short.parseShort(Bytes.toString(val)); case REAL: return Float.parseFloat(Bytes.toString(val)); case FLOAT8: return Double.parseDouble(Bytes.toString(val)); case BYTEA: return val; case BOOLEAN: return Boolean.valueOf(Bytes.toString(val)); case TIMESTAMP: return Timestamp.valueOf(Bytes.toString(val)); default: throw new UnsupportedTypeException("Unsupported data type " + typeName); } } catch (NumberFormatException e) { throw new BadRecordException("Error converting value '" + Bytes.toString(val) + "' " + "to type " + typeName + ". " + "(original error: " + e.getMessage() + ")"); } }  }
HBaseResolver extends BasePlugin implements Resolver { Object convertToJavaObject(int typeCode, String typeName, byte[] val) throws Exception { if (val == null) { return null; } try { switch (DataType.get(typeCode)) { case TEXT: case VARCHAR: case BPCHAR: case NUMERIC: return Bytes.toString(val); case INTEGER: return Integer.parseInt(Bytes.toString(val)); case BIGINT: return Long.parseLong(Bytes.toString(val)); case SMALLINT: return Short.parseShort(Bytes.toString(val)); case REAL: return Float.parseFloat(Bytes.toString(val)); case FLOAT8: return Double.parseDouble(Bytes.toString(val)); case BYTEA: return val; case BOOLEAN: return Boolean.valueOf(Bytes.toString(val)); case TIMESTAMP: return Timestamp.valueOf(Bytes.toString(val)); default: throw new UnsupportedTypeException("Unsupported data type " + typeName); } } catch (NumberFormatException e) { throw new BadRecordException("Error converting value '" + Bytes.toString(val) + "' " + "to type " + typeName + ". " + "(original error: " + e.getMessage() + ")"); } }  @Override void initialize(RequestContext requestContext); @Override List<OneField> getFields(OneRow onerow); @Override OneRow setFields(List<OneField> record); }
HBaseResolver extends BasePlugin implements Resolver { Object convertToJavaObject(int typeCode, String typeName, byte[] val) throws Exception { if (val == null) { return null; } try { switch (DataType.get(typeCode)) { case TEXT: case VARCHAR: case BPCHAR: case NUMERIC: return Bytes.toString(val); case INTEGER: return Integer.parseInt(Bytes.toString(val)); case BIGINT: return Long.parseLong(Bytes.toString(val)); case SMALLINT: return Short.parseShort(Bytes.toString(val)); case REAL: return Float.parseFloat(Bytes.toString(val)); case FLOAT8: return Double.parseDouble(Bytes.toString(val)); case BYTEA: return val; case BOOLEAN: return Boolean.valueOf(Bytes.toString(val)); case TIMESTAMP: return Timestamp.valueOf(Bytes.toString(val)); default: throw new UnsupportedTypeException("Unsupported data type " + typeName); } } catch (NumberFormatException e) { throw new BadRecordException("Error converting value '" + Bytes.toString(val) + "' " + "to type " + typeName + ". " + "(original error: " + e.getMessage() + ")"); } }  @Override void initialize(RequestContext requestContext); @Override List<OneField> getFields(OneRow onerow); @Override OneRow setFields(List<OneField> record);  }