How to load key value pairs into hive table? - hadoop

Below is my data set:
Jun name="balaji" id=101
Mar name="kumar" id=102
Created table:
create table sample(month string,name string, id int)
row format delimited fields terminated by 'space' map keys terminated by '=';
Result:
select * from sample;
JUN name="balaji" NULL
Mar name="kumar" NULL
Expected result:
JUN balaji 101
Mar kumar 102
Please help me on this.

Create tables like this:
create table sample(mnth string,names map<string,string>,ids map<string,int>)
row format delimited fields terminated by ' ' map keys terminated by '=';
Select query should be:
select mnth,names["name"],ids["id"] from sample;
result:
Jun "balaji" 101
Mar "kumar" 102
If you fire select * from sample:
Jun {"name":"\"balaji\""} {"id":101}
Mar {"name":"\"kumar\""} {"id":102}
For accessing each value in map you need to pass like names["name"].

Related

LATERAL VIEW explode funtion in hive

I am trying to export data from excel into a hive table, while doing so, i have a column 'ABC' which has values like '1,2,3'.
I used the lateral view explode function but it does not does anything to my data.
Following is my code snippet :
CREATE TABLE table_name
(
id string,
brand string,
data_name string,
name string,
address string,
country string,
flag string,
sample_list array )
ROW FORMAT DELIMITED FIELDS TERMINATED BY '\t'
STORED AS TEXTFILE
;
LOAD DATA LOCAL INPATH 'location' INTO TABLE
table_name ;
output sample:
id brand data_name name address country flag sample_list
19 1 ABC SQL ABC Cornstarch IN 1 ["[1,2,3]"]
then i do:
select * from franchise_unsupress LATERAL VIEW explode(SEslist) SEslist as final_SE;
output sample:
id brand data_name name address country flag sample_list
19 1 ABC SQL ABC Cornstarch IN 1 [1,2,3]
I also tried:
select * from franchise_unsupress lateral view explode(split(SEslist,',')) SEslist AS final_SE ;
but got an error:
FAILED: ClassCastException org.apache.hadoop.hive.serde2.objectinspector.StandardListObjectInspector cannot be cast to org.apache.hadoop.hive.serde2.objectinspector.PrimitiveObjectInspector
whereas, what i need is:
id brand data_name name address country flag sample_list
19 1 ABC SQL ABC Cornstarch IN 1 1
19 1 ABC SQL ABC Cornstarch IN 1 2
19 1 ABC SQL ABC Cornstarch IN 1 3
Any help will be greatly appreciated! thank you
The problem is that array is recognized in a wrong way and loaded as a single element array ["[1,2,3]"]. It should be [1,2,3] or ["1","2","3"] (if it is array<string>)
When creating table, specify delimiter for collections:
FIELDS TERMINATED BY '\t'
COLLECTION ITEMS TERMINATED BY ','
I wanted to provide my answer.
The issue was with the input that was being provided. My input txt file had [] around the input value. They had to be removed and it worked.

Grabbing all rows from a database that have specific column values in laravel

I am dealing with a table that has a bunch of short names, ex.
some_id
long_name
short_name
94
March
mar
19
April
apr
0
June
jun
2
September
Sep
I want to grab only a few supported short names, ex. mar, sep, jun.
How would I go about doing it in laravel?
Currently I have something like this:
$this->result = DB::table('table_above')->get();
But this just grabs every table. I was thinking of adding the where command, but not sure how to check for multiple values.
$this->result = DB::table('table_above')->where('short_name', [somehow say either mar, sep, jun])->get();
At the end I'd like an array that holds a dictionary of rows (i.e $result) with the supported short names (i.e here mar, june, sept).
Try this query
DB :: table ('table_above')->whereIn('short_name', ['mar', 'sep'])->get()

Pivot Dynamic Data in Oracle

