日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

DBMS、hdfs、hive之间的数据转换之sqoop

發(fā)布時(shí)間:2024/9/27 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 DBMS、hdfs、hive之间的数据转换之sqoop 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1 使用sqoop進(jìn)行數(shù)據(jù)導(dǎo)入導(dǎo)出

將B線數(shù)據(jù)中心所有的數(shù)據(jù)表放到xxxxx數(shù)據(jù)庫(kù)下。

1.1 導(dǎo)入?yún)^(qū)域編碼表

bin/sqoop import –connect jdbc:mysql://xxx.xxx.xxx.142:3306/db1?useSSL=false –username root –password 123456 –target-dir /xxxx/xxxx/sys_area –table tb_sys_area –m 1;

成功之后導(dǎo)入的文件目錄為:

/bplan/data-center/sys_area/part-m-00000

如下圖:

1.2 導(dǎo)入行業(yè)基礎(chǔ)數(shù)據(jù)

bin/sqoop import –connect jdbc:mysql://xxx.xxx.xxx.142:3306/db1?useSSL=false –username root –password 123456 –target-dir /bplan/data-center/sys_industry –table tb_sys_industry –m 1;

1.3 特別注意

如果按照上面的方式導(dǎo)入數(shù)據(jù),那么數(shù)據(jù)間的間隔符號(hào)默認(rèn)為”,”,若需自定義分割符則加入—fields-terminated-by ‘\t’;如:

bin/sqoop import --connect jdbc:mysql://xxx.xxx.xxx.142:3306/db1?useSSL=false --username root --password 123456 --target-dir /xxxx/xxxx/sys_industry_1 --table tb_sys_industry --m 1 --fields-terminated-by '\t';

成功后導(dǎo)入的文件目錄為:

/bplan/data-center/sys_industry/part-m-00000

如下圖:

1.4 將區(qū)域數(shù)據(jù)和行業(yè)數(shù)據(jù)導(dǎo)入到hive中

root@bigdata2 hive-2.3.2]# cd $HIVE_HOME root@bigdata2 hive-2.3.2]# bin/hive 創(chuàng)建新數(shù)據(jù)庫(kù)data_center; # create database data_center;

切換數(shù)據(jù)庫(kù) data_center; # user data_center;

創(chuàng)建hive 區(qū)域信息表 #CREATE TABLE IF NOT EXISTS tb_sys_area ( id int comment '主鍵id', code string comment '編碼', name string comment "地區(qū)名稱", parent_code int comment "父級(jí)地區(qū)編碼", short_name string comment "地區(qū)簡(jiǎn)稱", level_type smallint comment '地區(qū)層級(jí)', city_code string comment '城市編碼', zip_code string comment '郵政編碼', merger_name string comment '地區(qū)全稱', pinyin string comment '地區(qū)拼音', pingan_area_name string comment '平安銀行地區(qū)名稱' ) ROW FORMAT DELIMITED FIELDS TERMINATED BY ',';

將在hdfs中的數(shù)據(jù)導(dǎo)入到hive中

load data inpath '/bplan/data-center/sys_area/part-m-00000' into table data_center.tb_sys_area ;

1.5 Sqoop直接將數(shù)據(jù)導(dǎo)入到hive中

在hive中創(chuàng)建tb_sys_industry

# CREATE TABLE IF NOT EXISTS tb_sys_industry ( id int comment '主鍵id', category_id string comment '類(lèi)目id ', parent_category_id string comment "上級(jí)類(lèi)目id ", root_category_id int comment "根類(lèi)目id ", category_name string comment "類(lèi)目名稱", weixin_category_id smallint comment '微信類(lèi)目id ', merger_name string comment '地區(qū)全稱', mybank_category_id string comment '網(wǎng)商銀行類(lèi)目id ' ) ROW FORMAT DELIMITED FIELDS TERMINATED BY ', '; //如果是通過(guò)mysql直接導(dǎo)入到hive中,此步驟可以不做

使執(zhí)行sqoop導(dǎo)入

#cd $SQOOP_HOME #bin/sqoop import --connect jdbc:mysql://xxx.xxx.xxx.142:3306/db1 --username root --password 123456 --table tb_sys_industry --fields-terminated-by ',' --delete-target-dir --num-mappers 1 --hive-import --hive-database data_center --hive-table tb_sys_industry;

注意:在此過(guò)程中可能會(huì)出現(xiàn)如下:
執(zhí)行異常:ERROR tool.ImportTool: Import failed: java.io.IOException: java.lang.ClassNotFoundException: org.apache.hadoop.hive.conf.HiveConf
解決:sqoop需要一個(gè)hive的包,將hive/lib中的hive-common-2.3.3.jar拷貝到sqoop的lib目錄中。

