ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • hive sql -1
    개발기록/Hadoop 2017. 11. 20. 23:15

    hive sql



    show databases; 


    create databases 데이터베이스명

    comment database_comment

    location hdfs_path

    with dbproperties(property_name=property_value);

    use 생성한데이터베이스명;

    -hdfs의 저장경로를 설정하지 않을경우, 기본 하둡 파일 시스템 디렉토리로 들어간다 예를들면 /user/hive/warehouse

    하이브의 웨어항우스 디렉토리의 서브에 저장된다

    -낮은 버전의 hive일 경우에 alter로 location을 수정할 수 없으니 생성할때 locaiton path를 잘 잡아준다.


    <테이블 생성>

    create table records ( id int, name String)

    row format delimited

    fields terminated by '\t';

    -데이터 파일의 행은 개행문자로 분리된다. 각 행은 탭으로 필드를 구분한다.


    <데이터 로드하기>

    load data local inpath 'tmp/list.txt'

    overwrite into table records;

    - 지정된 로컬 파일을 하이브의 웨어하우스 디렉토리, 여기에서는 하둡 파일 시스템에 적재한다.

    - 이제 records 테이블에 로드된 파일은 하둡 파일 시스템의 /user/hive/warehouse/records 디렉터리에서 찾을 수 있다.


    <테이블 정보>

    describe test;

    describe formatted test;

    - hdfs에 저장되는 locationd을 확인할 수 있다


    hdfs 경로를 수정하려고 했는데 되지않았다.

    그 이유는 버전이 낮아서..ㅜ.ㅜ

    hive --version 확인해보자

    0.13.0 버전을 확인!

    ALTER (DATABASE|SCHEMA) database_name SET DBPROPERTIES (property_name=property_value, ...);

    -- (Note: SCHEMA added in Hive 0.14.0)

    ALTER (DATABASE|SCHEMA) database_name SET OWNER [USER|ROLE] user_or_role;

    -- (Note: Hive 0.13.0 and later; SCHEMA added in Hive 0.14.0)

    ALTER (DATABASE|SCHEMA) database_name SET LOCATION hdfs_path;

    -- (Note: Hive 2.2.1, 2.4.0 and later)


    hdfs 경로를 다시 잡기 위해서 데이터 베이스를 create 했다.

    create database lalaland

    comment 'jiyeon Test'

    location '/tmp/hdfs_test';


    <테이블 생성>

    create table list(user_no int, user_name String, user_mobile int)

    row format delimited

    fields terminated by '|'

    comment 'user info';



    <데이터 로드하기>

    load data local inpath '/tmp/list.txt'

    overwrite into table records;


    <테이블의 내용 날리기>

    truncate table table_name;



    제일 중요한건..

    데이터 파일 형식 확인 잘하기

    utf-8로 저장해서 올렸는데

    hive에서 select 했을때 깨지더라..ㅠㅠ FTP로 다운로드해서 확인해서 알게됐다.

    로드할 때 로드할 파일의 형식을 잘 확인할것


    그 이유가,

    메모장으로 데이터를 생성할 경우,

    메모장데이터 앞에 안보이게 메모장 헤더가 붙기 때문에, hive에서

    첫번째 데이터에 null이 뜬다는 것.

    Editplus나 AcroEdit 혹은 그외 Edit 프로그램으로 작성을 하는게 좋다

    Editplus의 hx 모드로 들어가서 확인해보면 헤더가 붙은걸 확인할 수 있다.




    '개발기록 > Hadoop' 카테고리의 다른 글

    hive - alter table mismatched input.. error  (0) 2018.03.28

    댓글

Designed by Tistory.