Thursday, June 22, 2017

Java-Features of java

Features  of java
-----------------
0)opensource:
    java is open source ,we can use java s/w for free of cost.

i)Platform independent
----------------------
*java is platform independent because we can execute .class[bytecode] of one os on any other os.

2)Object Oreinted
-----------------
Java is object oriented because it consists of objects and classes.

3)Portable
----------
*Bothe java s/w and java bytecode can be executed on different os's so java is portable.


4)simple
--------
Java is developed from c and c++,so learning java is simple.

5)robust[strong)
---------------
*Java programs do not crash easily because of exception handling mechanism so java is robust.

6)interpreted
--------------
As jvm interprets bytecode to machine code,java is called interpreted language.

7)secured
---------
Java is secured because virus don't attack .class files,also java is robust.

*No pointers in java.

*Java has both comiple time checks and run time checks

8)distributed
--------------
we can run java programs on different os's so we can say java is distributed.

9)Multithreaded
---------------
Thread
------
*Thread is an independent part of a program and a light weight process.

*Multithreading is group of threads executing parallely to accomplish a task.

Java-Basics

Platform
--------
*A platform is an environment where we can execute different programs.

ex:
  os(windows,linux,unix etc)
  technology(java,.net etc)

Platform Dependent
------------------
*If application of one operating system cannot be executed on other operating system such kind of application is called as platform dependent.
ex:
 c/c++ Applications

Platform Independent
--------------------
If  application of one os can be executed on other os such kind of applications are called platform independent.
ex:
Java applications


History of java
---------------
*In early 1990's sunmicrosystems was developing a software for electronic devices.


*At that time c/c++ are the most powerful languages but they are platform dependent.

*so sunmicrosystems started to develop a new programming language which is platform independent under the guidance of James Gosling.

*The name of the project is Green Project.

*By 1992 they had developed a platform independent language.

*Initially it is named as oak

*In 1995 oak is renamed to java due to patent issue.


Monday, June 19, 2017

Hive-eliminating duplicates and unins

Eliminating Duplicates and Unions (merging)


[training@localhost ~]$ cat dupes
101,aaa,10000
101,bbb,20000
101,aaa,10000
101,aaa,10000
101,aaa,10000
102,bbb,40000
103,cccc,50000
102,bbb,40000
102,bbb,40000
[training@localhost ~]$ 

hive> create database hdp;
OK
Time taken: 1.702 seconds
hive> use hdp;
OK
Time taken: 0.018 seconds
hive> create table info(id int, name string, 
    >   sal int)
    >  row format delimited fields terminated 
    >  by ',';
OK
Time taken: 0.439 seconds
hive> 

hive> load data local inpath 'dupes'
    >  into table info;
Copying data from file:/home/training/dupes
Copying file: file:/home/training/dupes
Loading data to table hdp.info
OK
Time taken: 0.209 seconds
hive> select * from info;
OK
101     aaa     10000
101     bbb     20000
101     aaa     10000
101     aaa     10000
101     aaa     10000
102     bbb     40000
103     cccc    50000
102     bbb     40000
102     bbb     40000
Time taken: 0.201 seconds
hive> 
hive> select distinct(id),name,sal
    >   from info;


101     aaa     10000
101     bbb     20000
102     bbb     40000
103     cccc    50000

way2)
hive> select id,name,sal           
    >   from info           
    >  group by id,name,sal;

101     aaa     10000
101     bbb     20000
102     bbb     40000
103     cccc    50000

Way3)
 if table has so many columns

 hive> create table dummy(line string);
 hive> load data inpath
  '/user/hive/warehouse/hdp.db/info/dupes'
  into table dummy;
 hive> insert overwrite table dummy
    select line from dummy group by line;

 hive> load data inpath
  '/user/hive/warehouse/hdp.db/dummy/000000_0'
  into table info;

  hive> drop table dummy;

________________________________________

 hive> select * from emp;
OK
101     vino    26000   m       11
102     Sri     25000   f       11
103     mohan   13000   m       13
104     lokitha 8000    f       12
105     naga    6000    m       13
101     janaki  10000   f       12
201     aaa     30000   m       12
202     bbbb    50000   f       13
203     ccc     10000   f       13
204     ddddd   50000   m       13
304     xxx     70000   m       14
305     yyy     80000   f       15
Time taken: 0.053 seconds
hive> 


hive> create table todayemp(id int, name string, 
    >  sal int, sex string, dno int) 
    > row format delimited
    >   fields terminated by ',';
