Monday, 4 July 2022

Hive: rename a table

Syntax

ALTER TABLE currentName RENAME TO newName;

 

Example

ALTER TABLE employee RENAME TO emp;

 

Login to hive shell by executing the command ‘hive’.

[cloudera@quickstart conf]$ hive

Logging initialized using configuration in file:/etc/hive/conf.dist/hive-log4j.properties
WARNING: Hive CLI is deprecated and migration to Beeline is recommended.
hive> ;
hive> SHOW tables;
OK
employee
Time taken: 0.411 seconds, Fetched: 1 row(s)

 

As you see above snippet, I have employee table in default database. Let’s execute below statement to rename employee table to emp.

 

ALTER TABLE employee RENAME TO emp;
hive> ALTER TABLE employee RENAME TO emp;
OK
Time taken: 0.159 seconds
hive> ;
hive> ;
hive> SHOW tables;
OK
emp
Time taken: 0.032 seconds, Fetched: 1 row(s)

 


Previous                                                    Next                                                    Home

Atlas client: update Atlas type definition

This is continuation to my previous post. In my previous post, I defined a new type ‘tablet’.

 

tablet definition looks like below

{
  "category": "ENTITY",
  "guid": "e7201b2c-a87d-49ef-aac0-c228b92f9763",
  "createdBy": "admin",
  "updatedBy": "admin",
  "createTime": 1644561370044,
  "updateTime": 1644561370044,
  "version": 1,
  "name": "tablet",
  "description": "Represent a laptoo specification",
  "typeVersion": "1.0",
  "attributeDefs": [
    {
      "name": "screen_size",
      "typeName": "string",
      "isOptional": false,
      "cardinality": "SINGLE",
      "valuesMinCount": 1,
      "valuesMaxCount": 1,
      "isUnique": false,
      "isIndexable": true,
      "includeInNotification": false,
      "searchWeight": -1
    },
    {
      "name": "operating_system",
      "typeName": "string",
      "isOptional": false,
      "cardinality": "SINGLE",
      "valuesMinCount": 1,
      "valuesMaxCount": 1,
      "isUnique": false,
      "isIndexable": true,
      "includeInNotification": false,
      "searchWeight": -1
    }
  ],
  "superTypes": [
    "DataSet"
  ],
  "subTypes": [],
  "relationshipAttributeDefs": [
    {
      "name": "inputToProcesses",
      "typeName": "array<Process>",
      "isOptional": true,
      "cardinality": "SET",
      "valuesMinCount": -1,
      "valuesMaxCount": -1,
      "isUnique": false,
      "isIndexable": false,
      "includeInNotification": false,
      "searchWeight": -1,
      "relationshipTypeName": "dataset_process_inputs",
      "isLegacyAttribute": false
    },
    {
      "name": "pipeline",
      "typeName": "spark_ml_pipeline",
      "isOptional": true,
      "cardinality": "SINGLE",
      "valuesMinCount": -1,
      "valuesMaxCount": -1,
      "isUnique": false,
      "isIndexable": false,
      "includeInNotification": false,
      "searchWeight": -1,
      "relationshipTypeName": "spark_ml_pipeline_dataset",
      "isLegacyAttribute": false
    },
    {
      "name": "schema",
      "typeName": "array<avro_schema>",
      "isOptional": true,
      "cardinality": "SET",
      "valuesMinCount": -1,
      "valuesMaxCount": -1,
      "isUnique": false,
      "isIndexable": false,
      "includeInNotification": false,
      "searchWeight": -1,
      "relationshipTypeName": "avro_schema_associatedEntities",
      "isLegacyAttribute": false
    },
    {
      "name": "model",
      "typeName": "spark_ml_model",
      "isOptional": true,
      "cardinality": "SINGLE",
      "valuesMinCount": -1,
      "valuesMaxCount": -1,
      "isUnique": false,
      "isIndexable": false,
      "includeInNotification": false,
      "searchWeight": -1,
      "relationshipTypeName": "spark_ml_model_dataset",
      "isLegacyAttribute": false
    },
    {
      "name": "meanings",
      "typeName": "array<AtlasGlossaryTerm>",
      "isOptional": true,
      "cardinality": "SET",
      "valuesMinCount": -1,
      "valuesMaxCount": -1,
      "isUnique": false,
      "isIndexable": false,
      "includeInNotification": false,
      "searchWeight": -1,
      "relationshipTypeName": "AtlasGlossarySemanticAssignment",
      "isLegacyAttribute": false
    },
    {
      "name": "outputFromProcesses",
      "typeName": "array<Process>",
      "isOptional": true,
      "cardinality": "SET",
      "valuesMinCount": -1,
      "valuesMaxCount": -1,
      "isUnique": false,
      "isIndexable": false,
      "includeInNotification": false,
      "searchWeight": -1,
      "relationshipTypeName": "process_dataset_outputs",
      "isLegacyAttribute": false
    }
  ],
  "businessAttributeDefs": {}
}

 

