use of org.apache.pig.ResourceSchema.ResourceFieldSchema in project phoenix by apache.
the class TypeUtilTest method testTransformToTuple.
@Test
public void testTransformToTuple() throws Exception {
PhoenixRecordWritable record = mock(PhoenixRecordWritable.class);
Double[] doubleArr = new Double[2];
doubleArr[0] = 64.87;
doubleArr[1] = 89.96;
PhoenixArray arr = PArrayDataType.instantiatePhoenixArray(PDouble.INSTANCE, doubleArr);
Map<String, Object> values = Maps.newLinkedHashMap();
values.put("first", "213123");
values.put("second", 1231123);
values.put("third", 31231231232131L);
values.put("four", "bytearray".getBytes());
values.put("five", arr);
when(record.getResultMap()).thenReturn(values);
ResourceFieldSchema field = new ResourceFieldSchema().setType(DataType.CHARARRAY);
ResourceFieldSchema field1 = new ResourceFieldSchema().setType(DataType.INTEGER);
ResourceFieldSchema field2 = new ResourceFieldSchema().setType(DataType.LONG);
ResourceFieldSchema field3 = new ResourceFieldSchema().setType(DataType.BYTEARRAY);
ResourceFieldSchema field4 = new ResourceFieldSchema().setType(DataType.TUPLE);
ResourceFieldSchema[] projectedColumns = { field, field1, field2, field3, field4 };
Tuple t = TypeUtil.transformToTuple(record, projectedColumns);
assertEquals(DataType.LONG, DataType.findType(t.get(2)));
assertEquals(DataType.TUPLE, DataType.findType(t.get(4)));
Tuple doubleArrayTuple = (Tuple) t.get(4);
assertEquals(2, doubleArrayTuple.size());
field = new ResourceFieldSchema().setType(DataType.BIGDECIMAL);
field1 = new ResourceFieldSchema().setType(DataType.BIGINTEGER);
values.clear();
values.put("first", new BigDecimal(123123123.123213));
values.put("second", new BigInteger("1312313231312"));
ResourceFieldSchema[] columns = { field, field1 };
t = TypeUtil.transformToTuple(record, columns);
assertEquals(DataType.BIGDECIMAL, DataType.findType(t.get(0)));
assertEquals(DataType.BIGINTEGER, DataType.findType(t.get(1)));
}
use of org.apache.pig.ResourceSchema.ResourceFieldSchema in project phoenix by apache.
the class TypeUtil method transformToTuple.
/**
* Transforms the PhoenixRecord to Pig {@link Tuple}.
*
* @param record
* @param projectedColumns
* @return
* @throws IOException
*/
public static Tuple transformToTuple(final PhoenixRecordWritable record, final ResourceFieldSchema[] projectedColumns) throws IOException {
Map<String, Object> columnValues = record.getResultMap();
if (columnValues == null || columnValues.size() == 0 || projectedColumns == null || projectedColumns.length != columnValues.size()) {
return null;
}
int numColumns = columnValues.size();
Tuple tuple = TUPLE_FACTORY.newTuple(numColumns);
try {
int i = 0;
for (Map.Entry<String, Object> entry : columnValues.entrySet()) {
final ResourceFieldSchema fieldSchema = projectedColumns[i];
Object object = entry.getValue();
if (object == null) {
tuple.set(i++, null);
continue;
}
switch(fieldSchema.getType()) {
case DataType.BYTEARRAY:
byte[] bytes = PDataType.fromTypeId(PBinary.INSTANCE.getSqlType()).toBytes(object);
tuple.set(i, new DataByteArray(bytes, 0, bytes.length));
break;
case DataType.CHARARRAY:
tuple.set(i, DataType.toString(object));
break;
case DataType.DOUBLE:
tuple.set(i, DataType.toDouble(object));
break;
case DataType.FLOAT:
tuple.set(i, DataType.toFloat(object));
break;
case DataType.INTEGER:
tuple.set(i, DataType.toInteger(object));
break;
case DataType.LONG:
tuple.set(i, DataType.toLong(object));
break;
case DataType.BOOLEAN:
tuple.set(i, DataType.toBoolean(object));
break;
case DataType.DATETIME:
if (object instanceof java.sql.Timestamp)
tuple.set(i, new DateTime(((java.sql.Timestamp) object).getTime()));
else
tuple.set(i, new DateTime(object));
break;
case DataType.BIGDECIMAL:
tuple.set(i, DataType.toBigDecimal(object));
break;
case DataType.BIGINTEGER:
tuple.set(i, DataType.toBigInteger(object));
break;
case DataType.TUPLE:
{
PhoenixArray array = (PhoenixArray) object;
Tuple t = TUPLE_FACTORY.newTuple(array.getDimensions());
;
for (int j = 0; j < array.getDimensions(); j++) {
t.set(j, array.getElement(j));
}
tuple.set(i, t);
break;
}
default:
throw new RuntimeException(String.format(" Not supported [%s] pig type", fieldSchema));
}
i++;
}
} catch (Exception ex) {
final String errorMsg = String.format(" Error transforming PhoenixRecord to Tuple [%s] ", ex.getMessage());
LOG.error(errorMsg);
throw new PigException(errorMsg);
}
return tuple;
}
use of org.apache.pig.ResourceSchema.ResourceFieldSchema in project phoenix by apache.
the class PhoenixPigSchemaUtilTest method testSchema.
@Test
public void testSchema() throws SQLException, IOException {
final Configuration configuration = mock(Configuration.class);
when(configuration.get(PhoenixConfigurationUtil.SCHEMA_TYPE)).thenReturn(SchemaType.TABLE.name());
final ResourceSchema actual = PhoenixPigSchemaUtil.getResourceSchema(configuration, new Dependencies() {
List<ColumnInfo> getSelectColumnMetadataList(Configuration configuration) throws SQLException {
return Lists.newArrayList(ID_COLUMN, NAME_COLUMN);
}
});
// expected schema.
final ResourceFieldSchema[] fields = new ResourceFieldSchema[2];
fields[0] = new ResourceFieldSchema().setName("ID").setType(DataType.LONG);
fields[1] = new ResourceFieldSchema().setName("NAME").setType(DataType.CHARARRAY);
final ResourceSchema expected = new ResourceSchema().setFields(fields);
assertEquals(expected.toString(), actual.toString());
}
use of org.apache.pig.ResourceSchema.ResourceFieldSchema in project phoenix by apache.
the class PhoenixHBaseStorage method putNext.
@Override
public void putNext(Tuple t) throws IOException {
ResourceFieldSchema[] fieldSchemas = (schema == null) ? null : schema.getFields();
PhoenixRecordWritable record = new PhoenixRecordWritable(this.columnInfo);
try {
for (int i = 0; i < t.size(); i++) {
Object value = t.get(i);
if (value == null) {
record.add(null);
continue;
}
ColumnInfo cinfo = this.columnInfo.get(i);
byte type = (fieldSchemas == null) ? DataType.findType(value) : fieldSchemas[i].getType();
PDataType pDataType = PDataType.fromTypeId(cinfo.getSqlType());
Object v = TypeUtil.castPigTypeToPhoenix(value, type, pDataType);
record.add(v);
}
this.writer.write(null, record);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException(e);
} catch (SQLException e) {
LOG.error("Error on tuple {} .", t);
throw new IOException(e);
}
}
use of org.apache.pig.ResourceSchema.ResourceFieldSchema in project hive by apache.
the class PigHCatUtil method getResourceSchema.
public static ResourceSchema getResourceSchema(HCatSchema hcatSchema) throws IOException {
List<ResourceFieldSchema> rfSchemaList = new ArrayList<ResourceFieldSchema>();
for (HCatFieldSchema hfs : hcatSchema.getFields()) {
ResourceFieldSchema rfSchema;
rfSchema = getResourceSchemaFromFieldSchema(hfs);
rfSchemaList.add(rfSchema);
}
ResourceSchema rSchema = new ResourceSchema();
rSchema.setFields(rfSchemaList.toArray(new ResourceFieldSchema[rfSchemaList.size()]));
return rSchema;
}
Aggregations