首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >ETL (Extract-Transform-Load) with Kiba(1)

ETL (Extract-Transform-Load) with Kiba(1)

作者头像
franket
发布2021-10-18 11:56:43
发布2021-10-18 11:56:43
7770
举报
文章被收录于专栏:技术杂记技术杂记

前言

在构建数据仓库,进行数据分析,实现异构数据库之间数据转存的情境下会涉及到数据的 ETL(Extract-Transform-Load)

Tip: 一般而言如下情况也可以使用 ETL 来解决:

  • 将遗留数据库中的数据迁移到新的数据库中
  • 自动处理数据以生成报表
  • 将多个系统中的所有数据或部分数据同步到一个中来
  • 将数据处理得易于搜索(导入到Elasticsearch 或 Solr 中)
  • 多个数据库中的数据进行聚合处理后将结果保存到一个数据一致的库中
  • 清理脏数据或无效数据
  • 将数据进行位置分配后显示到地图应用中
  • 为用户实现一个数据导出的服务

ETL主要分三部:

  • 数据抽取:(Data extraction)从各类数据源读取数据
  • 数据处理:(Data transformation)对数据进行适当的加工处理以适应需求
  • 数据装载:(Data loading)将结果保存到合适的地方

整个ETL的过程是像管道流一样进行处理的

Since the data extraction takes time, it is common to execute the three phases in parallel. While the data is being extracted, another transformation process executes. It processes the already received data and prepares it for loading. As soon as there is some data ready to be loaded into the target, the data loading kicks off without waiting for the completion of the previous phases

Ruby 的 kiba gem 可以很容易地实现轻量级的 ETL

这里分享一下 kiba 的简单使用,详细可以参考 官方文档How to reformat CSV files with Kiba (in-depth, hands-on tutorial)

Tip: 目前此 gem 的最新版本为 kiba 0.6.1


概要


环境

代码语言:javascript
复制
[root@h102 ~]# cat /etc/issue
CentOS release 6.6 (Final)
Kernel \r on an \m

[root@h102 ~]# uname  -a
Linux h102.temp 2.6.32-504.el6.x86_64 #1 SMP Wed Oct 15 04:27:16 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
[root@h102 ~]# ruby -v 
ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-linux]
[root@h102 ~]# gem --version
2.5.1
[root@h102 ~]#  

这里我们根据 How to reformat CSV files with Kiba (in-depth, hands-on tutorial) 中的实验一步步来体验一下 Kiba 的简单使用方法


源数据与目标数据

CSV源数据

代码语言:javascript
复制
date_facture;montant_eur;numero_commande
7/3/2015;10,96;FA1986
7/3/2015;85,11;FA1987
8/3/2015;6,41;FA1988

CSV目标数据

代码语言:javascript
复制
invoice_number,invoice_date,amount_eur
FA1986,2015-03-07,10.96
FA1987,2015-03-07,85.11
FA1988,2015-03-08,6.41

它们之间的差别

  • 列使用 ; 作为分割,要转化为 ,
  • 价格使用 , 作为分割,要转化为 .
  • 列名要从 date_facture 转为 invoice_date,从 montant_eur 转为 invoice_number
  • 数据的格式从 7/3/2015 转化为 2015-03-07

创建一个ETL项目

代码语言:javascript
复制
[root@h102 ~]# mkdir kiba
[root@h102 ~]# cd kiba
[root@h102 kiba]# ls
[root@h102 kiba]# 

创建一个 Gemfile 用来指定依赖

代码语言:javascript
复制
[root@h102 kiba]# vim Gemfile
[root@h102 kiba]# cat Gemfile 
source 'https://gems.ruby-china.org'

gem 'kiba', '~> 0.6.0'
gem 'awesome_print'
[root@h102 kiba]# 

这里的源我们使用 source 'https://gems.ruby-china.org' 因为 'https://rubygems.org' 会被墙

gem 'kiba', '~> 0.6.0' 是当前最新的 kiba 版本,项目中要使用到

gem 'awesome_print' 是一个很好用的打印工具

下面是它和普通打印的区别

代码语言:javascript
复制
[root@h102 ~]# irb
2.3.0 :001 > require 'awesome_print'
 => true 
2.3.0 :002 > p (1..8).to_a
[1, 2, 3, 4, 5, 6, 7, 8]
 => [1, 2, 3, 4, 5, 6, 7, 8] 
2.3.0 :003 > ap (1..8).to_a
[
    [0] 1,
    [1] 2,
    [2] 3,
    [3] 4,
    [4] 5,
    [5] 6,
    [6] 7,
    [7] 8
]
 => nil 
2.3.0 :004 >

它可以用很友好(便于人类阅读)地方式展示对象的结构和内容,更详细的用法可以参考 awesome_print


安装依赖并且测试

代码语言:javascript
复制
[root@h102 kiba]# bundle install 
Don't run Bundler as root. Bundler can ask for sudo if it is needed, and installing your bundle
as root will break this application for all non-root users on this machine.
Fetching gem metadata from https://gems.ruby-china.org/..
Fetching version metadata from https://gems.ruby-china.org/.
Resolving dependencies...
Installing awesome_print 1.7.0
Installing kiba 0.6.1
Using bundler 1.12.5
Bundle complete! 2 Gemfile dependencies, 3 gems now installed.
Use `bundle show [gemname]` to see where a bundled gem is installed.
[root@h102 kiba]# echo "puts 'Hello from Kiba'" > convert-csv.etl
[root@h102 kiba]# bundle exec kiba convert-csv.etl 
Hello from Kiba
[root@h102 kiba]#

Note: 这里必须确保 bundler gem 已经安装好,否则没法使用 bundle 命令

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 概要
    • 环境
    • 源数据与目标数据
      • CSV源数据
      • CSV目标数据
      • 它们之间的差别
    • 创建一个ETL项目
      • 创建一个 Gemfile 用来指定依赖
      • 安装依赖并且测试
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档