aeden-activewarehouse-etl 0.9.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (82) hide show
  1. data/etl/CHANGELOG +190 -0
  2. data/etl/LICENSE +7 -0
  3. data/etl/README +85 -0
  4. data/etl/Rakefile +153 -0
  5. data/etl/TODO +28 -0
  6. data/etl/bin/etl +28 -0
  7. data/etl/bin/etl.cmd +8 -0
  8. data/etl/examples/database.example.yml +16 -0
  9. data/etl/lib/etl.rb +97 -0
  10. data/etl/lib/etl/batch.rb +2 -0
  11. data/etl/lib/etl/batch/batch.rb +111 -0
  12. data/etl/lib/etl/batch/directives.rb +55 -0
  13. data/etl/lib/etl/builder.rb +2 -0
  14. data/etl/lib/etl/builder/date_dimension_builder.rb +96 -0
  15. data/etl/lib/etl/commands/etl.rb +89 -0
  16. data/etl/lib/etl/control.rb +3 -0
  17. data/etl/lib/etl/control/control.rb +403 -0
  18. data/etl/lib/etl/control/destination.rb +420 -0
  19. data/etl/lib/etl/control/destination/database_destination.rb +95 -0
  20. data/etl/lib/etl/control/destination/file_destination.rb +124 -0
  21. data/etl/lib/etl/control/source.rb +109 -0
  22. data/etl/lib/etl/control/source/database_source.rb +220 -0
  23. data/etl/lib/etl/control/source/enumerable_source.rb +11 -0
  24. data/etl/lib/etl/control/source/file_source.rb +90 -0
  25. data/etl/lib/etl/control/source/model_source.rb +39 -0
  26. data/etl/lib/etl/core_ext.rb +1 -0
  27. data/etl/lib/etl/core_ext/time.rb +5 -0
  28. data/etl/lib/etl/core_ext/time/calculations.rb +42 -0
  29. data/etl/lib/etl/engine.rb +552 -0
  30. data/etl/lib/etl/execution.rb +20 -0
  31. data/etl/lib/etl/execution/base.rb +9 -0
  32. data/etl/lib/etl/execution/batch.rb +8 -0
  33. data/etl/lib/etl/execution/job.rb +8 -0
  34. data/etl/lib/etl/execution/migration.rb +85 -0
  35. data/etl/lib/etl/generator.rb +2 -0
  36. data/etl/lib/etl/generator/generator.rb +20 -0
  37. data/etl/lib/etl/generator/surrogate_key_generator.rb +39 -0
  38. data/etl/lib/etl/http_tools.rb +125 -0
  39. data/etl/lib/etl/parser.rb +11 -0
  40. data/etl/lib/etl/parser/apache_combined_log_parser.rb +47 -0
  41. data/etl/lib/etl/parser/delimited_parser.rb +74 -0
  42. data/etl/lib/etl/parser/fixed_width_parser.rb +65 -0
  43. data/etl/lib/etl/parser/parser.rb +41 -0
  44. data/etl/lib/etl/parser/sax_parser.rb +218 -0
  45. data/etl/lib/etl/parser/xml_parser.rb +65 -0
  46. data/etl/lib/etl/processor.rb +11 -0
  47. data/etl/lib/etl/processor/block_processor.rb +14 -0
  48. data/etl/lib/etl/processor/bulk_import_processor.rb +81 -0
  49. data/etl/lib/etl/processor/check_exist_processor.rb +80 -0
  50. data/etl/lib/etl/processor/check_unique_processor.rb +35 -0
  51. data/etl/lib/etl/processor/copy_field_processor.rb +26 -0
  52. data/etl/lib/etl/processor/encode_processor.rb +55 -0
  53. data/etl/lib/etl/processor/hierarchy_exploder_processor.rb +55 -0
  54. data/etl/lib/etl/processor/print_row_processor.rb +12 -0
  55. data/etl/lib/etl/processor/processor.rb +25 -0
  56. data/etl/lib/etl/processor/rename_processor.rb +24 -0
  57. data/etl/lib/etl/processor/require_non_blank_processor.rb +26 -0
  58. data/etl/lib/etl/processor/row_processor.rb +17 -0
  59. data/etl/lib/etl/processor/sequence_processor.rb +23 -0
  60. data/etl/lib/etl/processor/surrogate_key_processor.rb +53 -0
  61. data/etl/lib/etl/processor/truncate_processor.rb +35 -0
  62. data/etl/lib/etl/row.rb +20 -0
  63. data/etl/lib/etl/screen.rb +14 -0
  64. data/etl/lib/etl/screen/row_count_screen.rb +20 -0
  65. data/etl/lib/etl/transform.rb +2 -0
  66. data/etl/lib/etl/transform/block_transform.rb +13 -0
  67. data/etl/lib/etl/transform/date_to_string_transform.rb +20 -0
  68. data/etl/lib/etl/transform/decode_transform.rb +51 -0
  69. data/etl/lib/etl/transform/default_transform.rb +20 -0
  70. data/etl/lib/etl/transform/foreign_key_lookup_transform.rb +122 -0
  71. data/etl/lib/etl/transform/hierarchy_lookup_transform.rb +49 -0
  72. data/etl/lib/etl/transform/ordinalize_transform.rb +12 -0
  73. data/etl/lib/etl/transform/sha1_transform.rb +13 -0
  74. data/etl/lib/etl/transform/string_to_date_transform.rb +16 -0
  75. data/etl/lib/etl/transform/string_to_datetime_transform.rb +14 -0
  76. data/etl/lib/etl/transform/string_to_time_transform.rb +11 -0
  77. data/etl/lib/etl/transform/transform.rb +61 -0
  78. data/etl/lib/etl/transform/trim_transform.rb +26 -0
  79. data/etl/lib/etl/transform/type_transform.rb +35 -0
  80. data/etl/lib/etl/util.rb +59 -0
  81. data/etl/lib/etl/version.rb +9 -0
  82. metadata +193 -0
