Search in sources :

Example 16 with Struct

use of org.apache.atlas.typesystem.Struct in project incubator-atlas by apache.

the class TraitTest method test1.

/*
     * Type Hierarchy is:
     *   A(a,b,c,d)
     *   B(b) extends A
     *   C(c) extends A
     *   D(d) extends B,C
     *
     * - There are a total of 11 fields in an instance of D
     * - an attribute that is hidden by a SubType can referenced by prefixing it with the
     * complete Path.
     *   For e.g. the 'b' attribute in A (that is a superType for B) is hidden the 'b' attribute
     *   in B.
     *   So it is available by the name 'A.B.D.b'
     *
     * - Another way to set attributes is to cast. Casting a 'D' instance of 'B' makes the 'A.B.D
     * .b' attribute
     *   available as 'A.B.b'. Casting one more time to an 'A' makes the 'A.B.b' attribute
     *   available as 'b'.
     */
@Test
public void test1() throws AtlasException {
    HierarchicalTypeDefinition A = createTraitTypeDef("A", null, createRequiredAttrDef("a", DataTypes.INT_TYPE), createOptionalAttrDef("b", DataTypes.BOOLEAN_TYPE), createOptionalAttrDef("c", DataTypes.BYTE_TYPE), createOptionalAttrDef("d", DataTypes.SHORT_TYPE));
    HierarchicalTypeDefinition B = createTraitTypeDef("B", ImmutableSet.of("A"), createOptionalAttrDef("b", DataTypes.BOOLEAN_TYPE));
    HierarchicalTypeDefinition C = createTraitTypeDef("C", ImmutableSet.of("A"), createOptionalAttrDef("c", DataTypes.BYTE_TYPE));
    HierarchicalTypeDefinition D = createTraitTypeDef("D", ImmutableSet.of("B", "C"), createOptionalAttrDef("d", DataTypes.SHORT_TYPE));
    defineTraits(A, B, C, D);
    TraitType DType = getTypeSystem().getDataType(TraitType.class, "D");
    //        for(String aName : DType.fieldMapping().fields.keySet()) {
    //            System.out.println(String.format("nameToQualifiedName.put(\"%s\", \"%s\");", aName, DType
    // .getQualifiedName(aName)));
    //        }
    Map<String, String> nameToQualifiedName = new HashMap();
    {
        nameToQualifiedName.put("d", "D.d");
        nameToQualifiedName.put("b", "B.b");
        nameToQualifiedName.put("c", "C.c");
        nameToQualifiedName.put("a", "A.a");
        nameToQualifiedName.put("A.B.D.b", "A.B.D.b");
        nameToQualifiedName.put("A.B.D.c", "A.B.D.c");
        nameToQualifiedName.put("A.B.D.d", "A.B.D.d");
        nameToQualifiedName.put("A.C.D.a", "A.C.D.a");
        nameToQualifiedName.put("A.C.D.b", "A.C.D.b");
        nameToQualifiedName.put("A.C.D.c", "A.C.D.c");
        nameToQualifiedName.put("A.C.D.d", "A.C.D.d");
    }
    Struct s1 = new Struct("D");
    s1.set("d", 1);
    s1.set("c", 1);
    s1.set("b", true);
    s1.set("a", 1);
    s1.set("A.B.D.b", true);
    s1.set("A.B.D.c", 2);
    s1.set("A.B.D.d", 2);
    s1.set("A.C.D.a", 3);
    s1.set("A.C.D.b", false);
    s1.set("A.C.D.c", 3);
    s1.set("A.C.D.d", 3);
    ITypedStruct ts = DType.convert(s1, Multiplicity.REQUIRED);
    Assert.assertEquals(ts.toString(), "{\n" + "\td : \t1\n" + "\tb : \ttrue\n" + "\tc : \t1\n" + "\ta : \t1\n" + "\tA.B.D.b : \ttrue\n" + "\tA.B.D.c : \t2\n" + "\tA.B.D.d : \t2\n" + "\tA.C.D.a : \t3\n" + "\tA.C.D.b : \tfalse\n" + "\tA.C.D.c : \t3\n" + "\tA.C.D.d : \t3\n" + "}");
    /*
         * cast to B and set the 'b' attribute on A.
         */
    TraitType BType = getTypeSystem().getDataType(TraitType.class, "B");
    IStruct s2 = DType.castAs(ts, "B");
    s2.set("A.B.b", false);
    Assert.assertEquals(ts.toString(), "{\n" + "\td : \t1\n" + "\tb : \ttrue\n" + "\tc : \t1\n" + "\ta : \t1\n" + "\tA.B.D.b : \tfalse\n" + "\tA.B.D.c : \t2\n" + "\tA.B.D.d : \t2\n" + "\tA.C.D.a : \t3\n" + "\tA.C.D.b : \tfalse\n" + "\tA.C.D.c : \t3\n" + "\tA.C.D.d : \t3\n" + "}");
    /*
         * cast again to A and set the 'b' attribute on A.
         */
    TraitType AType = getTypeSystem().getDataType(TraitType.class, "A");
    IStruct s3 = BType.castAs(s2, "A");
    s3.set("b", true);
    Assert.assertEquals(ts.toString(), "{\n" + "\td : \t1\n" + "\tb : \ttrue\n" + "\tc : \t1\n" + "\ta : \t1\n" + "\tA.B.D.b : \ttrue\n" + "\tA.B.D.c : \t2\n" + "\tA.B.D.d : \t2\n" + "\tA.C.D.a : \t3\n" + "\tA.C.D.b : \tfalse\n" + "\tA.C.D.c : \t3\n" + "\tA.C.D.d : \t3\n" + "}");
}
Also used : HashMap(java.util.HashMap) ITypedStruct(org.apache.atlas.typesystem.ITypedStruct) ITypedStruct(org.apache.atlas.typesystem.ITypedStruct) IStruct(org.apache.atlas.typesystem.IStruct) Struct(org.apache.atlas.typesystem.Struct) IStruct(org.apache.atlas.typesystem.IStruct) Test(org.testng.annotations.Test)