OK
Time taken: 0.031 seconds
hive> load data local inpath 'emp2'
    >  into table todayemp;
Copying data from file:/home/training/emp2
Copying file: file:/home/training/emp2
Loading data to table hdp.todayemp
OK
Time taken: 0.095 seconds
hive> select * from todayemp;
OK
401     aaa     50000   m       11
402     bbbbbb  60000   f       12
403     cc      90000   m       11
Time taken: 0.051 seconds
hive> 


appending  one table data another table data.


way1)
  insert overwrite table emp
    select * from (
      select * from emp
            union all
      select * from todayemp) e;


 -- if first table has 1lakh and 2nd has 10 rows, just to 10 rows, hive is scanning 1lakh+10 rows.  [ bad ]

 way2).

  if delimiters of both tables is same.


  hadoop fs -cp /user/hive/warehouse/hdp.db/todayemp/emp2   /user/hive/warehouse/hdp.db/emp
   
_______________________________________




 union all:

    if table schema is different.


  tab1 --> id name  sal   sex dno
 tab2  ---> id name dno sex sal

  select * from (
      select id, name,sal,sex,dno from tab1
           union all
   select id, name,sal,sex,dno from tab2 ) t;


________________

 if tables have different columns.

 tab1 --> id, name,  sal, sex , dno , desig

 tab2 --> id, name, income, gender, city



  select * from (
    select id, name, sal, sex, dno, desig, 'neevuru' as city from  tab1
           union all
 select id,name,income as sal, gender as sex,
   0 as dno , 'nopani' as desig, city 
    from tab2 ) t;

__________________________________________

Hive-Partition tables(A simple Case)

[training@localhost ~]$ cat trans
01/01/2010,30000
01/07/2010,40000
01/08/2010,30000
01/26/2010,40000
02/01/2010,30000
02/07/2010,40000
02/09/2010,30000
02/28/2010,40000
03/01/2010,80000
03/07/2010,40000
04/01/2010,30000
04/17/2010,70000
04/18/2010,80000
04/26/2010,90000
05/01/2010,10000
05/07/2010,20000
05/08/2010,30000
05/26/2010,60000
06/01/2010,30000
06/17/2010,50000
07/01/2010,30000
07/07/2010,40000
08/08/2010,10000
08/26/2010,20000
09/01/2010,90000
09/07/2010,40000
09/18/2010,30000
09/30/2010,40000
10/01/2010,30000
10/07/2010,40000
10/08/2010,30000
11/26/2010,40000
11/01/2010,30000
12/07/2010,40000
12/08/2010,30000
12/26/2010,40000
[training@localhost ~]$ 

hive> create table sales(dt string, 

amt int)
    >  row format delimited
    >   fields terminated by ',';
OK
Time taken: 0.046 seconds
hive> load data local inpath 'trans'
    >  into table sales;
Copying data from 

file:/home/training/trans
Copying file: 

file:/home/training/trans
Loading data to table cases.sales
OK
Time taken: 0.194 seconds
hive>

 insert overwrite table sales2
     select * from (
        select dt, amt from sales
          union all
   select concat(substr(dt,1,9),"1") 

as dt,
    amt+1000 as amt  from sales 
    union all
  select concat(substr(dt,1,9),"2") as 

dt,
  amt+3000  as amt from sales         

union all
  select concat(substr(dt,1,9),"3") as 

dt,
  amt+5000 as amt from sales          

union all
  select concat(substr(dt,1,9),"4") as 

dt,
   amt+10000 as amt from sales ) x;    
hive> create table sales02(dt 

array<string>,
    >     amt int);
OK
Time taken: 0.042 seconds
hive> insert overwrite table sales02
    >   select split(dt,'/'), amt from 

sales2;

hive> select * from sales02 limit 5;
OK
["01","01","2010"]      30000
["01","01","2013"]      35000
["01","01","2011"]      31000
["01","01","2014"]      40000
["01","01","2012"]      33000
Time taken: 0.083 seconds
hive> 
hive> create table sales3 like sales;
hive> insert overwrite table sales3
    >   select concat(dt[2],'-',dt

[0],'-',dt[1]),
    >  amt from sales02;

hive> create table salespart(dt 

string,
    >    amt int)      
    > partitioned by (y int, m int, d 

int) 
    > row format delimited           
    >   fields terminated by ',';
OK
Time taken: 0.034 seconds
hive> set 

hive.exec.dynamic.partition=true;
hive> set 

