RDBMS數據定時采集到HDFS

[toc]


RDBMS數據定時采集到HDFS

前言

其實并不難,就是使用sqoop定時從MySQL中導入到HDFS中,主要是sqoop命令的使用和Linux腳本的操作這些知識。

場景

在我們的場景中,需要每天將數據庫中新增的用戶數據采集到HDFS中,數據庫中有time字段,
用以標識該用戶信息錄入數據庫的時間,所以下面的數據導入操作也是依賴于這個字段。

數據準備

在MySQL數據庫中準備如下數據:

##構建sql的操作
create DATABASE IF NOT EXISTS db_log_statics;
use db_log_statics;
CREATE TABLE `t_user_info` (`id` bigint(20) NOT NULL,`name` varchar(20) COLLATE utf8_bin DEFAULT NULL,`address` varchar(20) COLLATE utf8_bin DEFAULT NULL,`time` date DEFAULT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;insert into `t_user_info` (`id`, `name`, `address`, `time`) values('1','張三','北京朝陽','2018-04-05');
insert into `t_user_info` (`id`, `name`, `address`, `time`) values('2','李四','河南洛陽','2018-04-05');
insert into `t_user_info` (`id`, `name`, `address`, `time`) values('3','王五','廣東邵陽','2018-04-05');
insert into `t_user_info` (`id`, `name`, `address`, `time`) values('4','趙六','山東德州','2018-04-07');
insert into `t_user_info` (`id`, `name`, `address`, `time`) values('5','周七','山東青島','2018-04-07');
insert into `t_user_info` (`id`, `name`, `address`, `time`) values('6','魏八','福建廈門','2018-04-07');
insert into `t_user_info` (`id`, `name`, `address`, `time`) values('7','王二麻子','山西五臺山','2018-04-06');
insert into `t_user_info` (`id`, `name`, `address`, `time`) values('8','王大錘','陜西榆林','2018-04-06');
insert into `t_user_info` (`id`, `name`, `address`, `time`) values('9','燕小六','云南大理','2018-04-06');
insert into `t_user_info` (`id`, `name`, `address`, `time`) values('10','雷布斯','湖北仙桃','2018-04-06');

腳本編寫

exportUser2HDFS.sh

#!/bin/env bash# shell中引用外部文件的變量
source /home/uplooking/shells/db-mysql.conf# source 系統的環境變量
source ~/.bash_profile
# 日期變量
today=`date +%Y-%m-%d`
yesterday=`date -d"1 day ago" +%Y-%m-%d`/home/uplooking/app/sqoop/bin/sqoop import \
--connect jdbc:mysql://${stat_ipaddr}:${stat_port}/${stat_dbname} \
--username ${stat_uname} \
--password ${stat_upwd} \
--target-dir hdfs://ns1/input/t_user/${yesterday} \
--query "SELECT id, name, address, time FROM t_user_info WHERE time >='${yesterday}' AND time < '${today}' AND \$CONDITIONS" \
-m 1 --fields-terminated-by "," --split-by ","

db-mysql.conf

#統計庫數據庫ip地址
stat_ipaddr=192.168.43.116
#統計庫端口
stat_port=3306
#統計庫名稱
stat_dbname=db_log_statics
#統計庫用戶名
stat_uname=root
#統計庫密碼
stat_upwd=root

編寫定時任務

crontab -e# 要求每天凌晨2點10分同步數據
10 2 * * * /bin/bash /home/uplooking/shells/exportUser2HDFS.sh >/dev/null 2>&1 &

需要注意的是,如果在Notepad++中遠程編輯shell腳本文件,在Linux中是無法執行的,原因為,此時在Linux中用vim查看文件格式:set ff,會發現為:fileformat=dos,而正常我們在Linux中編輯的文件應該為:fileformat=unix,所以解決方案為:set ff=unix

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/390746.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/390746.shtml
英文地址,請注明出處:http://en.pswp.cn/news/390746.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

單詞嵌入_神秘的文本分類:單詞嵌入簡介

單詞嵌入Natural language processing (NLP) is an old science that started in the 1950s. The Georgetown IBM experiment in 1954 was a big step towards a fully automated text translation. More than 60 Russian sentences were translated into English using simple…

使用Hadoop所需要的一些Linux基礎

Linux 概念 Linux 是一個類Unix操作系統&#xff0c;是 Unix 的一種&#xff0c;它 控制整個系統基本服務的核心程序 (kernel) 是由 Linus 帶頭開發出來的&#xff0c;「Linux」這個名稱便是以 「Linus’s unix」來命名的。 Linux泛指一類操作系統&#xff0c;具體的版本有&a…

python多項式回歸_Python從頭開始的多項式回歸

python多項式回歸Polynomial regression in an improved version of linear regression. If you know linear regression, it will be simple for you. If not, I will explain the formulas here in this article. There are other advanced and more efficient machine learn…

《Linux命令行與shell腳本編程大全 第3版》Linux命令行---4

以下為閱讀《Linux命令行與shell腳本編程大全 第3版》的讀書筆記&#xff0c;為了方便記錄&#xff0c;特地與書的內容保持同步&#xff0c;特意做成一節一次隨筆&#xff0c;特記錄如下&#xff1a; 《Linux命令行與shell腳本編程大全 第3版》Linux命令行--- Linux命令行與she…

徹底搞懂 JS 中 this 機制

徹底搞懂 JS 中 this 機制 摘要&#xff1a;本文屬于原創&#xff0c;歡迎轉載&#xff0c;轉載請保留出處&#xff1a;https://github.com/jasonGeng88/blog 目錄 this 是什么this 的四種綁定規則綁定規則的優先級綁定例外擴展&#xff1a;箭頭函數this 是什么 理解this之前&a…

?如何在2分鐘內將GraphQL服務器添加到RESTful Express.js API

You can get a lot done in 2 minutes, like microwaving popcorn, sending a text message, eating a cupcake, and hooking up a GraphQL server.您可以在2分鐘內完成很多工作&#xff0c;例如微波爐爆米花&#xff0c;發送短信&#xff0c; 吃蛋糕以及連接GraphQL服務器 。 …

leetcode 1744. 你能在你最喜歡的那天吃到你最喜歡的糖果嗎?

給你一個下標從 0 開始的正整數數組 candiesCount &#xff0c;其中 candiesCount[i] 表示你擁有的第 i 類糖果的數目。同時給你一個二維數組 queries &#xff0c;其中 queries[i] [favoriteTypei, favoriteDayi, dailyCapi] 。 你按照如下規則進行一場游戲&#xff1a; 你…

回歸分析_回歸

回歸分析Machine learning algorithms are not your regular algorithms that we may be used to because they are often described by a combination of some complex statistics and mathematics. Since it is very important to understand the background of any algorith…

ruby nil_Ruby中的數據類型-True,False和Nil用示例解釋

ruby niltrue, false, and nil are special built-in data types in Ruby. Each of these keywords evaluates to an object that is the sole instance of its respective class.true &#xff0c; false和nil是Ruby中的特殊內置數據類型。 這些關鍵字中的每一個都求值為一個對…

淺嘗flutter中的動畫(淡入淡出)

在移動端開發中&#xff0c;經常會有一些動畫交互&#xff0c;比如淡入淡出,效果如圖&#xff1a; 因為官方封裝好了AnimatedOpacity Widget&#xff0c;開箱即用&#xff0c;所以我們用起來很方便&#xff0c;代碼量很少&#xff0c;做少量配置即可&#xff0c;所以&#xff0…

數據科學還是計算機科學_何時不使用數據科學

數據科學還是計算機科學意見 (Opinion) 目錄 (Table of Contents) Introduction 介紹 Examples 例子 When You Should Use Data Science 什么時候應該使用數據科學 Summary 摘要 介紹 (Introduction) Both Data Science and Machine Learning are useful fields that apply sev…

空間復雜度 用什么符號表示_什么是大O符號解釋:時空復雜性

空間復雜度 用什么符號表示Do you really understand Big O? If so, then this will refresh your understanding before an interview. If not, don’t worry — come and join us for some endeavors in computer science.您真的了解Big O嗎&#xff1f; 如果是這樣&#xf…

leetcode 523. 連續的子數組和

給你一個整數數組 nums 和一個整數 k &#xff0c;編寫一個函數來判斷該數組是否含有同時滿足下述條件的連續子數組&#xff1a; 子數組大小 至少為 2 &#xff0c;且 子數組元素總和為 k 的倍數。 如果存在&#xff0c;返回 true &#xff1b;否則&#xff0c;返回 false 。 …

Docker學習筆記 - Docker Compose

一、概念 Docker Compose 用于定義運行使用多個容器的應用&#xff0c;可以一條命令啟動應用&#xff08;多個容器&#xff09;。 使用Docker Compose 的步驟&#xff1a; 定義容器 Dockerfile定義應用的各個服務 docker-compose.yml啟動應用 docker-compose up二、安裝 Note t…

創建shell腳本

1.寫一個腳本 a) 用touch命令創建一個文件&#xff1a;touch my_script b) 用vim編輯器打開my_script文件&#xff1a;vi my_script c) 用vim編輯器編輯my_script文件,內容如下&#xff1a; #!/bin/bash 告訴shell使用什么程序解釋腳本 #My first script l…

