In this post, I am going to explain how can we define an attribute that takes list of string.
To define an attribute with type list, you should specify
a. typeName as array<string>
b. cardinality should be set to LIST like below.
"attributeDefs": [
{
"name": "hobbies",
"typeName": "array<string>",
"isOptional": false,
"cardinality": "LIST",
"valuesMinCount": 1,
"valuesMaxCount": 2147483647,
"isUnique": false,
"isIndexable": true,
"includeInNotification": false,
"searchWeight": -1
}
]
Find the below working application.
Create atlas-application.properties file under src/main/resources folder.
atlas-application.properties
atlas.client.readTimeoutMSecs=30000 atlas.client.connectTimeoutMSecs=30000
Define AttributeWithListOfStrings class.
AttributeWithListOfStrings.java
package com.sample.app.attributes;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import org.apache.atlas.AtlasClientV2;
import org.apache.atlas.AtlasServiceException;
import org.apache.atlas.model.instance.AtlasEntity;
import org.apache.atlas.model.typedef.AtlasEntityDef;
import org.apache.atlas.model.typedef.AtlasTypesDef;
import org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef;
import com.fasterxml.jackson.core.JsonProcessingException;
public class AttributeWithListOfStrings {
private static final String TYPE_NAME = "DemoType2";
private static void createType(AtlasClientV2 atlasClient) throws AtlasServiceException {
AtlasAttributeDef attributeDef1 = new AtlasAttributeDef();
attributeDef1.setName("hobbies");
attributeDef1.setTypeName("array<string>");
attributeDef1.setCardinality(AtlasAttributeDef.Cardinality.LIST);
attributeDef1.setIsIndexable(true);
attributeDef1.setIsUnique(false);
AtlasEntityDef atlasEntityDef = new AtlasEntityDef();
atlasEntityDef.setName(TYPE_NAME);
atlasEntityDef.setCreatedBy("Krishna");
atlasEntityDef.setUpdatedBy("krishna");
atlasEntityDef.setDescription("Represent a laptoo specification");
atlasEntityDef.setAttributeDefs(Arrays.asList(attributeDef1));
Set<String> superTypes = new HashSet<>();
superTypes.add("DataSet");
atlasEntityDef.setSuperTypes(superTypes);
AtlasTypesDef atlasTypesDef = new AtlasTypesDef();
atlasTypesDef.getEntityDefs().add(atlasEntityDef);
atlasClient.createAtlasTypeDefs(atlasTypesDef);
}
private static void createEntity(AtlasClientV2 atlasClient) throws AtlasServiceException {
AtlasEntity atlasEntity = new AtlasEntity();
atlasEntity.setTypeName(TYPE_NAME);
atlasEntity.setAttribute("hobbies", Arrays.asList("football", "cricket", "football"));
// Following are the mandatory attributes
atlasEntity.setAttribute("qualifiedName", "AttributeWithListOfStrings_DEMO1");
atlasEntity.setAttribute("name", "AttributeWithListOfStrings_DEMO1");
AtlasEntity.AtlasEntityWithExtInfo atlasEntityWithExtInfo = new AtlasEntity.AtlasEntityWithExtInfo();
atlasEntityWithExtInfo.setEntity(atlasEntity);
atlasClient.createEntity(atlasEntityWithExtInfo);
}
public static void main(String[] args) throws AtlasServiceException, JsonProcessingException {
AtlasClientV2 atlasClient = new AtlasClientV2(new String[] { "http://localhost:21000" },
new String[] { "admin", "admin" });
createType(atlasClient);
createEntity(atlasClient);
}
}
Login to Atlas UI and query for the type ‘DemoType2’.
Navigate to the entity ‘AttributeWithListOfStrings_DEMO1’ to see all the properties.
As you see above image, hobby ‘football’ is repeated twice. We can solve this problem by making the attribute cardinality as SET. I will explain this in my next post.
No comments:
Post a Comment