Thursday, 6 October 2022

Atlas Client: Partial or patch update of glossary by id

AtlasClientV2#partialUpdateGlossaryByGuid method is used to update the glossary partially.

 

Signature

public AtlasGlossary partialUpdateGlossaryByGuid(String glossaryGuid, Map<String, String> attributes) throws AtlasServiceException

 

Example

Map<String, String> attributes = new HashMap<>();
attributes.put("name", atlasGlossary.getName() + "_updated");

atlasGlossary = atlasClient.partialUpdateGlossaryByGuid(glossaryId, attributes);

 Before updating.

 


Find the below working application.

 

atlas-application.properties

atlas.client.readTimeoutMSecs=30000
atlas.client.connectTimeoutMSecs=30000

JsonUtil.java

package com.sample.app.util;

import java.io.IOException;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

public class JsonUtil {
	public static String marshal(Object obj) throws JsonProcessingException {
		ObjectMapper mapper = new ObjectMapper();
		return mapper.writeValueAsString(obj);
	}

	public static <T> T unmarshal(Class<T> clazz, String json)
			throws JsonParseException, JsonMappingException, IOException {
		ObjectMapper mapper = new ObjectMapper();
		return (T) mapper.readValue(json, clazz);
	}

	public static String prettyPrintJson(Object obj) throws JsonProcessingException {
		ObjectMapper mapper = new ObjectMapper();
		mapper.enable(SerializationFeature.INDENT_OUTPUT);
		return mapper.writeValueAsString(obj);
	}
}

GlossaryPatchUpdateById.java

package com.sample.app.glossary;

import java.util.HashMap;
import java.util.Map;

import org.apache.atlas.AtlasClientV2;
import org.apache.atlas.AtlasServiceException;
import org.apache.atlas.model.glossary.AtlasGlossary;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.sample.app.util.JsonUtil;

public class GlossaryPatchUpdateById {

	public static void main(String[] args) throws AtlasServiceException, JsonProcessingException {
		AtlasClientV2 atlasClient = new AtlasClientV2(new String[] { "http://localhost:21000" },
				new String[] { "admin", "admin" });

		String glossaryId = "844a3e1a-110c-4bef-91bf-622dc1eab3e7";
		AtlasGlossary atlasGlossary = atlasClient.getGlossaryByGuid(glossaryId);
		System.out.println("persisted glossary : " + JsonUtil.prettyPrintJson(atlasGlossary));

		if (atlasGlossary == null) {
			System.out.println("Glossary not exists");
			System.exit(0);
		}

		Map<String, String> attributes = new HashMap<>();
		attributes.put("name", atlasGlossary.getName() + "_updated");

		atlasGlossary = atlasClient.partialUpdateGlossaryByGuid(glossaryId, attributes);

		System.out.println("Updated glossary : \n" + JsonUtil.prettyPrintJson(atlasGlossary));

	}

}

Output