As you see the definition, it has two attributes defined.

a.   screen_size

b.   operating_system

 

Now, I want to add one more property ‘configuration’ to the type ‘tablet’.

 

Step 1: Define new attribute definition.

AtlasAttributeDef attributeDef1 = new AtlasAttributeDef();
attributeDef1.setName("configuration");
attributeDef1.setTypeName("string");
attributeDef1.setCardinality(AtlasAttributeDef.Cardinality.SINGLE);
attributeDef1.setIsIndexable(true);
attributeDef1.setIsUnique(false);
attributeDef1.setIsOptional(true);

 

Step 2: Get the tablet entity and attach new attribute to it.

AtlasEntityDef atlasEntityDef = atlasClient.getEntityDefByName("tablet");
atlasEntityDef.getAttributeDefs().add(attributeDef1);

 

Step 3: Persist the changes to Atlas.

AtlasTypesDef atlasTypesDef = new AtlasTypesDef();
atlasTypesDef.getEntityDefs().add(atlasEntityDef);
AtlasTypesDef atlasTypesDefResponse = atlasClient.updateAtlasTypeDefs(atlasTypesDef);

Find the below working application.

 

Step 1: Create atlas-application.properties file under src/main/resources folder.

 

atlas-application.properties

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

Step 2: Define JsonUtil and UpdateType classes.

 

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

UpdateType.java

package com.sample.app.types;

import org.apache.atlas.AtlasClientV2;
import org.apache.atlas.AtlasServiceException;
import org.apache.atlas.model.typedef.AtlasEntityDef;
import org.apache.atlas.model.typedef.AtlasStructDef.AtlasAttributeDef;
import org.apache.atlas.model.typedef.AtlasTypesDef;

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

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

    AtlasEntityDef atlasEntityDef = atlasClient.getEntityDefByName("tablet");

    AtlasAttributeDef attributeDef1 = new AtlasAttributeDef();
    attributeDef1.setName("configuration");
    attributeDef1.setTypeName("string");
    attributeDef1.setCardinality(AtlasAttributeDef.Cardinality.SINGLE);
    attributeDef1.setIsIndexable(true);
    attributeDef1.setIsUnique(false);
    attributeDef1.setIsOptional(true);

    atlasEntityDef.getAttributeDefs().add(attributeDef1);

    AtlasTypesDef atlasTypesDef = new AtlasTypesDef();

    atlasTypesDef.getEntityDefs().add(atlasEntityDef);

    AtlasTypesDef atlasTypesDefResponse = atlasClient.updateAtlasTypeDefs(atlasTypesDef);

    String json = JsonUtil.prettyPrintJson(atlasTypesDefResponse);
    System.out.println(json);

  }
}

Output

