DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Introducing Graph Concepts in Java With Eclipse JNoSQL, Part 3: Understanding Janus
  • Introducing Graph Concepts in Java With Eclipse JNoSQL, Part 2: Understanding Neo4j
  • How to Introduce a New API Quickly Using Micronaut
  • Introducing Graph Concepts in Java With Eclipse JNoSQL

Trending

  • After 9 Years, Microsoft Fulfills This Windows Feature Request
  • Memory Leak Due to Time-Taking finalize() Method
  • Caching 101: Theory, Algorithms, Tools, and Best Practices
  • Introducing Graph Concepts in Java With Eclipse JNoSQL, Part 2: Understanding Neo4j
  1. DZone
  2. Coding
  3. Java
  4. Writing Java APIs Using Apache Atlas Client

Writing Java APIs Using Apache Atlas Client

See how to write Java APIs using an Apache Atlas client.

By 
Himani Arora user avatar
Himani Arora
·
Updated Jun. 26, 19 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
13.8K Views

Join the DZone community and get the full member experience.

Join For Free

In the previous blog, Data Governance using Apache ATLAS we discussed the advantages and use cases of using Apache Atlas as a data governance tool. In continuation to it, we will be discussing building our own Java APIs, which can interact with Apache Atlas using Apache atlas client to create new entities and types in it.

Image title

How to Create New Entities and Types Using Atlas Client

Atlas Client Maven Dependency

The following dependencies can be used to update pom.xml:

<dependency>
 <groupId>org.apache.atlas</groupId>
 <artifactId>atlas-client-common</artifactId>
 <version>1.1.0</version>
</dependency>
<dependency>
 <groupId>org.apache.atlas</groupId>
 <artifactId>atlas-common</artifactId>
 <version>1.1.0</version>
</dependency>
<dependency>
 <groupId>org.apache.atlas</groupId>
 <artifactId>atlas-client-v1</artifactId>
 <version>1.1.0</version>
</dependency>

Setting up Atlas-application.properties

Apache Atlas Client uses atlas-application properties to establish a connection between our API and Apache Atlas server. The properties should be placed in resources/atlas-application.properties.

#########  Server Properties  #########
atlas.rest.address=http://localhost:21000

atlas.hook.demo.kafka.retries=1
atlas.kafka.zookeeper.connect=localhost:3181
atlas.kafka.bootstrap.servers=localhost:9092
atlas.kafka.zookeeper.session.timeout.ms=400
atlas.kafka.zookeeper.connection.timeout.ms=200
atlas.kafka.zookeeper.sync.time.ms=20
atlas.kafka.auto.commit.interval.ms=1000
atlas.kafka.hook.group.id=atlas

Creating a Connection to Atlas Server

To create a connection with Apache atlas Server, baseUrl and username, the password is to be passed in AtlasClient constructor,

public AtlasClient(String[] baseUrl, String[] basicAuthUserNamePassword) {
...
}

For example,

private final AtlasClient atlasClient;
AtlasEntityExample() {
   atlasClient = new AtlasClient(new String[]{"http://localhost:21000"}, new String[]{"admin", "admin"});
}

Listing Different Types Registered With Apache Atlas

private void listTypes() throws AtlasServiceException {
   System.out.println("Types registered with Atlas:");   List<String> types = atlasClient.listTypes();   for (String type : types) {       System.out.println("Type: " + type);   } }

The method on execution will provide an output like this,

Type: hive_principal_type
Type: AtlasGlossaryTermRelationshipStatus
Type: file_action
Type: AtlasGlossaryTermAssignmentStatus
Type: hive_order
Type: hive_serde
Type: fs_permissions
Type: DataSet
Type: Process
Type: hive_table
Type: avro_primitive
Type: storm_node
Type: Referenceable
...
...
...

Registering New Types in Apache Atlas