persisted glossary : {
  "guid" : "844a3e1a-110c-4bef-91bf-622dc1eab3e7",
  "qualifiedName" : "myGlosary3",
  "name" : "myGlosary3",
  "shortDescription" : "myGlosary3...",
  "longDescription" : "myGlosary3...............",
  "terms" : [ {
    "termGuid" : "eb6afad3-2c82-4b16-8708-fc0694dbeb80",
    "relationGuid" : "80731d37-b42e-4e21-8d0e-ff9d20dc57e6",
    "displayText" : "term3"
  }, {
    "termGuid" : "b480660d-d038-41bd-b2c1-076adedd2421",
    "relationGuid" : "10fc95da-1638-4c84-b8e3-41b79034ab58",
    "displayText" : "term4"
  }, {
    "termGuid" : "8ba708a2-3e28-40a0-b0ee-3a642e2f5c5a",
    "relationGuid" : "cc153d34-6851-499d-988c-682eb0dd122d",
    "displayText" : "term5"
  }, {
    "termGuid" : "cd2dbbc5-0440-477e-a6e6-175eb13e3d46",
    "relationGuid" : "607768b1-d47a-4659-aa63-9acc8d175c80",
    "displayText" : "term1"
  }, {
    "termGuid" : "8d0451be-bfe9-497c-aae9-f1892b156c94",
    "relationGuid" : "8fe3ae77-0a42-42a5-8230-5a1c954b819b",
    "displayText" : "term2"
  } ],
  "categories" : [ {
    "categoryGuid" : "82d54387-9919-414d-9a1f-34bfeadc6ffa",
    "relationGuid" : "51f1d6c5-e164-4338-8e41-10c9aa97c4a0",
    "displayText" : "category2"
  }, {
    "categoryGuid" : "80e199fb-f924-4470-adc1-8227b5650f6c",
    "relationGuid" : "c9e00acf-17ce-4e06-b570-3c47a05dedb3",
    "displayText" : "category1"
  } ]
}
Updated glossary : 
{
  "guid" : "844a3e1a-110c-4bef-91bf-622dc1eab3e7",
  "qualifiedName" : "myGlosary3",
  "name" : "myGlosary3_updated",
  "shortDescription" : "myGlosary3...",
  "longDescription" : "myGlosary3...............",
  "terms" : [ {
    "termGuid" : "eb6afad3-2c82-4b16-8708-fc0694dbeb80",
    "relationGuid" : "80731d37-b42e-4e21-8d0e-ff9d20dc57e6",
    "displayText" : "term3"
  }, {
    "termGuid" : "b480660d-d038-41bd-b2c1-076adedd2421",
    "relationGuid" : "10fc95da-1638-4c84-b8e3-41b79034ab58",
    "displayText" : "term4"
  }, {
    "termGuid" : "8ba708a2-3e28-40a0-b0ee-3a642e2f5c5a",
    "relationGuid" : "cc153d34-6851-499d-988c-682eb0dd122d",
    "displayText" : "term5"
  }, {
    "termGuid" : "cd2dbbc5-0440-477e-a6e6-175eb13e3d46",
    "relationGuid" : "607768b1-d47a-4659-aa63-9acc8d175c80",
    "displayText" : "term1"
  }, {
    "termGuid" : "8d0451be-bfe9-497c-aae9-f1892b156c94",
    "relationGuid" : "8fe3ae77-0a42-42a5-8230-5a1c954b819b",
    "displayText" : "term2"
  } ],
  "categories" : [ {
    "categoryGuid" : "82d54387-9919-414d-9a1f-34bfeadc6ffa",
    "relationGuid" : "51f1d6c5-e164-4338-8e41-10c9aa97c4a0",
    "displayText" : "category2"
  }, {
    "categoryGuid" : "80e199fb-f924-4470-adc1-8227b5650f6c",
    "relationGuid" : "c9e00acf-17ce-4e06-b570-3c47a05dedb3",
    "displayText" : "category1"
  } ]
}


  

Previous                                                    Next                                                    Home

Saturday, 1 October 2022

Hive: pi(): Return the value of pi

Signature

pi()
hive> SELECT pi();
OK
_c0
3.141592653589793
Time taken: 0.046 seconds, Fetched: 1 row(s)

 

 

Previous                                                    Next                                                    Home

Atlas Client: Update glossary by id

AtlasClientV2#updateGlossaryByGuid method is used to update the glossary by guid.

 

Signature

public AtlasGlossary updateGlossaryByGuid(String glossaryGuid, AtlasGlossary updatedGlossary) throws AtlasServiceException

 

Example

AtlasGlossary glossaryToUpdate = new AtlasGlossary();
glossaryToUpdate.setName(atlasGlossary.getName() + "_updated");
glossaryToUpdate.setShortDescription(atlasGlossary.getShortDescription() + "_updated");
atlasGlossary = atlasClient.updateGlossaryByGuid(glossaryId, glossaryToUpdate);

Before updating the glossary ‘myGlosary3’

 

 

Find the below working application.

 

atlas-application.properties
atlas.client.readTimeoutMSecs=30000
atlas.client.connectTimeoutMSecs=30000

 

JsonUtil.java

package com.sample.app.util;

import java.io.IOException;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

public class JsonUtil {
	public static String marshal(Object obj) throws JsonProcessingException {
		ObjectMapper mapper = new ObjectMapper();
		return mapper.writeValueAsString(obj);
	}

	public static <T> T unmarshal(Class<T> clazz, String json)
			throws JsonParseException, JsonMappingException, IOException {
		ObjectMapper mapper = new ObjectMapper();
		return (T) mapper.readValue(json, clazz);
	}

	public static String prettyPrintJson(Object obj) throws JsonProcessingException {
		ObjectMapper mapper = new ObjectMapper();
		mapper.enable(SerializationFeature.INDENT_OUTPUT);
		return mapper.writeValueAsString(obj);
	}
}

 