{
  "enumDefs" : [ ],
  "structDefs" : [ ],
  "classificationDefs" : [ ],
  "entityDefs" : [ {
    "category" : "ENTITY",
    "guid" : "e7201b2c-a87d-49ef-aac0-c228b92f9763",
    "createdBy" : "admin",
    "updatedBy" : "admin",
    "createTime" : 1644561370044,
    "updateTime" : 1644563715634,
    "version" : 2,
    "name" : "tablet",
    "description" : "Represent a laptoo specification",
    "typeVersion" : "1.0",
    "attributeDefs" : [ {
      "name" : "screen_size",
      "typeName" : "string",
      "isOptional" : false,
      "cardinality" : "SINGLE",
      "valuesMinCount" : 1,
      "valuesMaxCount" : 1,
      "isUnique" : false,
      "isIndexable" : true,
      "includeInNotification" : false,
      "searchWeight" : -1
    }, {
      "name" : "operating_system",
      "typeName" : "string",
      "isOptional" : false,
      "cardinality" : "SINGLE",
      "valuesMinCount" : 1,
      "valuesMaxCount" : 1,
      "isUnique" : false,
      "isIndexable" : true,
      "includeInNotification" : false,
      "searchWeight" : -1
    }, {
      "name" : "configuration",
      "typeName" : "string",
      "isOptional" : true,
      "cardinality" : "SINGLE",
      "valuesMinCount" : 0,
      "valuesMaxCount" : 1,
      "isUnique" : false,
      "isIndexable" : true,
      "includeInNotification" : false,
      "searchWeight" : -1
    } ],
    "superTypes" : [ "DataSet" ],
    "subTypes" : [ ],
    "relationshipAttributeDefs" : [ {
      "name" : "inputToProcesses",
      "typeName" : "array<Process>",
      "isOptional" : true,
      "cardinality" : "SET",
      "valuesMinCount" : -1,
      "valuesMaxCount" : -1,
      "isUnique" : false,
      "isIndexable" : false,
      "includeInNotification" : false,
      "searchWeight" : -1,
      "relationshipTypeName" : "dataset_process_inputs",
      "isLegacyAttribute" : false
    }, {
      "name" : "pipeline",
      "typeName" : "spark_ml_pipeline",
      "isOptional" : true,
      "cardinality" : "SINGLE",
      "valuesMinCount" : -1,
      "valuesMaxCount" : -1,
      "isUnique" : false,
      "isIndexable" : false,
      "includeInNotification" : false,
      "searchWeight" : -1,
      "relationshipTypeName" : "spark_ml_pipeline_dataset",
      "isLegacyAttribute" : false
    }, {
      "name" : "schema",
      "typeName" : "array<avro_schema>",
      "isOptional" : true,
      "cardinality" : "SET",
      "valuesMinCount" : -1,
      "valuesMaxCount" : -1,
      "isUnique" : false,
      "isIndexable" : false,
      "includeInNotification" : false,
      "searchWeight" : -1,
      "relationshipTypeName" : "avro_schema_associatedEntities",
      "isLegacyAttribute" : false
    }, {
      "name" : "model",
      "typeName" : "spark_ml_model",
      "isOptional" : true,
      "cardinality" : "SINGLE",
      "valuesMinCount" : -1,
      "valuesMaxCount" : -1,
      "isUnique" : false,
      "isIndexable" : false,
      "includeInNotification" : false,
      "searchWeight" : -1,
      "relationshipTypeName" : "spark_ml_model_dataset",
      "isLegacyAttribute" : false
    }, {
      "name" : "meanings",
      "typeName" : "array<AtlasGlossaryTerm>",
      "isOptional" : true,
      "cardinality" : "SET",
      "valuesMinCount" : -1,
      "valuesMaxCount" : -1,
      "isUnique" : false,
      "isIndexable" : false,
      "includeInNotification" : false,
      "searchWeight" : -1,
      "relationshipTypeName" : "AtlasGlossarySemanticAssignment",
      "isLegacyAttribute" : false
    }, {
      "name" : "outputFromProcesses",
      "typeName" : "array<Process>",
      "isOptional" : true,
      "cardinality" : "SET",
      "valuesMinCount" : -1,
      "valuesMaxCount" : -1,
      "isUnique" : false,
      "isIndexable" : false,
      "includeInNotification" : false,
      "searchWeight" : -1,
      "relationshipTypeName" : "process_dataset_outputs",
      "isLegacyAttribute" : false
    } ],
    "businessAttributeDefs" : { }
  } ],
  "relationshipDefs" : [ ],
  "businessMetadataDefs" : [ ]
}