I have rollup output of month-wise average sales of products in a quarter as shown below:
Rollup output:
Product Month Sales
------------------------------
Product1 MAY 101.27
Product2 MAY 5.47
Product1 JUN 1481.19
Product2 JUN 84.95
ALL QTR 836.44
I need final output in the following format:
Product1 Product2 AverageSales
May 101.27 1481.19 (null)
Jun 5.47 84.95 (null)
Jul 0 0 (null)
ALL (null) (null) 836.44
I tried to apply pivot on month but since month name is a dynamic value based on the quarter selected, I tried to pivot on the product:
select * from (rollup output)
PIVOT MIN(Sales) FOR Product IN ('Product1' AS Product1_sales, 'Product2' AS Product2_sales, 'ALL' AS Average Sales');
I received the following output:
MONTH Product1_sales Product2_sales Average Sales
MAY 5.47 (null) (null)
MAY (null) 101.27 (null)
JUN 84.95 (null) (null)
JUN (null) 1481.19 (null)
Qtr (null) (null) 836.44
However, I need data in the final format mentioned above. How can I achieve this?
Also, if data for a given month of the quarter is not available, I want to display its value as 0 (also shown in the final format above). How can I achieve that, considering that month name is dynamic?
Edit: I can pivot using product as well since I do not want xml output that comes out of dynamic pivot.
I'm not sure why it needs to be dynamic since the number of months is unlikely to change. Why not just:
select product
, nvl(jan,0) as jan
, nvl(feb,0) as feb
, nvl(mar,0) as mar
, nvl(apr,0) as apr
, nvl(may,0) as may
, nvl(jun,0) as jun
, nvl(jul,0) as jul
, nvl(aug,0) as aug
, nvl(sep,0) as sep
, nvl(oct,0) as oct
, nvl(nov,0) as nov
, nvl(dec,0) as dec
from rollup_output
pivot (min(sales) for (month) in
( 'JAN' as jan, 'FEB' as feb, 'MAR' as mar, 'APR' as apr, 'MAY' as may, 'JUN' as jun
, 'JUL' as jul, 'AUG' as aug, 'SEP' as sep, 'OCT' as oct, 'NOV' as nov, 'DEC' as dec)
);
SQL Fiddle

Conversion of 01 to jan , 02 to feb including year in the field

I have a field containing 201402, 201404, here I want to convert 02 to Feb and 04 to April. Is it possible to do that? This field contains entries of all months. Please help.
There is a lot that you can do in terms of datetime format models within Oracle. A simple example below (replace the string literal within the TO_DATE(...) with your field if it is a string or replace the entire TO_DATE with your field if it is already of data type DATE):
SELECT TO_CHAR(TO_DATE('201402','YYYYMM'),'YYYY-MONTH') FROM DUAL;
SELECT TO_CHAR(TO_DATE('201404','YYYYMM'),'YYYY-MONTH') FROM DUAL;

Event Study (Extracting Dates in SAS)

I need to analyse abnormal returns for an event study on mergers and acquisitions.
** I would like to analyse abnormal returns to acquirers by using event windows. Basically I would like to extract the prices for the acquirers using -1 (the day before the announcement date), announcement date, and +1 (the day after the announcement date).**
I have two different datasets to extract information from.
The first is a dataset with all the merger and acquisition information that has the information in the following format:
DealNO AcquirerNO TargetNO AnnouncementDate
123 abcd Cfgg 22/12/2010
222 qwert cddfgf 26/12/1998
In addition, I have a 2nd dataset which has all the prices.
ISINnumber Date Price
abcd 21/12/2010 10
abcd 22/12/2010 11
abcd 23/12/2010 11
abcd 24/12/2010 12
qwert 20/12/1998 20
qwert 21/12/1998 20
qwert 22/12/1998 21
qwert 23/12/1998 21
qwert 24/12/1998 21
qwert 25/12/1998 22
qwert 26/12/1998 21
qwert 27/12/1998 23
ISIN number is the same as acquirer no, and that is the matching code.
In the end I would like to have a database something like this:
DealNO AcquirerNO TargetNO AnnouncementDate Acquirerprice(-1day) Acquireeprice(0day) Acquirerprice(+1day)
123 abcd Cfgg 22/12/2010 10 11 12
222 qwert cddfgf 26/12/1998 22 21 23
Do you know how I can get this?
I'd prefer to use sas to run the code, but if you are familiar with any other programs that can get the data like this, please let me know.
Thank you in advance ^_^.
This can be done quite easily with PROC SQL and joining the PRICE dataset three times. Try this (assuming data set names of ANNOUCE and PRICE):
Warning: untested code
%let day='21DEC2010'd;
proc sql;
create table RESULT as
select a.dealno,
a.acquirerno,
a.targetno,
a.annoucementdate,
p.price as acquirerprice_prev,
c.price as acquirerprice_cur,
n.price as acquirerprice_next
from ANNOUCE a
left join (select * from PRICE where date = &day-1) p on a.acquirerno = p.isinumber
left join (select * from PRICE where date = &day) c on a.acquirerno = c.isinumber
left join (select * from PRICE where date = &day+1) n on a.acquirerno = n.isinumber
;
quit;

Resources