hive.exec.dynamic.partition.mode=nonst

rict;
hive> set 

hive.exec.max.dynamic.partitions.perno

de=10000;
hive> set 

hive.exec.max.dynamic.partitions=10000

0;


hive> insert overwrite table salespart
      partition(y,m,d)
      select dt, amt, year(dt), month

(dt),
       day(dt) from sales3;

-- to access a perticular day

hive> select * from salespart
   where y=2010 and m=1 and d=8; --> 1 

part

-- to access a perticular month.
hive> select * from salespart
   where y=2010 and m=1; ---> 31 parts

-- to access a year.

hive> select * from salespart
   where y=2014; ---> 366 part

---> range of dates.
Hive> select * from salespart
   where (y=2002 and m>=5)
             or
         (y>2002 and y<2009)
              or
         (y=2009 and m<=6);

Hive-Xml data Processing2

hive> create database xxmls;
OK
Time taken: 0.182 seconds

hive> use xxmls;
OK
Time taken: 0.017 seconds

[training@localhost ~]$ cat xml5
<tr><cid>101</cid><pr>3000</pr><qnt>3</qnt><pr>5000</pr><qnt>2</qnt><pr>1000</pr><qnt>5</qnt></tr>
<tr><cid>102</cid><pr>2000</pr><qnt>5</qnt><pr>5000</pr><qnt>2</qnt></tr>
<tr><cid>101</cid><pr>6000</pr><qnt>5</qnt></tr>

[training@localhost ~]$
hive> create table raw(line string);
OK
Time taken: 0.252 seconds

hive> load data local inpath 'xml5'
    >  into table raw;

Copying data from file:/home/training/xml5
Copying file: file:/home/training/xml5
Loading data to table xxmls.raw
OK
Time taken: 0.178 seconds

hive> select * from raw;
OK
<tr><cid>101</cid><pr>3000</pr><qnt>3</qnt><pr>5000</pr><qnt>2</qnt><pr>1000</pr><qnt>5</qnt></tr>
<tr><cid>102</cid><pr>2000</pr><qnt>5</qnt><pr>5000</pr><qnt>2</qnt></tr>
<tr><cid>101</cid><pr>6000</pr><qnt>5</qnt></tr>
Time taken: 0.155 seconds

hive> create table raw2(cid int,
    > pr array<string>, qnt array<string>);
OK
Time taken: 0.069 seconds

hive> insert overwrite table raw2
    >   select xpath_int(line,'tr/cid'),
    >     xpath(line,'tr/pr/text()'),
    >     xpath(line,'tr/qnt/text()')
    > from raw;

hive> create table cidpr(cid int , pr int);

hive> insert overwrite table cidpr
    >   select cid, mypr from raw2
    >    lateral view explode(pr) p as mypr;

hive> create table cidqnt(cid int, qnt int);

hive> insert overwrite table cidqnt
    >    select cid, myqnt from raw2
    >   lateral view explode(qnt) q as myqnt;


hive> select * from cidpr;
OK
101     3000
101     5000
101     1000
102     2000
102     5000
101     6000
Time taken: 0.06 seconds
hive> select * from cidqnt;
OK
101     3
101     2
101     5
102     5
102     2
101     5
Time taken: 0.046 seconds
hive>


hive> add jar Desktop/hivejars.jar;

hive> create temporary function auto
    >  as 'hive.analytics.SeqNumber';


hive> alter table cidpr add columns(n int);

hive> insert overwrite table cidpr
select cid, pr, auto() from cidpr;

hive> alter table cidqnt add columns(n int);

hive> insert overwrite table cidqnt
select cid, qnt, auto() from cidqnt;

hive> create table trans(cid int, pr int,
             qnt int, bill int);

hive> insert overwrite table trans
select l.cid, pr, qnt, pr*qnt
from cidpr l join cidqnt r
on (l.n=r.n);

hive> create table report(cid int, totbill int);

hive> insert overwrite table report
    select cid, sum(bill) from trans
        group by cid;

hive> select * from report;
________________________

hive.analytics.SeqNumber.java
______________________
package hive.analytics;

import java.io.IOException;
import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.io.IntWritable;

public class SeqNumber extends UDF
{
  int cnt = 0;
 public IntWritable  evaluate()
 throws IOException
 {
  cnt++;
  return  new IntWritable(cnt);
 }


}

______________________________

keep all above statements into a script file.
 
hscript1.hql
________________

create database urheroes;

use urheroes;

