Friday, 30 September 2022

Hive: negative: Negate the number

Signature

negative(n)

Return -n.

 

hive> SELECT negative(-10);
OK
_c0
10
Time taken: 0.053 seconds, Fetched: 1 row(s)
hive> ;
hive> ;
hive> SELECT negative(10);
OK
_c0
-10
Time taken: 0.039 seconds, Fetched: 1 row(s)

 

  

Previous                                                    Next                                                    Home

Atlas Client: Get related terms of this term

AtlasClientV2#getRelatedTerms method return the related terms of this term.

 

Signature

public Map<AtlasGlossaryTerm.Relation, Set<AtlasRelatedTermHeader>> getRelatedTerms(String termGuid, String sortByAttribute, int limit, int offset) throws AtlasServiceException

 

Example

Map<AtlasGlossaryTerm.Relation, Set<AtlasRelatedTermHeader>> relatedTerms = atlasClient.getRelatedTerms(termId, null, 10, 0);

 

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);
	}
}

GetRelatedTerms.java

package com.sample.app.glossary;

import java.util.Map;
import java.util.Set;

import org.apache.atlas.AtlasClientV2;
import org.apache.atlas.AtlasServiceException;
import org.apache.atlas.model.glossary.AtlasGlossaryTerm;
import org.apache.atlas.model.glossary.relations.AtlasRelatedTermHeader;

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

public class GetRelatedTerms {

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

    String termId = "cd2dbbc5-0440-477e-a6e6-175eb13e3d46";

    Map<AtlasGlossaryTerm.Relation, Set<AtlasRelatedTermHeader>> relatedTerms = atlasClient.getRelatedTerms(termId, null, 10, 0);

    if (relatedTerms == null) {
      System.out.println("Related terms not exists");
    } else {
      System.out.println("Related terms : \n" + JsonUtil.prettyPrintJson(relatedTerms));
    }

  }

}

Output

Related terms : 
{
  "seeAlso" : [ {
    "termGuid" : "b480660d-d038-41bd-b2c1-076adedd2421",
    "relationGuid" : "d807b7c7-f243-4541-94c5-8555bd689cfa",
    "displayText" : "term4"
  }, {
    "termGuid" : "8d0451be-bfe9-497c-aae9-f1892b156c94",
    "relationGuid" : "a6b631ef-3b5b-4260-a069-5368ab39ad78",
    "displayText" : "term2"
  } ]
}

  

Previous                                                    Next                                                    Home

Thursday, 29 September 2022

How to execute tests in a maven project?

In this post, I am going to explain various scenarios to execute tests in a maven project.

 

Demo application has three test classes, where each class has one or more test cases.

 


 

Scenario 1: Execute all the tests.

mvn test

Scenario 2: Execute single test class.

mvn -Dtest=TestClass1 test

Above command execute all the tests in TestClass1.

Scenario 3: Execute tests in multiple test classes.

mvn -Dtest=TestClass1,TestClass3 test

Above command execute all the tests in TestClass1, TestClass3.

 

Scenario 4: Run single test case from a test class.

mvn -Dtest=TestClass1#testCase1 test

Scenario 5: Run all the test cases that match to a pattern in a test class.

mvn -Dtest=TestClass1#*2 test

Above snippet run all the test cases where the test case name ends with 2 in TestClass1.

 

You can download this application from this link.



Previous                                                 Next                                                 Home

Tuesday, 27 September 2022

Atlas Client: Get terms in a category

AtlasClientV2#getCategoryTerms method is used to get terms in a category.

 

Signature

public List<AtlasRelatedTermHeader> getCategoryTerms(String categoryGuid, String sortByAttribute, int limit, int offset) throws AtlasServiceException

 

Example

List<AtlasRelatedTermHeader> termsInACategory = atlasClient.getCategoryTerms(categoryId, null, 10, 0);

 

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);
	}
}

TermsInACategory.java

package com.sample.app.glossary;

import java.util.List;

import org.apache.atlas.AtlasClientV2;
import org.apache.atlas.AtlasServiceException;
import org.apache.atlas.model.glossary.relations.AtlasRelatedTermHeader;

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