線性回歸算法數學原理_線性回歸算法-非數學家的高級數學

線性回歸算法數學原理內部AI (Inside AI) Linear regression is one of the most popular algorithms used in different fields well before the advent of computers. Today with the powerful computers, we can solve multi-dimensional linear regression which was not p…

您應該在2020年首先學習哪種編程語言? ????d???s????:???su?

Most people’s journey toward learning to program starts with a single late-night Google search.大多數人學習編程的旅程都是從一個深夜Google搜索開始的。 Usually it’s something like “Learn ______”通常它類似于“學習______” But how do they decide which la…

Linux 概述

UNIX發展歷程 第一個版本是1969年由Ken Thompson&#xff08;UNIX之父&#xff09;在AT& T貝爾實驗室實現Ken Thompson和Dennis Ritchie&#xff08;C語言之父&#xff09;使用C語言對整個系統進行了再加工和編寫UNIX的源代碼屬于SCO公司&#xff08;AT&T ->Novell …

課程一(Neural Networks and Deep Learning),第四周(Deep Neural Networks)—— 0.學習目標...

Understand the key computations underlying deep learning, use them to build and train deep neural networks, and apply it to computer vision. 學習目標 See deep neural networks as successive blocks put one after each otherBuild and train a deep L-layer Neura…

使用ActionTrail Python SDK

ActionTrail提供官方的Python SDK。本文將簡單介紹一下如何使用ActionTrail的Python SDK。 安裝Aliyun Core SDK。 pip install aliyun-python-sdk-core 安裝ActionTrail Python SDK。 pip install aliyun-python-sdk-actiontrail 下面是測試的代碼。調用LookupEventsRequest獲…