Search in sources :

Example 31 with HCatFieldSchema

use of org.apache.hive.hcatalog.data.schema.HCatFieldSchema in project hive by apache.

the class HCatBaseStorer method doSchemaValidations.

protected void doSchemaValidations(Schema pigSchema, HCatSchema tblSchema) throws FrontendException, HCatException {
    // Iterate through all the elements in Pig Schema and do validations as
    // dictated by semantics, consult HCatSchema of table when need be.
    // helps with debug messages
    int columnPos = 0;
    for (FieldSchema pigField : pigSchema.getFields()) {
        HCatFieldSchema hcatField = getColFromSchema(pigField.alias, tblSchema);
        validateSchema(pigField, hcatField, pigSchema, tblSchema, columnPos++);
    }
    try {
        PigHCatUtil.validateHCatTableSchemaFollowsPigRules(tblSchema);
    } catch (IOException e) {
        throw new FrontendException("HCatalog schema is not compatible with Pig: " + e.getMessage(), PigHCatUtil.PIG_EXCEPTION_CODE, e);
    }
}
Also used : HCatFieldSchema(org.apache.hive.hcatalog.data.schema.HCatFieldSchema) FieldSchema(org.apache.pig.impl.logicalLayer.schema.Schema.FieldSchema) IOException(java.io.IOException) FrontendException(org.apache.pig.impl.logicalLayer.FrontendException) HCatFieldSchema(org.apache.hive.hcatalog.data.schema.HCatFieldSchema)

Example 32 with HCatFieldSchema

use of org.apache.hive.hcatalog.data.schema.HCatFieldSchema in project hive by apache.

the class InternalUtil method createStructObjectInspector.

static StructObjectInspector createStructObjectInspector(HCatSchema outputSchema) throws IOException {
    if (outputSchema == null) {
        throw new IOException("Invalid output schema specified");
    }
    List<ObjectInspector> fieldInspectors = new ArrayList<ObjectInspector>();
    List<String> fieldNames = new ArrayList<String>();
    for (HCatFieldSchema hcatFieldSchema : outputSchema.getFields()) {
        TypeInfo type = TypeInfoUtils.getTypeInfoFromTypeString(hcatFieldSchema.getTypeString());
        fieldNames.add(hcatFieldSchema.getName());
        fieldInspectors.add(getObjectInspector(type));
    }
    StructObjectInspector structInspector = ObjectInspectorFactory.getStandardStructObjectInspector(fieldNames, fieldInspectors);
    return structInspector;
}
Also used : ListObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ListObjectInspector) ObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector) MapObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.MapObjectInspector) StructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector) ArrayList(java.util.ArrayList) IOException(java.io.IOException) MapTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.MapTypeInfo) ListTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.ListTypeInfo) StructTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.StructTypeInfo) PrimitiveTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.PrimitiveTypeInfo) TypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfo) HCatFieldSchema(org.apache.hive.hcatalog.data.schema.HCatFieldSchema) StructObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.StructObjectInspector)

Example 33 with HCatFieldSchema

use of org.apache.hive.hcatalog.data.schema.HCatFieldSchema in project hive by apache.

the class TestSequenceFileReadWrite method getSchema.

private HCatSchema getSchema() throws HCatException {
    HCatSchema schema = new HCatSchema(new ArrayList<HCatFieldSchema>());
    schema.append(new HCatFieldSchema("a0", HCatFieldSchema.Type.INT, ""));
    schema.append(new HCatFieldSchema("a1", HCatFieldSchema.Type.STRING, ""));
    schema.append(new HCatFieldSchema("a2", HCatFieldSchema.Type.STRING, ""));
    return schema;
}
Also used : HCatSchema(org.apache.hive.hcatalog.data.schema.HCatSchema) HCatFieldSchema(org.apache.hive.hcatalog.data.schema.HCatFieldSchema)

Example 34 with HCatFieldSchema

use of org.apache.hive.hcatalog.data.schema.HCatFieldSchema in project hive by apache.

the class HCatLoader method getHCatComparisonString.

private String getHCatComparisonString(Expression expr) {
    if (expr instanceof BinaryExpression) {
        // call getHCatComparisonString on lhs and rhs, and and join the
        // results with OpType string
        // we can just use OpType.toString() on all Expression types except
        // Equal, NotEqualt since Equal has '==' in toString() and
        // we need '='
        String opStr = null;
        switch(expr.getOpType()) {
            case OP_EQ:
                opStr = " = ";
                break;
            default:
                opStr = expr.getOpType().toString();
        }
        BinaryExpression be = (BinaryExpression) expr;
        if (be.getRhs() instanceof Const) {
            // If the expr is column op const, will try to cast the const to string
            // according to the data type of the column
            UDFContext udfContext = UDFContext.getUDFContext();
            Properties udfProps = udfContext.getUDFProperties(this.getClass(), new String[] { signature });
            HCatSchema hcatTableSchema = (HCatSchema) udfProps.get(HCatConstants.HCAT_TABLE_SCHEMA);
            HCatFieldSchema fs = null;
            try {
                fs = hcatTableSchema.get(be.getLhs().toString());
            } catch (HCatException e) {
            // Shall never happen
            }
            if (fs != null) {
                return "(" + getHCatComparisonString(be.getLhs()) + opStr + getHCatConstString((Const) be.getRhs(), fs.getType()) + ")";
            }
        }
        return "(" + getHCatComparisonString(be.getLhs()) + opStr + getHCatComparisonString(be.getRhs()) + ")";
    } else {
        // should be a constant or column
        return expr.toString();
    }
}
Also used : BinaryExpression(org.apache.pig.Expression.BinaryExpression) HCatSchema(org.apache.hive.hcatalog.data.schema.HCatSchema) Const(org.apache.pig.Expression.Const) HCatException(org.apache.hive.hcatalog.common.HCatException) UDFContext(org.apache.pig.impl.util.UDFContext) Properties(java.util.Properties) HCatFieldSchema(org.apache.hive.hcatalog.data.schema.HCatFieldSchema)

Example 35 with HCatFieldSchema

use of org.apache.hive.hcatalog.data.schema.HCatFieldSchema 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

HCatFieldSchema (org.apache.hive.hcatalog.data.schema.HCatFieldSchema)61 ArrayList (java.util.ArrayList)34 Test (org.junit.Test)30 HCatException (org.apache.hive.hcatalog.common.HCatException)22 IOException (java.io.IOException)21 HCatSchema (org.apache.hive.hcatalog.data.schema.HCatSchema)21 HashMap (java.util.HashMap)19 Configuration (org.apache.hadoop.conf.Configuration)15 MetaException (org.apache.hadoop.hive.metastore.api.MetaException)15 FieldSchema (org.apache.hadoop.hive.metastore.api.FieldSchema)7 ResourceSchema (org.apache.pig.ResourceSchema)6 HCatTable (org.apache.hive.hcatalog.api.HCatTable)5 ResourceFieldSchema (org.apache.pig.ResourceSchema.ResourceFieldSchema)5 List (java.util.List)4 Map (java.util.Map)4 Properties (java.util.Properties)4 Path (org.apache.hadoop.fs.Path)4 StorageDescriptor (org.apache.hadoop.hive.metastore.api.StorageDescriptor)3 FrontendException (org.apache.pig.impl.logicalLayer.FrontendException)3 FieldSchema (org.apache.pig.impl.logicalLayer.schema.Schema.FieldSchema)3