create table raw(line string);

load data local inpath 'xml5' into table raw;

create table raw2(cid int, pr array<string>, qnt array<string>);

insert overwrite table raw2
select xpath_int(line,'tr/cid'),
xpath(line,'tr/pr/text()'),
xpath(line,'tr/qnt/text()') from raw;

create table cidpr(cid int, pr int);

insert overwrite table cidpr
select cid, mypr from raw2
lateral view explode(pr) p as mypr;

create table cidqnt(cid int, qnt int);

insert overwrite table cidqnt
select cid, myq from raw2
lateral view explode(qnt) q as myq;

alter table cidpr add columns(n int);
alter table cidqnt add columns(n int);

add jar Desktop/hivejars.jar;

create temporary function auto
as 'hive.analytics.SeqNumber';

insert overwrite table cidpr
select cid, pr, auto() from cidpr;

insert overwrite table cidqnt
select cid, qnt, auto() from cidqnt;

create table trans(cid int, pr int, qnt int,
bill int);

insert overwrite table trans
select l.cid, pr, qnt, pr*qnt
from cidpr l join cidqnt r
 on (l.n=r.n);

create table report(cid int, totbill int);

insert overwrite table report
   select cid, sum(bill) from trans
    group by cid;

select * from trans;
select * from report;

drop table raw;
drop table raw2;
drop table cidpr;
drop table cidqnt;
________________________

Hive-xml data processing

[training@localhost ~]$ cat sampxml1
<rec>
  <name>Manohar</name>
  <age>34</age>
  <sex>m</sex>
</rec>
<rec>
  <name>vidhatri</name>
  <sex>f</sex>
  <city>Hyd</city>
</rec>
[training@localhost ~]$

hive> create table samp(line string);
OK
Time taken: 0.309 seconds

hive> load data local inpath 'sampxml1'
    >  into table samp;
Copying data from file:/home/training/sampxml1
Copying file: file:/home/training/sampxml1
Loading data to table ourxml.samp
OK
Time taken: 0.202 seconds
hive>

hive> select * from samp;
OK
<rec>
  <name>Manohar</name>
  <age>34</age>
  <sex>m</sex>
</rec>
<rec>
  <name>Vidhatri</name>
  <sex>f</sex>
  <city>Hyd</city>
</rec>
Time taken: 0.184 seconds
hive>

above xml file has 2 records,
when this file is loaded into hive table, hive treats, each line as a record,
as per xml == two records.
as per hive --> 10 records(rows.).

this data is not ready for hive,
hive expects total xml record in a single line.
so data preperation step is required.

Prepare a mapreduce Job , which can convert xml record into single row.

(Later we see in Mapreduce session).,

[training@localhost ~]$ cat xml1
<rec><name>Manohar</name><age>25</age><city>hyd</city></rec>
<rec><name>Vidhatri</name><age>24</age><sex>f</sex></rec>
<rec><name>Vijay</name><sex>m</sex><city>Del</city></rec>
[training@localhost ~]$


hive> create table raw(line string);
OK
Time taken: 0.05 seconds
hive> load data local inpath 'xml1'
    >  into table raw;
Copying data from file:/home/training/xml1
Copying file: file:/home/training/xml1
Loading data to table ourxml.raw
OK
Time taken: 0.123 seconds
hive>


hive> select * from raw;
OK
<rec><name>Manohar</name><age>34</age><city>hyd</city></rec>
<rec><name>Vidhatri</name><age>1</age><sex>f</sex></rec>
<rec><name>vijay</name><sex>m</sex><city>Del</city></rec>
Time taken: 0.066 seconds
hive> select count(*) from raw;
3
-- 3 rows.

hive> select xpath_string(line,'rec/name') from raw;
Manohar
Vidhatri
Vijay

hive> select
    >  xpath_string(line,'rec/name'),
    >  xpath_int(line,'rec/age'),
    >  xpath_string(line,'rec/sex'),
    >  xpath_string(line,'rec/city')
    > from raw;

Manohar    25              hyd
Vidhatri   24      f
Vijay      0       m       Del

if string fields is missed, it returns blank string, if numeric field is missed it returns 0.
_________________

xml with nested tags.

[training@localhost ~]$ cat xml2
<rec><name><fname>Manohar</fname><lname>papasani</lname></name><age>34</age><contact><email><personal>mano@gmail.com</personal><official>mano@infy.com</official></email><phone><mobile>12345</mobile><office>12346</office><residence>12347</residence></phone></contact></rec>