public class TermsInACategory {

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

    String categoryId = "80e199fb-f924-4470-adc1-8227b5650f6c";

    List<AtlasRelatedTermHeader> termsInACategory = atlasClient.getCategoryTerms(categoryId, null, 10, 0);

    if (termsInACategory == null) {
      System.out.println("no terms associated with this category");
    } else {
      System.out.println("terms : \n" + JsonUtil.prettyPrintJson(termsInACategory));
    }

  }

}

Output

terms : 
[ {
  "termGuid" : "cd2dbbc5-0440-477e-a6e6-175eb13e3d46",
  "relationGuid" : "c1a5772f-290e-4696-a80c-5cc5a69b9bdd",
  "displayText" : "term1"
}, {
  "termGuid" : "8d0451be-bfe9-497c-aae9-f1892b156c94",
  "relationGuid" : "aa82b016-39bb-40ab-83fc-aa7f14c17e5a",
  "displayText" : "term2"
} ]




 

Previous                                                    Next                                                    Home

Hive: log10: Return base 10 logarithm of a number

Signature

log10(n)

 

hive> SELECT log10(100);
OK
_c0
2.0
Time taken: 0.045 seconds, Fetched: 1 row(s)
hive> SELECT log10(1000);
OK
_c0
3.0
Time taken: 0.04 seconds, Fetched: 1 row(s)

 

 

Previous                                                    Next                                                    Home

Jackson: Convert TOML to a map in java

Using TypeReference , we can map JSON data to a map.

Map<Integer, Employee> map = tomlMapper.readValue(
		toml, new TypeReference<Map<Integer, Employee>>() {
});

 

Find the below working application.

 

Employee.java
package com.sample.app.dto;

public class Employee {
	private int id;

	private String firstName;

	private String lastName;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	@Override
	public String toString() {
		return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + "]";
	}

}

TomlToMap.java

package com.sample.app;

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

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.dataformat.toml.TomlMapper;
import com.sample.app.dto.Employee;

public class TomlToMap {

	public static void main(String[] args) throws JsonProcessingException {
		Employee emp1 = new Employee();
		emp1.setFirstName("Krishna");
		emp1.setId(1);
		emp1.setLastName("Gurram");

		Employee emp2 = new Employee();
		emp2.setFirstName("Ram");
		emp2.setId(2);
		emp2.setLastName("Maj");

		Map<Integer, Employee> empsMap = new HashMap<>();
		empsMap.put(1, emp1);
		empsMap.put(2, emp2);

		TomlMapper tomlMapper = new TomlMapper();
		String toml = tomlMapper.writeValueAsString(empsMap);
		System.out.println(toml);

		Map<Integer, Employee> map = tomlMapper.readValue(
				toml, new TypeReference<Map<Integer, Employee>>() {
		});

		for (Integer key : map.keySet()) {
			Employee emp = map.get(key);
			System.out.println(emp);
		}

	}

}

Output

1.id = 1
1.firstName = 'Krishna'
1.lastName = 'Gurram'
2.id = 2
2.firstName = 'Ram'
2.lastName = 'Maj'

Employee [id=1, firstName=Krishna, lastName=Gurram]
Employee [id=2, firstName=Ram, lastName=Maj]

Following toml

1.id = 1
1.firstName = 'Krishna'
1.lastName = 'Gurram'
2.id = 2
2.firstName = 'Ram'
2.lastName = 'Maj'

is equivalent to below json.

{
  "1": {
    "id": 1,
    "firstName": "Krishna",
    "lastName": "Gurram"
  },
  "2": {
    "id": 2,
    "firstName": "Ram",
    "lastName": "Maj"
  }
}

Dependency used

<dependencies>
  <dependency>
    <groupId>com.fasterxml.jackson.dataformat</groupId>
    <artifactId>jackson-dataformat-toml</artifactId>
    <version>2.13.4</version>
  </dependency>

  <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.13.4</version>
  </dependency>

  <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.13.4</version>
  </dependency>

  <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.13.4</version>
  </dependency>

</dependencies>





Previous                                                 Next                                                 Home