UpdateGlossaryById.java

package com.sample.app.glossary;

import org.apache.atlas.AtlasClientV2;
import org.apache.atlas.AtlasServiceException;
import org.apache.atlas.model.glossary.AtlasGlossary;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.sample.app.util.JsonUtil;

public class UpdateGlossaryById {

  public static void main(String[] args) throws AtlasServiceException, JsonProcessingException {
    AtlasClientV2 atlasClient = new AtlasClientV2(new String[] { "http://localhost:21000" },
        new String[] { "admin", "admin" });

    String glossaryId = "844a3e1a-110c-4bef-91bf-622dc1eab3e7";
    AtlasGlossary atlasGlossary = atlasClient.getGlossaryByGuid(glossaryId);
    System.out.println("persisted glossary : " + JsonUtil.prettyPrintJson(atlasGlossary));

    if (atlasGlossary == null) {
      System.out.println("Glossary not exists");
      System.exit(0);
    }

    AtlasGlossary glossaryToUpdate = new AtlasGlossary();
    glossaryToUpdate.setName(atlasGlossary.getName() + "_updated");
    glossaryToUpdate.setShortDescription(atlasGlossary.getShortDescription() + "_updated");

    atlasGlossary = atlasClient.updateGlossaryByGuid(glossaryId, glossaryToUpdate);

    System.out.println("Updated glossary : \n" + JsonUtil.prettyPrintJson(atlasGlossary));

  }

}

 

Output

persisted glossary : {
  "guid" : "844a3e1a-110c-4bef-91bf-622dc1eab3e7",
  "qualifiedName" : "myGlosary3",
  "name" : "myGlosary3",
  "shortDescription" : "myGlosary3....",
  "longDescription" : "myGlosary3.................",
  "terms" : [ {
    "termGuid" : "eb6afad3-2c82-4b16-8708-fc0694dbeb80",
    "relationGuid" : "80731d37-b42e-4e21-8d0e-ff9d20dc57e6",
    "displayText" : "term3"
  }, {
    "termGuid" : "b480660d-d038-41bd-b2c1-076adedd2421",
    "relationGuid" : "10fc95da-1638-4c84-b8e3-41b79034ab58",
    "displayText" : "term4"
  }, {
    "termGuid" : "8ba708a2-3e28-40a0-b0ee-3a642e2f5c5a",
    "relationGuid" : "cc153d34-6851-499d-988c-682eb0dd122d",
    "displayText" : "term5"
  }, {
    "termGuid" : "cd2dbbc5-0440-477e-a6e6-175eb13e3d46",
    "relationGuid" : "607768b1-d47a-4659-aa63-9acc8d175c80",
    "displayText" : "term1"
  }, {
    "termGuid" : "8d0451be-bfe9-497c-aae9-f1892b156c94",
    "relationGuid" : "8fe3ae77-0a42-42a5-8230-5a1c954b819b",
    "displayText" : "term2"
  } ],
  "categories" : [ {
    "categoryGuid" : "82d54387-9919-414d-9a1f-34bfeadc6ffa",
    "relationGuid" : "51f1d6c5-e164-4338-8e41-10c9aa97c4a0",
    "displayText" : "category2"
  }, {
    "categoryGuid" : "80e199fb-f924-4470-adc1-8227b5650f6c",
    "relationGuid" : "c9e00acf-17ce-4e06-b570-3c47a05dedb3",
    "displayText" : "category1"
  } ]
}
Updated glossary : 
{
  "guid" : "844a3e1a-110c-4bef-91bf-622dc1eab3e7",
  "qualifiedName" : "myGlosary3",
  "name" : "myGlosary3_updated",
  "shortDescription" : "myGlosary3...._updated",
  "terms" : [ {
    "termGuid" : "eb6afad3-2c82-4b16-8708-fc0694dbeb80",
    "relationGuid" : "80731d37-b42e-4e21-8d0e-ff9d20dc57e6",
    "displayText" : "term3"
  }, {
    "termGuid" : "b480660d-d038-41bd-b2c1-076adedd2421",
    "relationGuid" : "10fc95da-1638-4c84-b8e3-41b79034ab58",
    "displayText" : "term4"
  }, {
    "termGuid" : "8ba708a2-3e28-40a0-b0ee-3a642e2f5c5a",
    "relationGuid" : "cc153d34-6851-499d-988c-682eb0dd122d",
    "displayText" : "term5"
  }, {
    "termGuid" : "cd2dbbc5-0440-477e-a6e6-175eb13e3d46",
    "relationGuid" : "607768b1-d47a-4659-aa63-9acc8d175c80",
    "displayText" : "term1"
  }, {
    "termGuid" : "8d0451be-bfe9-497c-aae9-f1892b156c94",
    "relationGuid" : "8fe3ae77-0a42-42a5-8230-5a1c954b819b",
    "displayText" : "term2"
  } ],
  "categories" : [ {
    "categoryGuid" : "82d54387-9919-414d-9a1f-34bfeadc6ffa",
    "relationGuid" : "51f1d6c5-e164-4338-8e41-10c9aa97c4a0",
    "displayText" : "category2"
  }, {
    "categoryGuid" : "80e199fb-f924-4470-adc1-8227b5650f6c",
    "relationGuid" : "c9e00acf-17ce-4e06-b570-3c47a05dedb3",
    "displayText" : "category1"
  } ]
}