[training@localhost ~]$

hive> create table xraw(line string);
OK
Time taken: 0.075 seconds
hive> load data local inpath 'xml2'
    >  into table xraw;

hive> select * from xraw;
OK
<rec><name><fname>Manohar</fname><lname>Papasani</lname></name><age>34</age><contact><email><personal>mano@gmail.com</personal><official>mano@infy.com</official></email><phone><mobile>12345</mobile><office>12346</office><residence>12347</residence></phone></contact></rec>
Time taken: 0.064 seconds
hive>

hive> create table info(fname string,
    >   lname string,
    > age int, personal_email string,
    >  official_email string,
    >  mobile string, office string,
    >  residence string);
OK
Time taken: 0.042 seconds
hive>

hive> insert overwrite table info
    >   select
    > xpath_string(line,'rec/name/fname'),
    > xpath_string(line,'rec/name/lname'),
    > xpath_int(line,'rec/age'),        
    > xpath_string(line,'rec/contact/email/personal'),
    > xpath_string(line,'rec/contact/email/official'),
    > xpath_string(line,'rec/contact/phone/mobile'),
    > xpath_string(line,'rec/contact/phone/office'),
    > xpath_string(line,'rec/contact/phone/residence')
    >  from xraw;

hive> select * from info;
OK
Manohar    Papasani   25      mano@gmail.com  mano@infy.com     12345   12346   12347
Time taken: 0.064 seconds
hive>
_____________

xml with collections.


[training@localhost ~]$ cat xml3
<rec><name>Manohar</name><qual>Btech</qual><qual>Mtech</qual></rec>
<rec><name>Vidhatri</name><qual>Bsc</qual><qual>Msc</qual><qual>Mtech</qual></rec>
<rec><name>vijay</name><qual>Btech</qual><qual>Mba</qual></rec>

[training@localhost ~]$

hive> create table yraw(line string);
OK
Time taken: 0.043 seconds
hive> load data local inpath 'xml3'
    >  into table yraw;

hive> select * from yraw;
<rec><name>Manohar</name><qual>Btech</qual><qual>Mtech</qual></rec>
<rec><name>Vidhatri</name><qual>Bsc</qual><qual>Msc</qual><qual>Mtech</qual></rec>
<rec><name>Vijay</name><qual>Btech</qual><qual>Mba</qual></rec>

hive>
hive> create table raw2(name string,
    >    qual  array<string>);
OK
Time taken: 0.039 seconds
hive> insert overwrite table raw2
    >   select xpath_string(line,'rec/name'),
    >  xpath(line,'rec/qual/text()')
    >   from yraw;

hive> select * from raw2;
OK
Manohar    ["Btech","Mtech"]
Vidhatri   ["Bsc","Msc","Mtech"]
Vijay   ["Btech","Mba"]
Time taken: 0.061 seconds
hive>

hive> select name, size(qual) from raw2;

Manohar    2
Vidhatri   3
Vijay   2

how to access array elements,

 by using index numbers

 indexing starts from 0.

hive> select qual[0], qual[1],
          qual[2] from raw2;
Btech   Mtech   NULL
Bsc     Msc     Mtech
Btech   Mba     NULL

search for elements with in array.

hive> select * from raw2
    >  where array_contains(qual,'Mtech');


Manohar    ["Btech","Mtech"]
Vidhatri   ["Bsc","Msc","Mtech"]

_______________
Flattening Array elements:

hive> select explode(qual) as q
   from raw2;

Btech
Mtech
Bsc
Msc
Mtech
Btech
Mba


hive> select name, explode(qual) as q from raw2;
FAILED: Error in semantic analysis: UDTF's are not supported outside the SELECT clause, nor nested in expressions
hive>

-- above statement is invalid,
   bcoz, udtf s can not be applied with other column expressions.

hive> create table  yinfo(name string,
    >      qual string);
OK
Time taken: 0.035 seconds
hive> insert overwrite table yinfo
    >   select name, myq from raw2
    >    lateral view explode(qual) q as myq;

hive> select * from yinfo;
OK
Manohar    Btech
Manohar    Mtech
Vidhatri   Bsc
Vidhatri   Msc
Vidhatri   Mtech
Vijay   Btech
Vijay   Mba
Time taken: 0.055 seconds

hive> select * from yinfo  
    >   where qual in ('Msc','Mtech');


Manohar    Mtech
Vidhatri   Msc
Vidhatri   Mtech