private void createTypes() throws Exception {
   TypesDef typesDef = createTypeDefinitions();

   String typesAsJSON = AtlasType.toV1Json(typesDef);   System.out.println("typesAsJSON = " + typesAsJSON);   atlasClient.createType(typesAsJSON); } TypesDef createTypeDefinitions() throws Exception {   ClassTypeDefinition dbClsDef = TypesUtil           .createClassTypeDef(DATABASE_TYPE, DATABASE_TYPE, null,                   TypesUtil.createUniqueRequiredAttrDef("name", AtlasBaseTypeDef.ATLAS_TYPE_STRING),                   attrDef("description", AtlasBaseTypeDef.ATLAS_TYPE_STRING), attrDef("locationUri", AtlasBaseTypeDef.ATLAS_TYPE_STRING),                   attrDef("owner", AtlasBaseTypeDef.ATLAS_TYPE_STRING), attrDef("createTime", AtlasBaseTypeDef.ATLAS_TYPE_LONG));         TraitTypeDefinition jdbcTraitDef = TypesUtil.createTraitTypeDef("JdbcAccess_v11", "JdbcAccess Trait", null);     return new TypesDef(Collections.<EnumTypeDefinition>emptyList(), Collections.<StructTypeDefinition>emptyList(),           Arrays.asList(jdbcTraitDef),           Arrays.asList(dbClsDef)); } AttributeDefinition attrDef(String name, String dT) {   return attrDef(name, dT, Multiplicity.OPTIONAL, false, null); } AttributeDefinition attrDef(String name, String dT, Multiplicity m, boolean isComposite,                           String reverseAttributeName) {   Preconditions.checkNotNull(name);   Preconditions.checkNotNull(dT);   return new AttributeDefinition(name, dT, m, isComposite, reverseAttributeName); }

If the new type has been successfully created in Atlas Server, a success code of 200 will be rerun,

------------------------------------------------------
Call         : GET api/atlas/types
Content-type : application/json; charset=UTF-8 
[Accept       : application/json 
HTTP Status  : 200
------------------------------------------------------

Creating New Entity for a Registered Type

private String createEntity()
       throws AtlasServiceException {

   Referenceable referenceable = new Referenceable("DB_v11");
   referenceable.set("name", "testt-db-v1");
   referenceable.set("description", "desc");
   referenceable.set("owner", "himani");
   referenceable.set("locationUri", "test-db-v2");

   String entityJSON = AtlasType.toV1Json(referenceable);   System.out.println("Submitting new entity= " + entityJSON);   List<String> entitiesCreated = atlasClient.createEntity(entityJSON);     return entitiesCreated.get(entitiesCreated.size() - 1); }

A unique GUID of the entity will be returned if it has been successfully created in the Server. For example,

------------------------------------------------------
Call         : POST api/atlas/entities
Content-type : application/json; charset=UTF-8 
Accept       : application/json 
Request      : [{"typeName":"DB_v11","values":{"owner":"himani","name":"testt-schema-v2","description":"desc","locationUri":"test-schema-v2"},"id":{"id":"-4336797886256","typeName":"DB_v11","version":0,"state":"ACTIVE","jsonClass":"org.apache.atlas.typesystem.json.InstanceSerialization$_Id"},"traits":{},"traitNames":[],"systemAttributes":null,"jsonClass":"org.apache.atlas.typesystem.json.InstanceSerialization$_Reference"}]
HTTP Status  : 201
Response     : {"entities":{"created":["20c8b710-eed5-4512-beda-d9fcf08f4bb7"]},"requestId":"..."} ------------------------------------------------------

Deleting Entities From Atlas Server Using GUIDs

The following code snippet can be used to delete existing entities from Atlas Server:

private List<String> deleteEntity(final String... guids) throws AtlasServiceException {
   return atlasClient.deleteEntities(guids).getDeletedEntities();
}

The complete example code for creating a CRUD Java API to interact with Apache Atlas Server can be found here.

References

  • https://github.com/knoldus/atlas-java-crud
  • https://blog.knoldus.com/apache-atlas/
  • https://github.com/apache/atlas

This article was first published on the Knoldus blog.

Java (programming language)

Published at DZone with permission of Himani Arora, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Introducing Graph Concepts in Java With Eclipse JNoSQL, Part 3: Understanding Janus
  • Introducing Graph Concepts in Java With Eclipse JNoSQL, Part 2: Understanding Neo4j
  • How to Introduce a New API Quickly Using Micronaut
  • Introducing Graph Concepts in Java With Eclipse JNoSQL

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!