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.mzph.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获…