Search in sources :

Example 11 with ResourceFieldSchema

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)));
}
Also used : ResourceFieldSchema(org.apache.pig.ResourceSchema.ResourceFieldSchema) PhoenixRecordWritable(org.apache.phoenix.mapreduce.PhoenixRecordWritable) PhoenixArray(org.apache.phoenix.schema.types.PhoenixArray) BigInteger(java.math.BigInteger) PDouble(org.apache.phoenix.schema.types.PDouble) Tuple(org.apache.pig.data.Tuple) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 12 with ResourceFieldSchema

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;
}
Also used : PhoenixArray(org.apache.phoenix.schema.types.PhoenixArray) PUnsignedTimestamp(org.apache.phoenix.schema.types.PUnsignedTimestamp) Timestamp(java.sql.Timestamp) PTimestamp(org.apache.phoenix.schema.types.PTimestamp) PUnsignedSmallint(org.apache.phoenix.schema.types.PUnsignedSmallint) PUnsignedTinyint(org.apache.phoenix.schema.types.PUnsignedTinyint) PTinyint(org.apache.phoenix.schema.types.PTinyint) PSmallint(org.apache.phoenix.schema.types.PSmallint) DateTime(org.joda.time.DateTime) PigException(org.apache.pig.PigException) SQLException(java.sql.SQLException) IOException(java.io.IOException) ResourceFieldSchema(org.apache.pig.ResourceSchema.ResourceFieldSchema) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) DataByteArray(org.apache.pig.data.DataByteArray) Tuple(org.apache.pig.data.Tuple) PigException(org.apache.pig.PigException)

Example 13 with ResourceFieldSchema

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());
}
Also used : ResourceSchema(org.apache.pig.ResourceSchema) Configuration(org.apache.hadoop.conf.Configuration) SQLException(java.sql.SQLException) ResourceFieldSchema(org.apache.pig.ResourceSchema.ResourceFieldSchema) List(java.util.List) Dependencies(org.apache.phoenix.pig.util.PhoenixPigSchemaUtil.Dependencies) Test(org.junit.Test)

Example 14 with ResourceFieldSchema

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);
    }
}
Also used : PDataType(org.apache.phoenix.schema.types.PDataType) ResourceFieldSchema(org.apache.pig.ResourceSchema.ResourceFieldSchema) SQLException(java.sql.SQLException) PhoenixRecordWritable(org.apache.phoenix.mapreduce.PhoenixRecordWritable) ColumnInfo(org.apache.phoenix.util.ColumnInfo) IOException(java.io.IOException)

Example 15 with ResourceFieldSchema

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;
}
Also used : ResourceSchema(org.apache.pig.ResourceSchema) ResourceFieldSchema(org.apache.pig.ResourceSchema.ResourceFieldSchema) ArrayList(java.util.ArrayList) HCatFieldSchema(org.apache.hive.hcatalog.data.schema.HCatFieldSchema)

Aggregations

ResourceFieldSchema (org.apache.pig.ResourceSchema.ResourceFieldSchema)20 ResourceSchema (org.apache.pig.ResourceSchema)11 IOException (java.io.IOException)10 Map (java.util.Map)6 Tuple (org.apache.pig.data.Tuple)6 ArrayList (java.util.ArrayList)5 HCatFieldSchema (org.apache.hive.hcatalog.data.schema.HCatFieldSchema)5 BasicDBObjectBuilder (com.mongodb.BasicDBObjectBuilder)4 SQLException (java.sql.SQLException)4 BasicBSONObject (org.bson.BasicBSONObject)4 Test (org.junit.Test)4 HashMap (java.util.HashMap)3 List (java.util.List)3 BSONObject (org.bson.BSONObject)3 DateTime (org.joda.time.DateTime)3 LinkedHashMap (java.util.LinkedHashMap)2 HCatSchema (org.apache.hive.hcatalog.data.schema.HCatSchema)2 PhoenixRecordWritable (org.apache.phoenix.mapreduce.PhoenixRecordWritable)2 PDataType (org.apache.phoenix.schema.types.PDataType)2 PhoenixArray (org.apache.phoenix.schema.types.PhoenixArray)2