@@ -0,0 +1,35 @@
1
+ module ETL #:nodoc:
2
+ module Processor #:nodoc:
3
+ # A processor which will truncate a table. Use as a pre-processor for cleaning out a table
4
+ # prior to loading
5
+ class TruncateProcessor < ETL::Processor::Processor
6
+ # Defines the table to truncate
7
+ attr_reader :table
8
+
9
+ # Defines the database connection to use
10
+ attr_reader :target
11
+
12
+ # Initialize the processor
13
+ #
14
+ # Options:
15
+ # * <tt>:target</tt>: The target connection
16
+ # * <tt>:table</tt>: The table name
17
+ def initialize(control, configuration)
18
+ super
19
+ #@file = File.join(File.dirname(control.file), configuration[:file])
20
+ @target = configuration[:target] || {}
21
+ @table = configuration[:table]
22
+ end
23
+
24
+ def process
25
+ conn = ETL::Engine.connection(target)
26
+ conn.truncate(table_name)
27
+ end
28
+
29
+ private
30
+ def table_name
31
+ ETL::Engine.table(table, ETL::Engine.connection(target))
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,20 @@
1
+ # This source file contains the ETL::Row class.
2
+
3
+ module ETL #:nodoc:
4
+ # This class represents a single row currently passing through the ETL pipeline
5
+ class Row < Hash
6
+ # Accessor for the originating source
7
+ attr_accessor :source
8
+
9
+ # All change types
10
+ CHANGE_TYPES = [:insert, :update, :delete]
11
+
12
+ # Accessor for the row's change type
13
+ attr_accessor :change_type
14
+
15
+ # Get the change type, defaults to :insert
16
+ def change_type
17
+ @change_type ||= :insert
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,14 @@
1
+ # This source file contains the ETL::Screen module and requires all of the
2
+ # screens
3
+
4
+ module ETL #:nodoc:
5
+ # The ETL::Screen module contains pre-built screens useful for checking the
6
+ # ETL state during execution. Screens may be fatal, which will result in
7
+ # termination of the ETL process, errors, which will result in the
8
+ # termination of just the current ETL control file, or warnings, which will
9
+ # result in a warning message.
10
+ module Screen
11
+ end
12
+ end
13
+
14
+ Dir[File.dirname(__FILE__) + "/screen/*.rb"].each { |file| require(file) }
@@ -0,0 +1,20 @@
1
+ module ETL
2
+ module Screen
3
+ # This screen validates the number of rows which will be bulk loaded
4
+ # against the results from some sort of a row count query. If there
5
+ # is a difference then the screen will not pass
6
+ class RowCountScreen
7
+ attr_accessor :control, :configuration
8
+ def initialize(control, configuration={})
9
+ @control = control
10
+ @configuration = configuration
11
+ execute
12
+ end
13
+ def execute
14
+ unless Engine.rows_written == configuration[:rows]
15
+ raise "Rows written (#{Engine.rows_written}) does not match expected rows (#{configuration[:rows]})"
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,2 @@
1
+ require 'etl/transform/transform'
2
+ Dir[File.dirname(__FILE__) + "/transform/*.rb"].each { |file| require(file) }
@@ -0,0 +1,13 @@
1
+ module ETL
2
+ module Transform
3
+ class BlockTransform < ETL::Transform::Transform
4
+ def initialize(control, name, configuration)
5
+ super
6
+ @block = configuration[:block]
7
+ end
8
+ def transform(name, value, row)
9
+ @block.call(name, value, row)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,20 @@
1
+ module ETL #:nodoc:
2
+ module Transform #:nodoc:
3
+ # Transform a Date or Time to a formatted string instance
4
+ class DateToStringTransform < ETL::Transform::Transform
5
+ # Initialize the transformer.
6
+ #
7
+ # Configuration options:
8
+ # * <tt>:format</tt>: A format passed to strftime. Defaults to %Y-%m-%d
9
+ def initialize(control, name, configuration={})
10
+ super
11
+ @format = configuration[:format] || "%Y-%m-%d"
12
+ end
13
+ # Transform the value using strftime
14
+ def transform(name, value, row)
15
+ return value unless value.respond_to?(:strftime)
16
+ value.strftime(@format)
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,51 @@
1
+ module ETL #:nodoc:
2
+ module Transform #:nodoc:
3
+ # Transform which decodes coded values
4
+ class DecodeTransform < ETL::Transform::Transform
5
+ attr_accessor :decode_table_path
6
+
7
+ attr_accessor :decode_table_delimiter
8
+
9
+ attr_accessor :default_value
10
+
11
+ # Initialize the transformer
12
+ #
13
+ # Configuration options:
14
+ # * <tt>:decode_table_path</tt>: The path to the decode table (defaults to 'decode.txt')
15
+ # * <tt>:decode_table_delimiter</tt>: The decode table delimiter (defaults to ':')
16
+ # * <tt>:default_value</tt>: The default value to use (defaults to 'No Value')
17
+ def initialize(control, name, configuration={})
18
+ super
19
+
20
+ if configuration[:decode_table_path]
21
+ configuration[:decode_table_path] = File.join(File.dirname(control.file), configuration[:decode_table_path])
22
+ end
23
+
24
+ @decode_table_path = (configuration[:decode_table_path] || 'decode.txt')
25
+ @decode_table_delimiter = (configuration[:decode_table_delimiter] || ':')
26
+ @default_value = (configuration[:default_value] || 'No Value')
27
+ end
28
+
29
+ # Transform the value
30
+ def transform(name, value, row)
31
+ decode_table[value] || default_value
32
+ end
33
+
34
+ # Get the decode table
35
+ def decode_table
36
+ unless @decode_table
37
+ @decode_table = {}
38
+ open(decode_table_path).each do |line|
39
+ code, value = line.strip.split(decode_table_delimiter)
40
+ if code && code.length > 0
41
+ @decode_table[code] = value
42
+ else
43
+ @default_value = value
44
+ end
45
+ end
46
+ end
47
+ @decode_table
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,20 @@
1
+ module ETL #:nodoc:
2
+ module Transform #:nodoc:
3
+ # Transform which will replace nil or empty values with a specified value.
4
+ class DefaultTransform < Transform
5
+ attr_accessor :default_value
6
+ # Initialize the transform
7
+ #
8
+ # Configuration options:
9
+ # * <tt>:default_value</tt>: The default value to use if the incoming value is blank
10
+ def initialize(control, name, configuration)
11
+ super
12
+ @default_value = configuration[:default_value]
13
+ end
14
+ # Transform the value
15
+ def transform(name, value, row)
16
+ value.blank? ? default_value : value
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,122 @@
1
+ module ETL #:nodoc:
2
+ module Transform #:nodoc:
3
+ # Transform which looks up the value and replaces it with a foriegn key reference
4
+ class ForeignKeyLookupTransform < ETL::Transform::Transform
5
+ # The resolver to use if the foreign key is not found in the collection
6
+ attr_accessor :resolver
7
+
8
+ # Initialize the foreign key lookup transform.
9
+ #
10
+ # Configuration options:
11
+ # *<tt>:collection</tt>: A Hash of natural keys mapped to surrogate keys. If this is not specified then
12
+ # an empty Hash will be used. This Hash will be used to cache values that have been resolved already
13
+ # for future use.
14
+ # *<tt>:resolver</tt>: Object or Class which implements the method resolve(value)
15
+ def initialize(control, name, configuration={})
16
+ super
17
+
18
+ @collection = (configuration[:collection] || {})
19
+ @resolver = configuration[:resolver]
20
+ @resolver = @resolver.new if @resolver.is_a?(Class)
21
+ end
22
+
23
+ # Transform the value by resolving it to a foriegn key
24
+ def transform(name, value, row)
25
+ fk = @collection[value]
26
+ unless fk
27
+ raise ResolverError, "Foreign key for #{value} not found and no resolver specified" unless resolver
28
+ raise ResolverError, "Resolver does not appear to respond to resolve method" unless resolver.respond_to?(:resolve)
29
+ fk = resolver.resolve(value)
30
+ raise ResolverError, "Unable to resolve #{value} to foreign key for #{name} in row #{ETL::Engine.rows_read}" unless fk
31
+ @collection[value] = fk
32
+ end
33
+ fk
34
+ end
35
+ end
36
+ # Alias class name for the ForeignKeyLookupTransform.
37
+ class FkLookupTransform < ForeignKeyLookupTransform; end
38
+ end
39
+ end
40
+
41
+ # Resolver which resolves using ActiveRecord.
42
+ class ActiveRecordResolver
43
+ # The ActiveRecord class to use
44
+ attr_accessor :ar_class
45
+
46
+ # The find method to use (as a symbol)
47
+ attr_accessor :find_method
48
+
49
+ # Initialize the resolver. The ar_class argument should extend from
50
+ # ActiveRecord::Base. The find_method argument must be a symbol for the
51
+ # finder method used. For example:
52
+ #
53
+ # ActiveRecordResolver.new(Person, :find_by_name)
54
+ #
55
+ # Note that the find method defined must only take a single argument.
56
+ def initialize(ar_class, find_method)
57
+ @ar_class = ar_class
58
+ @find_method = find_method
59
+ end
60
+
61
+ # Resolve the value
62
+ def resolve(value)
63
+ rec = ar_class.__send__(find_method, value)
64
+ rec.nil? ? nil : rec.id
65
+ end
66
+ end
67
+
68
+ class SQLResolver
69
+ # Initialize the SQL resolver. Use the given table and field name to search
70
+ # for the appropriate foreign key. The field should be the name of a natural
71
+ # key that is used to locate the surrogate key for the record.
72
+ #
73
+ # The connection argument is optional. If specified it can be either a symbol
74
+ # referencing a connection defined in the ETL database.yml file or an actual
75
+ # ActiveRecord connection instance. If the connection is not specified then
76
+ # the ActiveRecord::Base.connection will be used.
77
+ def initialize(table, field, connection=nil)
78
+ @table = table
79
+ @field = field
80
+ @connection = (connection.respond_to?(:quote) ? connection : ETL::Engine.connection(connection)) if connection
81
+ @connection ||= ActiveRecord::Base.connection
82
+ end
83
+ def resolve(value)
84
+ @connection.select_value("SELECT id FROM #{table_name} WHERE #{@field} = #{@connection.quote(value)}")
85
+ end
86
+ def table_name
87
+ ETL::Engine.table(@table, @connection)
88
+ end
89
+ end
90
+
91
+ class FlatFileResolver
92
+ # Initialize the flat file resolver. Expects to open a comma-delimited file.
93
+ # Returns the column with the given result_field_index.
94
+ #
95
+ # The matches argument is a Hash with the key as the column index to search and
96
+ # the value of the Hash as a String to match exactly. It will only match the first
97
+ # result.
98
+ def initialize(file, match_index, result_field_index)
99
+ @file = file
100
+ @match_index = match_index
101
+ @result_field_index = result_field_index
102
+ end
103
+
104
+ # Get the rows from the file specified in the initializer.
105
+ def rows
106
+ @rows ||= FasterCSV.read(@file)
107
+ end
108
+ protected :rows
109
+
110
+ # Match the row field from the column indicated by the match_index with the given
111
+ # value and return the field value from the column identified by the result_field_index.
112
+ def resolve(value)
113
+ rows.each do |row|
114
+ #puts "checking #{row.inspect} for #{value}"
115
+ if row[@match_index] == value
116
+ #puts "match found!, returning #{row[@result_field_index]}"
117
+ return row[@result_field_index]
118
+ end
119
+ end
120
+ nil
121
+ end
122
+ end
@@ -0,0 +1,49 @@
1
+ module ETL #:nodoc:
2
+ module Transform #:nodoc:
3
+ # Transform which walks up the hierarchy tree to find a value of the current level's value
4
+ # is nil.
5
+ #
6
+ # TODO: Let the resolver be implemented in a class so different resolution methods are
7
+ # possible.
8
+ class HierarchyLookupTransform < ETL::Transform::Transform
9
+ # The name of the field to use for the parent ID
10
+ attr_accessor :parent_id_field
11
+
12
+ # The target connection name
13
+ attr_accessor :target
14
+
15
+ # Initialize the transform
16
+ #
17
+ # Configuration options:
18
+ # * <tt>:target</tt>: The target connection name (required)
19
+ # * <tt>:parent_id_field</tt>: The name of the field to use for the parent ID (defaults to :parent_id)
20
+ def initialize(control, name, configuration={})
21
+ super
22
+ @parent_id_field = configuration[:parent_id_field] || :parent_id
23
+ @target = configuration[:target]
24
+ end
25
+
26
+ # Transform the value.
27
+ def transform(name, value, row)
28
+ if parent_id = row[parent_id_field]
29
+ # TODO: should use more than just the first source out of the control
30
+ parent_id, value = lookup(name,
31
+ control.sources.first.configuration[:table], parent_id, parent_id_field)
32
+ until value || parent_id.nil?
33
+ # TODO: should use more than just the first source out of the control
34
+ parent_id, value = lookup(name,
35
+ control.sources.first.configuration[:table], parent_id, parent_id_field)
36
+ end
37
+ end
38
+ value
39
+ end
40
+
41
+ # Lookup the parent value.
42
+ def lookup(field, table, parent_id, parent_id_field)
43
+ q = "SELECT #{parent_id_field}, #{field} FROM #{table} WHERE id = #{parent_id}"
44
+ row = ETL::Engine.connection(target).select_one(q)
45
+ return row[parent_id_field.to_s], row[field.to_s]
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,12 @@
1
+ module ETL #:nodoc:
2
+ module Transform #:nodoc:
3
+ # Transform a number to an ordinalized version using the ActiveSupport ordinalize
4
+ # core extension
5
+ class OrdinalizeTransform < ETL::Transform::Transform
6
+ # Transform the value from a number to an ordinalized number
7
+ def transform(name, value, row)
8
+ value.ordinalize
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,13 @@
1
+ require 'digest/sha1'
2
+
3
+ module ETL #:nodoc:
4
+ module Transform #:nodoc:
5
+ # Transform which hashes the original value with a SHA-1 hash algorithm
6
+ class Sha1Transform < ETL::Transform::Transform
7
+ # Transform the value with a SHA1 digest algorithm.
8
+ def transform(name, value, row)
9
+ Digest::SHA1.hexdigest(value)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,16 @@
1
+ module ETL #:nodoc:
2
+ module Transform #:nodoc:
3
+ # Transform a String representation of a date to a Date instance
4
+ class StringToDateTransform < ETL::Transform::Transform
5
+ # Transform the value using Date.parse
6
+ def transform(name, value, row)
7
+ return value if value.nil?
8
+ begin
9
+ Date.parse(value)
10
+ rescue => e
11
+ return value
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,14 @@
1
+ module ETL #:nodoc:
2
+ module Transform #:nodoc:
3
+ # Transform a String representation of a date to a DateTime instance
4
+ class StringToDateTimeTransform < ETL::Transform::Transform
5
+ # Transform the value using DateTime.parse.
6
+ #
7
+ # WARNING: This transform is slow (due to the Ruby implementation), but if you need to
8
+ # parse timestamps before or after the values supported by the Time.parse.
9
+ def transform(name, value, row)
10
+ DateTime.parse(value) unless value.nil?
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,11 @@
1
+ module ETL #:nodoc:
2
+ module Transform #:nodoc:
3
+ # Transform a String representation of a date to a Time instance
4
+ class StringToTimeTransform < ETL::Transform::Transform
5
+ # Transform the value using Time.parse
6
+ def transform(name, value, row)
7
+ Time.parse(value) unless value.nil?
8
+ end
9
+ end
10
+ end
11
+ end