레이블이 nano tips인 게시물을 표시합니다. 모든 게시물 표시
레이블이 nano tips인 게시물을 표시합니다. 모든 게시물 표시

2019년 5월 9일 목요일

Hive: Map 내부의 json format의 multidimensional array string 데이터 추출 하기

Hive Map 내부의 json format의 multidimensional array string 데이터 추출 하기

데이터 형식

{
    "name" : "Admin",
    "age" : 20,
    "click" : [
        {"dt":"20190101","cnt":12},
        {"dt":"20190102","cnt":21},
        {"dt":"20190103","cnt":24},
        {"dt":"20190104","cnt":25}
    ]
}  

위와 같은 데이터 형식의 map 컬럼 내부의 click 데이터가 multidimensional array로 건수가 유동적인 경우 각 데이터를 한 row씩 출력하고 하고자 할때 아래 방법을 사용

데이터 생성

select map('name','Admin','age',20,'click','[{"dt":"20190101","cnt":12},{"dt":"20190102","cnt":21},{"dt":"20190103","cnt":24},{"dt":"20190104","cnt":25}]') as mapcol


쿼리

select 
    get_json_object(val_json,'$.dt') as dt
    , get_json_object(val_json,'$.cnt') as cnt
from (
    select map('name','Admin','age',20,'click','[{"dt":"20190101","cnt":12},{"dt":"20190102","cnt":21},{"dt":"20190103","cnt":24},{"dt":"20190104","cnt":25}]') as mapcol
) temp
lateral view explode(split(regexp_replace(regexp_replace(mapcol['click'],'\\}\\,\\{','\\}\\#\\{'),'\\[|\\]',''), '\\#')) tbl_json as val_json;

결과


2019년 3월 14일 목요일

Hive : Calculating Running Total

hive에서 누적합계 계산하기

create table

CREATE TABLE sample_rolling_sum(
    ins_date STRING
    , partition_no int
    , amount int
);

INSERT INTO sample_rolling_sum (ins_date, partition_no, amount)
VALUES
    (20190101, 0, 5),
    (20190102, 0, 5),
    (20190103, 0, 10),
    (20190104, 1, 10),
    (20190105, 2, 20),
    (20190106, 2, 5),
    (20190107, 2, 10),
    (20190108, 2, 5)
;

query


select
      ins_date, partition_no, amount
    , sum(amount) over(partition by partition_no order by ins_date) partition_r_sum 
    , sum(amount) over(order by ins_date) r_sum
from sample_rolling_sum

result



2019년 1월 22일 화요일

쉘스크립트 문자열 바꾸기


<< test.txt >>
this is an apple.
path=/aa/bb/cc

1. 기본 명령어

cat test.txt | sed -e "s/{TARGET_STRING}/{REPLACE_STRING}/g"

1.1 apple --> orange 로 바꾸기

cat test.txt | sed -e "s/apple/orange/g"

결과

this is an orange.
path=/aa/bb/cc

 1.2 /aa/bb --> local 로 바꾸기

cat test.txt | sed -e "s/\/aa\/bb/local/g"

결과


this is an orange.
path=local/cc

2019년 1월 21일 월요일

xcrun 에러 발생시 해결책

0. 에러 로그


xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun

1. 해결책

아래 명령어 수행후 설치

xcode-select --install

2019년 1월 17일 목요일

Python : 리스트를 문자로 변환 ( How to Convert a list to string )

join 함수 사용

list 내부 값이 string인 경우

list1 = ['a', 'p', 'p' ,'l', 'e']
jn_str = ''
print (jn_str.join(list1))

Reuslt

apple


list 내부 값이 string이 아닌 경우

list1 = [1, 2, 3,'abcd',[11,22],1.23]
jn_str = ','
jn_str.join(str(e) for e in list1)

Result

'1,2,3,abcd,[11, 22],1.23'


map 함수 사용

조금 더 간단히 표현이 가능

list1 = [1, 2, 3,'abcd',[11,22],1.23]
jn_str = ','
jn_str.join(map(str,list1))

Result

'1,2,3,abcd,[11, 22],1.23'






추천 게시물

python: SVD(Singular Value Decomposition)로 간단한 추천시스템 만들기( feat. surprise )

svd_example In [15]: # !pip install surprise In [21]: from...