Weather

HIVE SCDTYPE II implementation based on stage table in MYSQL .(using SQOOP incremental load)

 HIVE SCDTYPE II implementation based on stage table in MYSQL .(using SQOOP incremental load) explanation

--------------------
In MYSQL TABLE

mysql>describe pt_adt_info_stg;(this is stage table of scdtype-2 implementation in mysql)

 pt_adt_info_stg_id              int (primary key in Mysql table)
pt_adt_admission_date        timestamp(admission date of patient)
 pt_adt_inventory_id            int
 pt_adt_patient_id                int
pt_adt_discharge_date         timestamp(discharge date of patient)
 pt_adt_staff_id                     int
pt_adt_last_update                timestamp(timestamp when record is modified in table)

------------------------------------------
To set unixtime with any field
hive> Select t.*, from_unixtime(unix_timestamp())  from temp_filmdet t;

//from_unixtime(bigint unixtime[, string format]) is a built in date function in hive ,returns string
//unix_timestamp() built in function in hive gets current UNIX timestamp in seconds

STEP 1:

 sqoop incremental update ( to find out new records based on pt_adt_info_stg_id )

go to $SQOOP_HOME:

 $SQOOP_HOME/bin> ./sqoop import --connect jdbc:mysql://191.160.463.1/ptdb
 --table pt_adt_info_stg
--username biadmin  --password biadmin
 --target-dir /user/hadoop/admissionnew
 --check-column "pt_adt_info_stg_id" 
--incremental append 
--last-value 2000000 -m 3

Note: where new admission data in "/user/hadoop/admissionnew"(when ever a  new admission  incident occurs then the new data for Id field gets inserted into table and data in pt_adt_discharge_date field is open ) 

STEP 2

$SQOOP_HOME/bin>./sqoop import --connect jdbc:mysql://192.168.56.1/ptdb  --table pt_adt_info_stg --username biadmin --password biadmin
 --target-dir /user/hadoop/admissionlastmod
 --check-column "last_update" --incremental lastmodified 
 --last-value "2016-01-25 11:51:25" 
--where " pt_adt_admission_date not null and pt_adt_discharge_date is not null" -m 3

Note: where discharge_date is set after admission_date, 2) data in hdfs file in path
"/user/hadoop/admissionlastmod"                           

STEP 3:

 The previous history data that should be updated in table (updating previous id's where changes did happen). Above using sqoop we imported the record into HDFS file named "admissionlastmod"in above step 2


hive:>create external table pt_adt_stage
(
pt_adt_info_stg_id              int
pt_adt_admission_date        timestamp
 pt_adt_inventory_id            int
 pt_adt_patient_id                int
pt_adt_discharge_date         timestamp
 pt_adt_staff_id                     int
pt_adt_last_update                timestamp
 )
     row format delimited
     fields terminated by ','
    stored as textfile
     location '/user/hadoop/admissionlastmod';

STEP 4:

Original table in hive: with scdtype 2 implementation is pt_adt_info (already present in hive with SCD-type2 implementation with history of data stored in it)


$HIVE_HOME/bin:> hive (to go to hive shell)

hive:>describe pt_adt_info(shows the structure of the table that exist with data in hive)

details are
pt_adt_info_stg_id              int
pt_adt_admission_date        timestamp
 pt_adt_inventory_id            int
 pt_adt_patient_id                int
pt_adt_discharge_date         timestamp
 pt_adt_staff_id                     int
pt_adt_last_update                timestamp

     partitioned by(scdtype2 timestamp),stored as textfile, location '/user/hadoop/pt_adt_info_org/';

execute describe pt_adt_info to know the structure of the table

STEP 5:

Creating a new table with scdtype 2 implementation
 hive:>
create external table pt_adt_info_20150120
(
pt_adt_info_stg_id              int
pt_adt_admission_date        timestamp
 pt_adt_inventory_id            int
 pt_adt_patient_id                int
pt_adt_discharge_date         timestamp
 pt_adt_staff_id                     int
pt_adt_last_update                timestamp
 )
     partitioned by(scdtype2 timestamp)
     row format delimited
     fields terminated by ','
    stored as textfile
     location '/user/hadoop/pt_adt_info_20150120/';
------------------------------------------------------------

Step 6

Loading the history data rows  from Original table (pt_adt_info) which does have any modifications into pt_adt_info_20150120 (work around)

hive> insert into table  pt_adt_info_20150120                                         
     partition(scdtype2='2015-01-19 11:51:25')                         
    select pto. pt_adt_info_stg_id ,
 pto.pt_adt_admission_date,
pto.pt_adt_inventory_id , pto.pt_adt_patient_id ,
pto.pt_adt_discharge_date, pto. pt_adt_staff_id ,
pto.pt_adt_last_update
from pt_adt_info pto where
pto. pt_adt_info_stg_id not in (select pstg.pt_adt_info_stg_id  from pt_adt_stg pstg).

Step 7

 loading the data that changed (means updated records) into table pt_adt_info_20150120(We have this data loaded in staging table pt_adt_stage)

hive> 
insert into table  pt_adt_info_20150120                                         
     partition(scdtype2='2015-01-19 11:51:25')                         
    select stg. pt_adt_info_stg_id , stg.pt_adt_admission_date,
stg.pt_adt_inventory_id , stg.pt_adt_patient_id ,
stg.pt_adt_discharge_date,stg. pt_adt_staff_id ,
stg.pt_adt_last_update  from pt_adt_stage stg

Step 8:

Loading    where new admission data in "/user/hadoop/admissionnew"(where new admission data exists with open pt_adt_discharge_date) 

 Hive>load data inpath '/user/hadoop/admissionnew' into table pt_adt_info_20150120

partition(scdtype2='2015-01-19 11:51:25');

-----------------------------------
Finally loaded all the data into  pt_adt_info_20150120 in three stages
 1) loading the records data that does not had any changes in STEP 6
 2)Loading the  updated data only from staging table in STEP 7