1.6 Hive中的數(shù)據(jù)導(dǎo)入到mysql中

在MySQL中新建hive導(dǎo)入的表

CREATE TABLE `tb_sys_industry_1` (`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,`category_id` VARCHAR(50) NOT NULL COMMENT '類(lèi)目id',`parent_category_id` VARCHAR(50) DEFAULT NULL COMMENT '上級(jí)類(lèi)目id',`root_category_id` VARCHAR(50) NOT NULL COMMENT '根類(lèi)目id',`category_name` VARCHAR(100) NOT NULL COMMENT '類(lèi)目名稱',`weixin_category_id` VARCHAR(50) DEFAULT NULL COMMENT '微信類(lèi)目id',`merger_name` VARCHAR(100) DEFAULT NULL,`mybank_category_id` VARCHAR(50) DEFAULT NULL COMMENT '網(wǎng)商銀行類(lèi)目id',PRIMARY KEY (`id`) ) ENGINE=INNODB AUTO_INCREMENT=157 DEFAULT CHARSET=utf8mb4 COMMENT='行業(yè)信息’

在sqoop中執(zhí)行:

# bin/sqoop export --connect jdbc:mysql://xxx.xxx.xxx.142:3306/db1 --username root --password 123456 --table tb_sys_industry_1 --export-dir /user/hive/warehouse/data_center.db/tb_sys_industry/part-m-00000 如果分割符不一樣的話 則命令后+ --input-fields-terminated-by '\t'

注意:
hive默認(rèn)的字段分隔符為’\001’,sqoop默認(rèn)的分隔符是’,’。
–input-fields-terminated-by:表示用于hive或hdfs數(shù)據(jù)導(dǎo)出到外部存儲(chǔ)分隔參數(shù);
–fields-terminated-by:表示用于外面存儲(chǔ)導(dǎo)入到hive或hdfs中需要實(shí)現(xiàn)字段分隔的參數(shù);

1.7 HIVE數(shù)據(jù)備份

進(jìn)入hive;

use nginx_log;

仿照MySQL方式進(jìn)行表數(shù)據(jù)備份

create table nginx_log_info_20180724 as select * from nginx_log_info;

把Hive中的表數(shù)據(jù)備份到磁盤(pán)中。
備份示例:

insert overwrite local directory '/home/bigdata_bak/nginx_log /nginx_log_info_20180724' ROW FORMAT DELIMITED FIELDS TERMINATED BY '|' STORED AS TEXTFILE select * from nginx_log_info;

1.8 把磁盤(pán)數(shù)據(jù)導(dǎo)入到hive中

創(chuàng)建表:

CREATE TABLE IF NOT EXISTS nginx_log_info_20180724 ( id bigint comment '主鍵id', product_name string comment '所屬業(yè)務(wù)', remote_addr string comment "遠(yuǎn)程服務(wù)器ip", access_time int comment "訪問(wèn)時(shí)間,格式為:yyyyMMdd", access_timestamp double comment "時(shí)間戳", time_zone string comment '時(shí)區(qū)', request_type string comment '請(qǐng)求類(lèi)型', request_url string comment '請(qǐng)求url', request_protocol string comment '請(qǐng)求協(xié)議', status smallint comment '請(qǐng)求狀態(tài)', body_bytes_sent int comment '發(fā)送內(nèi)容大小', request_body string comment '請(qǐng)求體', http_referer string comment 'http引用頁(yè)', http_user_agent string comment 'http_user_agent', os_name string comment '操作系統(tǒng)名稱', os string comment '操作系統(tǒng)', browser_name string comment '瀏覽器名稱', browser_version string comment '瀏覽器版本', device_type string comment '設(shè)備類(lèi)型', browser string comment '瀏覽器', access_tool string comment '類(lèi)型', http_x_forwarded_for string comment 'http_x_forwarded_for', request_time double comment '請(qǐng)求響應(yīng)時(shí)間' ) ROW FORMAT DELIMITED FIELDS TERMINATED BY '|' LINES TERMINATED BY '\n' STORED AS TEXTFILE; LOAD DATA LOCAL INPATH '/home/bigdata_bak/nginx_log /nginx_log_info_20180724' OVERWRITE INTO TABLE nginx_log_info_20180724;

清空表數(shù)據(jù):

insert overwrite table nginx_log_info_20180724 select * from nginx_log_info_20180724 where 1=0;

總結(jié)

以上是生活随笔為你收集整理的DBMS、hdfs、hive之间的数据转换之sqoop的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。