前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Forklift ETL 基础(一)(2)

Forklift ETL 基础(一)(2)

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

forklift wrapper 脚本

代码语言:javascript
复制
[root@h102 ~]# cat /usr/local/rvm/gems/ruby-2.3.0/gems/forklift_etl-1.2.2/bin/forklift
#!/usr/bin/env ruby

require 'rubygems'
require 'fileutils'

begin
  require 'forklift'
rescue LoadError
  require "#{File.expand_path(File.dirname(__FILE__))}/../lib/forklift.rb"
end

def generate
  p = Dir.pwd

  Dir.mkdir "#{p}/config"
  Dir.mkdir "#{p}/config/connections"
  Dir.mkdir "#{p}/config/connections/mysql"
  Dir.mkdir "#{p}/config/connections/elasticsearch"
  Dir.mkdir "#{p}/config/connections/csv"
  Dir.mkdir "#{p}/log"
  Dir.mkdir "#{p}/pid"
  Dir.mkdir "#{p}/template"
  Dir.mkdir "#{p}/transformations"
  Dir.mkdir "#{p}/transports"
  Dir.mkdir "#{p}/patterns"

  template('source.yml',      "#{p}/config/connections/mysql/source.yml")
  template('destination.yml', "#{p}/config/connections/mysql/destination.yml")
  template('email.yml',       "#{p}/config/email.yml")
  template('email.erb',       "#{p}/template/email.erb")
  template('plan.rb',         "#{p}/plan.rb")
end

def template(source, destination)
  t = "#{File.expand_path(File.dirname(__FILE__))}/../template"
  FileUtils.copy("#{t}/#{source}", destination)
  puts "Example plan generated"
end


def run_plan
  file = "#{Dir.pwd}/#{ARGV[0]}"
  if ARGV[0].nil? 
    puts "[error] Please provide a plan.rb as the first argument"
    exit(1)
  end
  Dir.chdir File.expand_path(File.dirname(ARGV[0]))
  begin
    require 'bundler'
    Bundler.require(:default)
  rescue Exception => e
    puts "cannot load bundler: #{e}"
  end
  require file
end

############

if ['--generate', '-generate'].include?(ARGV[0])
  generate
else
  run_plan
end
[root@h102 ~]# 

这个脚本做了三件事:

  • 加载了一个文件
  • 定义了一个 generate 方法
  • 定义了一个 run_plan 方法
加载了一个文件

加载了这个文件

代码语言:javascript
复制
[root@h102 bin]# ls
forklift  x
[root@h102 bin]# pwd
/usr/local/rvm/gems/ruby-2.3.0/gems/forklift_etl-1.2.2/bin
[root@h102 bin]# cat x
puts "#{File.expand_path(File.dirname(__FILE__))}/../lib/forklift.rb"
[root@h102 bin]# ruby x
/usr/local/rvm/gems/ruby-2.3.0/gems/forklift_etl-1.2.2/bin/../lib/forklift.rb
[root@h102 bin]# ll /usr/local/rvm/gems/ruby-2.3.0/gems/forklift_etl-1.2.2/bin/../lib/forklift.rb
-rwxr-xr-x 1 root rvm 651 Aug  9 15:12 /usr/local/rvm/gems/ruby-2.3.0/gems/forklift_etl-1.2.2/bin/../lib/forklift.rb
[root@h102 bin]# 
generate 方法
代码语言:javascript
复制
def generate
  p = Dir.pwd

  Dir.mkdir "#{p}/config"
  Dir.mkdir "#{p}/config/connections"
  Dir.mkdir "#{p}/config/connections/mysql"
  Dir.mkdir "#{p}/config/connections/elasticsearch"
  Dir.mkdir "#{p}/config/connections/csv"
  Dir.mkdir "#{p}/log"
  Dir.mkdir "#{p}/pid"
  Dir.mkdir "#{p}/template"
  Dir.mkdir "#{p}/transformations"
  Dir.mkdir "#{p}/transports"
  Dir.mkdir "#{p}/patterns"

  template('source.yml',      "#{p}/config/connections/mysql/source.yml")
  template('destination.yml', "#{p}/config/connections/mysql/destination.yml")
  template('email.yml',       "#{p}/config/email.yml")
  template('email.erb',       "#{p}/template/email.erb")
  template('plan.rb',         "#{p}/plan.rb")
end

整个过程就是在当前目录中创建若干目录,然后把模板文件拷贝进来

其中使用到了 template 方法

代码语言:javascript
复制
def template(source, destination)
  t = "#{File.expand_path(File.dirname(__FILE__))}/../template"
  FileUtils.copy("#{t}/#{source}", destination)
  puts "Example plan generated"
end

它起的作用就是负责拷贝模板文件到目标位置中

代码语言:javascript
复制
[root@h102 bin]# pwd
/usr/local/rvm/gems/ruby-2.3.0/gems/forklift_etl-1.2.2/bin
[root@h102 bin]# ll ../template/
total 20
-rwxr-xr-x 1 root rvm  89 Aug  9 15:12 destination.yml
-rwxr-xr-x 1 root rvm  29 Aug  9 15:12 email.erb
-rwxr-xr-x 1 root rvm 538 Aug  9 15:12 email.yml
-rwxr-xr-x 1 root rvm 150 Aug  9 15:12 plan.rb
-rwxr-xr-x 1 root rvm  84 Aug  9 15:12 source.yml
[root@h102 bin]#
run_plan 方法
代码语言:javascript
复制
def run_plan
  file = "#{Dir.pwd}/#{ARGV[0]}"
  if ARGV[0].nil?
    puts "[error] Please provide a plan.rb as the first argument"
    exit(1)
  end
  Dir.chdir File.expand_path(File.dirname(ARGV[0]))
  begin
    require 'bundler'
    Bundler.require(:default)
  rescue Exception => e
    puts "cannot load bundler: #{e}"
  end
  require file
end

这个方法最主要的就是 require file

file 就是当前目录中的 plan.rb 文件(可以不是这个文件名,自定义其它文件名),接在 forklift 后面,作为第一个参数


命令汇总

  • mkdir forklift
  • cd forklift/
  • vim Gemfile
  • cat Gemfile
  • bundle install
  • bundle exec forklift --generate
  • tree
  • which forklift
  • cat /usr/local/rvm/gems/ruby-2.3.0/bin/forklift
  • ruby -e "puts Gem.bin_path('forklift_etl', 'forklift', '>= 0.a')"
  • cat /usr/local/rvm/gems/ruby-2.3.0/gems/forklift_etl-1.2.2/bin/forklift
  • ruby x
  • ll /usr/local/rvm/gems/ruby-2.3.0/gems/forklift_etl-1.2.2/bin/../lib/forklift.rb
  • ll ../template/

原文地址

本文系转载,前往查看

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

本文系转载前往查看

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • forklift wrapper 脚本
  • 命令汇总
相关产品与服务
Elasticsearch Service
腾讯云 Elasticsearch Service(ES)是云端全托管海量数据检索分析服务,拥有高性能自研内核,集成X-Pack。ES 支持通过自治索引、存算分离、集群巡检等特性轻松管理集群,也支持免运维、自动弹性、按需使用的 Serverless 模式。使用 ES 您可以高效构建信息检索、日志分析、运维监控等服务,它独特的向量检索还可助您构建基于语义、图像的AI深度应用。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档