3) Loading the new records  data in STEP 8

Importing a table from teradata into hadoop hdfs /hive using TDCH command line interface .(In two different environments)

 Explanation of importing table from Teradata into Hive, Hdfs

-----------------------------------------
a)Teradata database in 192.168.229.130  and Cloudera /hortonworks is in different operating system.
b) Teradata connector for hadoop is installed in Cloudera where library files are stored in lib folder and configuration files "*.xml" stored in hadoop home($HADOOP_HOME) "CONF" directory.

c)teradata-export-properties.xml.template
teradata-import-properties.xml.template

d)tdgssconfig.jar , teradata-connector-1.4.1.jar,  terajdbc4.jar in lib folder of tdch .

e) ConnectorImportTool for import jobs
f)ConnectorExportTool  for export jobs
g)ConnectorPluginTool is also another method to do the desired import/export where the job is identified by
command line parameters sourceplugin/targetplugin and plugin-in specific parameters identified by -D< ..> option
 
Step 1:
cli>export TDCH_JAR=/usr/lib/tdch/1.4/lib/teradata-connector-1.4.1.jar

 cli>
hadoop jar $TDCH_JAR com.teradata.connector.common.tool.ConnectorImportTool 
-classname com.teradata.jdbc.TeraDriver 
-url jdbc:teradata://192.168.229.130/DATABASE=dbc
-sourcetable tables
-username dbc -password dbc
-jobtype hdfs -fileformat textfile
-method split.by.hash
-separator ","
 -splitbycolumn DatabaseName  
-targetpaths /user/hadoop/sales_transaction

NOTES: 
Database in teradata : DBC
(sourcetable)Table name in teradata : tables
Username for teradata : dbc
Password for teradata : dbc
jobtype : HDFS (where the file has to be imported to hdfs that is the reason the job type is hdfs)
Target paths : hdfs path where the file has to be stored.
seperator: the field names data is seperated by ',' in hdfs
fileformat : the import file format has to be stored as textfile
method : split.by.hash
splitbycloumn: in table which column you want to split data by for the mapper/reducer(to identify as key)






Ouput "n" no of part files instead of one.

Splitting a huge GB file into smaller part files for each accessing and export operations.(hands on example)

Difference in two BAG elements comparision/Two fields comparison when not in a BAG:DIFF()

Explanation:

DIFF takes two bags as arguments and compares them. Any tuples that are in one bag but not the other are returned. 

- If the fields are not bags then they will be returned if they do not match,

 -or an empty bag will be returned if the two records match.

The implementation assumes that both bags being passed to this function will fit entirely into memory simultaneously. If that is not the case the UDF will still function, but it will be very slow.

Counting records with nulls, and counting without nulls.COUNT_STAR(),COUNT()


Pig COUNT() And COUNT_STAR() hands on example

SUBSTRING() of a String in pig

In Pig Starting from count of zero as position value will fetch the length of five as given above.