Example 17 with Struct

use of org.apache.atlas.typesystem.Struct in project incubator-atlas by apache.

the class HiveMetaStoreBridge method fillStorageDesc.

public Referenceable fillStorageDesc(StorageDescriptor storageDesc, String tableQualifiedName, String sdQualifiedName, Id tableId) throws AtlasHookException {
    LOG.debug("Filling storage descriptor information for {}", storageDesc);
    Referenceable sdReferenceable = new Referenceable(HiveDataTypes.HIVE_STORAGEDESC.getName());
    sdReferenceable.set(AtlasClient.REFERENCEABLE_ATTRIBUTE_NAME, sdQualifiedName);
    SerDeInfo serdeInfo = storageDesc.getSerdeInfo();
    LOG.debug("serdeInfo = {}", serdeInfo);
    // SkewedInfo skewedInfo = storageDesc.getSkewedInfo();
    String serdeInfoName = HiveDataTypes.HIVE_SERDE.getName();
    Struct serdeInfoStruct = new Struct(serdeInfoName);
    serdeInfoStruct.set(AtlasClient.NAME, serdeInfo.getName());
    serdeInfoStruct.set("serializationLib", serdeInfo.getSerializationLib());
    serdeInfoStruct.set(PARAMETERS, serdeInfo.getParameters());
    sdReferenceable.set("serdeInfo", serdeInfoStruct);
    sdReferenceable.set(STORAGE_NUM_BUCKETS, storageDesc.getNumBuckets());
    sdReferenceable.set(STORAGE_IS_STORED_AS_SUB_DIRS, storageDesc.isStoredAsSubDirectories());
    List<Struct> sortColsStruct = new ArrayList<>();
    for (Order sortcol : storageDesc.getSortCols()) {
        String hiveOrderName = HiveDataTypes.HIVE_ORDER.getName();
        Struct colStruct = new Struct(hiveOrderName);
        colStruct.set("col", sortcol.getCol());
        colStruct.set("order", sortcol.getOrder());
        sortColsStruct.add(colStruct);
    }
    if (sortColsStruct.size() > 0) {
        sdReferenceable.set("sortCols", sortColsStruct);
    }
    sdReferenceable.set(LOCATION, storageDesc.getLocation());
    sdReferenceable.set("inputFormat", storageDesc.getInputFormat());
    sdReferenceable.set("outputFormat", storageDesc.getOutputFormat());
    sdReferenceable.set("compressed", storageDesc.isCompressed());
    if (storageDesc.getBucketCols().size() > 0) {
        sdReferenceable.set("bucketCols", storageDesc.getBucketCols());
    }
    sdReferenceable.set(PARAMETERS, storageDesc.getParameters());
    sdReferenceable.set("storedAsSubDirectories", storageDesc.isStoredAsSubDirectories());
    sdReferenceable.set(TABLE, tableId);
    return sdReferenceable;
}
Also used : Order(org.apache.hadoop.hive.metastore.api.Order) Referenceable(org.apache.atlas.typesystem.Referenceable) SerDeInfo(org.apache.hadoop.hive.metastore.api.SerDeInfo) ArrayList(java.util.ArrayList) Struct(org.apache.atlas.typesystem.Struct)

Example 18 with Struct

use of org.apache.atlas.typesystem.Struct in project incubator-atlas by apache.

the class EntityNotificationImpl method getAllTraits.

// ----- helper methods ----------------------------------------------------
private static List<IStruct> getAllTraits(IReferenceableInstance entityDefinition, TypeSystem typeSystem) throws AtlasException {
    List<IStruct> traitInfo = new LinkedList<>();
    for (String traitName : entityDefinition.getTraits()) {
        IStruct trait = entityDefinition.getTrait(traitName);
        String typeName = trait.getTypeName();
        Map<String, Object> valuesMap = trait.getValuesMap();
        traitInfo.add(new Struct(typeName, valuesMap));
        traitInfo.addAll(getSuperTraits(typeName, valuesMap, typeSystem));
    }
    return traitInfo;
}
Also used : LinkedList(java.util.LinkedList) IStruct(org.apache.atlas.typesystem.IStruct) IStruct(org.apache.atlas.typesystem.IStruct) Struct(org.apache.atlas.typesystem.Struct)