As you see the output, you can confirm that new attribute ‘configuration’ is added.

 

Note

a.   You can’t add a mandatory attribute to the existing type.

 


 

Previous                                                    Next                                                    Home

HIVE: Data types

Hive data types are categorized into two types.

a.   Primitive types

b.   Complex types

 

Following table summarizes primitive types.

 

Data type

Description

Example

TINYINT

Occupies 1 byte. Minimum value is -128 and maximum value is 128. Postfix is Y.

123Y

SMALLINT

Occupies 2 bytes. Minimum value is -32768 and maximum value is 32767. Postfix is S.

12345S

INT

Occupies 4 bytes. Minimum value is -2,147,483,648 and maximum value is 2,147,483,647.

12345678

BIGINT

Occupies 8 bytes. Minimum value is 9,223,372,036,854,775,808 and maximum value is 9,223,372,036,854,775,807. Postfix is L.

123L

FLOAT

4 bytes single precision number.

1.2345

DOUBLE

8 bytes double precision number

1.2345

BOOLEAN

Represent TRUE or FALSE value

TRUE

STRING

Collection of characters represented in either single quotes or double quotes. Maximum size is around 2GB.

'hello wolrd', "Hello World"

CHAR

Maximum length is 255 characters

'hello wolrd', "Hello World"

VARCHAR

Maximum length is 65535 characters.

'hello wolrd', "Hello World"

DATE

Specify year, month, and day in the format of YYYY-MM-DD. The range of dates is from 0000-01-01 to 9999-12-31.

2020-01-08

TIMESTAMP

This describes a specific year, month, day, hour, minute, second, and millisecond in the format of YYYY-MM-DD HH:MM:SS.fff.

2020-01-08 111.23.34.123

 

Example

create table numeric_data_types_demo(
a TINYINT,
b SMALLINT,
c INT,
d BIGINT,
e FLOAT,
g DOUBLE,
h BOOLEAN,
i STRING,
j CHAR(10),
k VARCHAR(123),
l DATE,
m TIMESTAMP,
n DECIMAL,
o DECIMAL(10, 2)
)

hive> create table numeric_data_types_demo(
    > a TINYINT,
    > b SMALLINT,
    > c INT,
    > d BIGINT,
    > e FLOAT,
    > g DOUBLE,
    > h BOOLEAN,
    > i STRING,
    > j CHAR(10),
    > k VARCHAR(123),
    > l DATE,
    > m TIMESTAMP,
    > n DECIMAL,
    > o DECIMAL(10, 2)
    > );
OK
Time taken: 0.061 seconds

 

Get the columns and their respective data types information.

hive> describe numeric_data_types_demo;
OK
a                       tinyint                                     
b                       smallint                                    
c                       int                                         
d                       bigint                                      
e                       float                                       
g                       double                                      
h                       boolean                                     
i                       string                                      
j                       char(10)                                    
k                       varchar(123)                                
l                       date                                        
m                       timestamp                                   
n                       decimal(10,0)                               
o                       decimal(10,2)                               
Time taken: 0.064 seconds, Fetched: 14 row(s)

 

Following table summarizes the complex types supported in HIVE

 

Data type

Description

Example

ARRAY

List of items of same type

[1, 2, 3, 4]

[1.2, 3.4, 5.6]

MAP

Represent set of key,value pairs

{1 : "Krishna", 2 : "Ram"}

STRUCT

Collection of elements of different type.

Address struct<city:string,state:string,country:string,pin:bigint>

 

 

Previous                                                    Next                                                    Home