hive> create table yres(qual string, cnt int);
OK
Time taken: 0.036 seconds
hive> insert overwrite table yres
    >   select qual, count(*) from yinfo
    >    group by qual;

hive> select * from yres;
OK
Bsc     1
Btech   2
Mba     1
Msc     1
Mtech   2
Time taken: 0.051 seconds
hive>

____________________

Assignment:

[training@localhost ~]$ cat xml4
<tr><cid>101</cid><pr>1000</pr><pr>3000</pr></tr>
<tr><cid>102</cid><pr>1200</pr><pr>2000</pr><pr>5000</pr></tr>
<tr><cid>101</cid><pr>4000</pr></tr>
[training@localhost ~]$

hive> create database sales;
hive> use sales;
hive> create table raw(line string);
hive> load data local inpath 'xml4'
   into table raw;
hive> create table raw2(cid string,
    pr array<string>);

hive> insert overwrite table raw2
   select xpath_string(line,'tr/cid'),
   xpath(line,'tr/pr/text()')
    from raw;
hive> create table raw3(cid string,
   pr int);
hive> insert overwrite table raw3
    select cid, mypr from  raw2
   lateral view explode(pr) p as mypr;

hive> create table results(cid string,
   totbill int);
hive> insert overwrite table results
   select cid, sum(pr)
   from raw3
   group by cid;

hive> select * from results;

--- Manohar students are the best -- 

Hive-Json Procession[semi structured data]

A json is a



{
  "name":"manohar",
  "gender":"male",
  "color":"white",
  "age":34
}

[cloudera@quickstart ~]$ cat >  json1
{"name":"manohar","gender":"male","age":"34"}
{"name":"vidhatri","gender":"female","age":"1","city":"nellore"}
^C

hive> create database jsondatabase;
hive> use jsondatabase;
hive> create table person(line string);
hive> load data local inpath 'json1' into table person;
hive> select *from person;

hive:get_json_object
--------------------
This method is used to select a property/node from a json file/record in a hive table.

It takes 2  parameters
i)colname
ii)propertyname

syntax
------
get_json_object(colname,'propertyname');

$-->It is rootnode of json record.

hive> select get_json_object(line,'$.name') from person;

hive> select
    > get_json_object(line,'$.name'),
    > get_json_object(line,'$.age'),
    > get_json_object(line,'$.gender'),
    > get_json_object(line,'$.city')
    > from person;

converting semistructured to structured in hive
-----------------------------------------------
we can split the properties of a json into cols of a hive table,which makes json strcuctured.

hive> create table personal_details(name string,age int,gender string,city string);

hive> insert overwrite table personal_details
    > select
    > get_json_object(line,'$.name')
    > ,get_json_object(line,'$.age'),
    > get_json_object(line,'$.gender'),
    > get_json_object(line,'$.city')
    > from person;


hive:json_tuple
---------------
A json_tuple is used to split raw data into cols/properties.

hive> create table personal_details(name string,gender string,age int,city string);

hive>insert ....
   >select alisname.* from tablename
    lateral view
    json_tuple(colname,'prop1,'prop2',.....)
    aliasname as name1,name2,.....;
   

hive> insert overwrite table personal_details
    > select x.* from json_rawtable
    > lateral view json_tuple
    > (line,'name','gender','age','city')
    > x as n,g,a,c;


Json with child node
--------------------
[cloudera@quickstart ~]$ cat >  json2
{"name":"manohar","age":"34","address":{"state":"india","city":"hyd"},"gender":"male"}


hive> create table person_info(line string);
hive> load data local inpath 'json2' into table person_info;
hive> select *from person_info;

Loading data col wise
---------------------
hive> create table person_info_table(name string,age int,address string,gender string);

hive> insert overwrite table person_info_table
    > select x.* from person_info
    > lateral view
    > json_tuple(line,'name','age','address','gender')x
    > as n,a,addr,g;
hive> select *from person_info_table;

Loading child node by spitting col wise
---------------------------------------
hive> create table per_info(name string,age int,state string,city string,gender string);

hive> insert overwrite table json_strdata
    > select
    > get_json_object(line,'$.name'),
    > get_json_object(line,'$.age'),
    > get_json_object(line,'$.address.state'),
    > get_json_object(line,'$.address.city'),
    > get_json_object(line,'$.gender')
    > from json_rawtable;


hive> insert overwrite table per_info
    > select name,age,get_json_object(address,'$.state'),
    > get_json_object(address,'$.city'),
    > gender from person_info_table;