After updation



 

As you see above image, Long description is unset (it is because I didn’t set longDescription explicitly while updating, if you want to go with partial update, use the method partialUpdateGlossaryByGuid).

  

Previous                                                    Next                                                    Home

Create a fat jar using maven assembly plugin

Maven assembly plugin enable developers to generate a fat jar that can contains dependencies, modules, site documentation, and other files.

 

At the time of writing this post, Maven assembly plugin can create distributions in the following formats:

 

a.   zip

b.   tar

c.    tar.gz (or tgz)

d.   tar.bz2 (or tbz2)

e.   tar.snappy

f.     tar.xz (or txz)

g.   jar

h.   dir

i.     war

 

How to configure assembly plugin in pom.xml?

Maven assembly plugin block looks like below.

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-assembly-plugin</artifactId>
	<version>2.6</version>
	<configuration>
		<descriptorRefs>
			<descriptorRef>jar-with-dependencies</descriptorRef>
		</descriptorRefs>
		<archive>
			<manifest>
				<addClasspath>true</addClasspath>
				<mainClass>com.sample.app.App</mainClass>
			</manifest>
		</archive>
	</configuration>
	<executions>
		<execution>
			<id>assemble-all</id>
			<phase>package</phase>
			<goals>
				<goal>single</goal>
			</goals>
		</execution>
	</executions>
</plugin>

 

Follow below step-by-step procedure to build a fat jar using assembly plugin.

 

Step 1: Create new maven project ‘assembly-plugin-demo’.

 

Step 2: Update pom.xml with maven dependencies.

 

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.sample.app</groupId>
	<artifactId>assembly-plugin-demo</artifactId>
	<version>1</version>

	<properties>
		<maven.compiler.target>1.8</maven.compiler.target>
		<maven.compiler.source>1.8</maven.compiler.source>
	</properties>

	<dependencies>
		<dependency>
			<groupId>com.google.code.gson</groupId>
			<artifactId>gson</artifactId>
			<version>2.9.1</version>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-assembly-plugin</artifactId>
				<version>2.6</version>
				<configuration>
					<descriptorRefs>
						<descriptorRef>jar-with-dependencies</descriptorRef>
					</descriptorRefs>
					<archive>
						<manifest>
							<addClasspath>true</addClasspath>
							<mainClass>com.sample.app.App</mainClass>
						</manifest>
					</archive>
				</configuration>
				<executions>
					<execution>
						<id>assemble-all</id>
						<phase>package</phase>
						<goals>
							<goal>single</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
</project>

 

Step 3: Define main application class.

 

App.java

package com.sample.app;

import com.google.gson.Gson;

public class App {
	static class Employee {
		private int id;
		private String name;

		public Employee(int id, String name) {
			this.id = id;
			this.name = name;
		}

	}

	public static void main(String[] args) {
		Employee emp = new Employee(1, "Krishna");
		Gson gson = new Gson();
		System.out.println(gson.toJson(emp));
		
	}
}

 

Total project structure looks like below.



How to get a fat jar from maven project?

Navigate to the folder where pom.xml is located and execute the command ‘mvn package’.

 

It generates jar file (assembly-plugin-demo-1-jar-with-dependencies.jar ) in the folder target.

 

How to run the jar file?

java -jar {jar_file}

$java -jar ./target/assembly-plugin-demo-1-jar-with-dependencies.jar 
{"id":1,"name":"Krishna"}

You can download the application from this link.




Previous                                                 Next                                                 Home