Example 19 with Struct

use of org.apache.atlas.typesystem.Struct in project incubator-atlas by apache.

the class EntityNotificationImpl method getSuperTraits.

private static List<IStruct> getSuperTraits(String typeName, Map<String, Object> values, TypeSystem typeSystem) throws AtlasException {
    List<IStruct> superTypes = new LinkedList<>();
    TraitType traitDef = typeSystem.getDataType(TraitType.class, typeName);
    Set<String> superTypeNames = traitDef.getAllSuperTypeNames();
    for (String superTypeName : superTypeNames) {
        TraitType superTraitDef = typeSystem.getDataType(TraitType.class, superTypeName);
        Map<String, Object> superTypeValues = new HashMap<>();
        FieldMapping fieldMapping = superTraitDef.fieldMapping();
        if (fieldMapping != null) {
            Set<String> superTypeAttributeNames = fieldMapping.fields.keySet();
            for (String superTypeAttributeName : superTypeAttributeNames) {
                if (values.containsKey(superTypeAttributeName)) {
                    superTypeValues.put(superTypeAttributeName, values.get(superTypeAttributeName));
                }
            }
        }
        IStruct superTrait = new Struct(superTypeName, superTypeValues);
        superTypes.add(superTrait);
        superTypes.addAll(getSuperTraits(superTypeName, values, typeSystem));
    }
    return superTypes;
}
Also used : HashMap(java.util.HashMap) TraitType(org.apache.atlas.typesystem.types.TraitType) FieldMapping(org.apache.atlas.typesystem.types.FieldMapping) LinkedList(java.util.LinkedList) IStruct(org.apache.atlas.typesystem.IStruct) IStruct(org.apache.atlas.typesystem.IStruct) Struct(org.apache.atlas.typesystem.Struct)

Example 20 with Struct

use of org.apache.atlas.typesystem.Struct in project incubator-atlas by apache.

the class NotificationEntityChangeListenerTest method testGetAllTraitsSuperTraits.

@Test
public void testGetAllTraitsSuperTraits() throws Exception {
    TypeSystem typeSystem = mock(TypeSystem.class);
    String traitName = "MyTrait";
    IStruct myTrait = new Struct(traitName);
    String superTraitName = "MySuperTrait";
    TraitType traitDef = mock(TraitType.class);
    Set<String> superTypeNames = Collections.singleton(superTraitName);
    TraitType superTraitDef = mock(TraitType.class);
    Set<String> superSuperTypeNames = Collections.emptySet();
    Referenceable entity = getEntity("id", myTrait);
    when(typeSystem.getDataType(TraitType.class, traitName)).thenReturn(traitDef);
    when(typeSystem.getDataType(TraitType.class, superTraitName)).thenReturn(superTraitDef);
    when(traitDef.getAllSuperTypeNames()).thenReturn(superTypeNames);
    when(superTraitDef.getAllSuperTypeNames()).thenReturn(superSuperTypeNames);
    List<IStruct> allTraits = NotificationEntityChangeListener.getAllTraits(entity, typeSystem);
    assertEquals(2, allTraits.size());
    for (IStruct trait : allTraits) {
        String typeName = trait.getTypeName();
        assertTrue(typeName.equals(traitName) || typeName.equals(superTraitName));
    }
}
Also used : TypeSystem(org.apache.atlas.typesystem.types.TypeSystem) Referenceable(org.apache.atlas.typesystem.Referenceable) TraitType(org.apache.atlas.typesystem.types.TraitType) IStruct(org.apache.atlas.typesystem.IStruct) IStruct(org.apache.atlas.typesystem.IStruct) Struct(org.apache.atlas.typesystem.Struct) Test(org.testng.annotations.Test)

Aggregations

Struct (org.apache.atlas.typesystem.Struct)56 Test (org.testng.annotations.Test)36 IStruct (org.apache.atlas.typesystem.IStruct)29 Referenceable (org.apache.atlas.typesystem.Referenceable)25 ITypedStruct (org.apache.atlas.typesystem.ITypedStruct)17 TraitType (org.apache.atlas.typesystem.types.TraitType)12 ArrayList (java.util.ArrayList)9 LinkedList (java.util.LinkedList)9 HashMap (java.util.HashMap)8 Map (java.util.Map)8 Id (org.apache.atlas.typesystem.persistence.Id)7 JSONObject (org.codehaus.jettison.json.JSONObject)7 BeforeTest (org.testng.annotations.BeforeTest)6 AfterTest (org.testng.annotations.AfterTest)5 ImmutableList (com.google.common.collect.ImmutableList)4 List (java.util.List)4 ITypedInstance (org.apache.atlas.typesystem.ITypedInstance)4 ITypedReferenceableInstance (org.apache.atlas.typesystem.ITypedReferenceableInstance)4 TypeSystem (org.apache.atlas.typesystem.types.TypeSystem)4 Date (java.util.Date)3