Class: Spark::JavaBridge::Base

Inherits:
Object
  • Object
show all
Includes:
Helper::System
Defined in:
lib/spark/java_bridge/base.rb

Direct Known Subclasses

JRuby, RJB

Constant Summary collapse

JAVA_OBJECTS =
[
  'java.util.ArrayList',
  'org.apache.spark.SparkConf',
  'org.apache.spark.api.java.JavaSparkContext',
  'org.apache.spark.api.ruby.RubyRDD',
  'org.apache.spark.api.ruby.RubyUtils',
  'org.apache.spark.api.ruby.RubyWorker',
  'org.apache.spark.api.ruby.PairwiseRDD',
  'org.apache.spark.api.ruby.RubyAccumulatorParam',
  'org.apache.spark.api.ruby.RubySerializer',
  'org.apache.spark.api.python.PythonRDD',
  'org.apache.spark.api.python.PythonPartitioner',
  'org.apache.spark.ui.ruby.RubyTab',
  'org.apache.spark.mllib.api.ruby.RubyMLLibAPI',
  'scala.collection.mutable.HashMap',
  :JInteger  => 'java.lang.Integer',
  :JLong     => 'java.lang.Long',
  :JLogger   => 'org.apache.log4j.Logger',
  :JLevel    => 'org.apache.log4j.Level',
  :JPriority => 'org.apache.log4j.Priority',
  :JUtils    => 'org.apache.spark.util.Utils',
  :JStorageLevel => 'org.apache.spark.storage.StorageLevel',
  :JDenseVector => 'org.apache.spark.mllib.linalg.DenseVector',
  :JDenseMatrix => 'org.apache.spark.mllib.linalg.DenseMatrix'
]
JAVA_TEST_OBJECTS =
[
  'org.apache.spark.mllib.api.ruby.RubyMLLibUtilAPI'
]
RUBY_TO_JAVA_SKIP =
[Fixnum, Integer]

Instance Method Summary collapse

Methods included from Helper::System

included

Constructor Details

#initialize(target) ⇒ Base

Returns a new instance of Base.



44
45
46
# File 'lib/spark/java_bridge/base.rb', line 44

def initialize(target)
  @target = target
end

Instance Method Details

#call(klass, method, *args) ⇒ Object

Call java object



73
74
75
76
77
78
79
80
81
82
# File 'lib/spark/java_bridge/base.rb', line 73

def call(klass, method, *args)
  # To java
  args.map!{|item| to_java(item)}

  # Call java
  result = klass.__send__(method, *args)

  # To ruby
  to_ruby(result)
end

#import_allObject

Import all important classes into Objects



49
50
51
52
53
54
55
56
57
58
# File 'lib/spark/java_bridge/base.rb', line 49

def import_all
  return if @imported

  java_objects.each do |name, klass|
    import(name, klass)
  end

  @imported = true
  nil
end

#import_all_testObject

Import classes for testing



61
62
63
64
65
66
67
68
69
70
# File 'lib/spark/java_bridge/base.rb', line 61

def import_all_test
  return if @imported_test

  java_test_objects.each do |name, klass|
    import(name, klass)
  end

  @imported_test = true
  nil
end

#to_java(object) ⇒ Object Also known as: ruby_to_java



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/spark/java_bridge/base.rb', line 97

def to_java(object)
  if RUBY_TO_JAVA_SKIP.include?(object.class)
    # Some object are convert automatically
    # This is for preventing errors
    # For example: jruby store integer as long so 1.to_java is Long
    object
  elsif object.respond_to?(:to_java)
    object.to_java
  elsif object.is_a?(Array)
    to_java_array_list(object)
  else
    object
  end
end

#to_java_array_list(array) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/spark/java_bridge/base.rb', line 84

def to_java_array_list(array)
  array_list = ArrayList.new
  array.each do |item|
    array_list.add(to_java(item))
  end
  array_list
end

#to_long(number) ⇒ Object



92
93
94
95
# File 'lib/spark/java_bridge/base.rb', line 92

def to_long(number)
  return nil if number.nil?
  JLong.new(number)
end

#to_ruby(object) ⇒ Object Also known as: java_to_ruby

Array problem:

Rjb:   object.toArray -> Array
Jruby: object.toArray -> java.lang.Object


116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/spark/java_bridge/base.rb', line 116

def to_ruby(object)
  if java_object?(object)
    class_name = object.getClass.getSimpleName
    case class_name
    when 'ArraySeq'
      result = []
      iterator = object.iterator
      while iterator.hasNext
        result << to_ruby(iterator.next)
      end
      result
    when 'Map2', 'Map3', 'Map4', 'HashTrieMap'
      Hash[
        object.toSeq.array.to_a.map!{|item| [item._1, item._2]}
      ]
    when 'SeqWrapper'; object.toArray.to_a.map!{|item| to_ruby(item)}
    when 'ofRef';      object.array.to_a.map!{|item| to_ruby(item)} # WrappedArray$ofRef
    when 'LabeledPoint'; Spark::Mllib::LabeledPoint.from_java(object)
    when 'DenseVector';  Spark::Mllib::DenseVector.from_java(object)
    when 'KMeansModel';  Spark::Mllib::KMeansModel.from_java(object)
    when 'DenseMatrix';  Spark::Mllib::DenseMatrix.from_java(object)
    else
      # Some RDD
      if class_name != 'JavaRDD' && class_name.end_with?('RDD')
        object = object.toJavaRDD
        class_name = 'JavaRDD'
      end

      # JavaRDD
      if class_name == 'JavaRDD'
        jrdd = RubyRDD.toRuby(object)

        serializer = Spark::Serializer.build { __batched__(__marshal__) }
        serializer = Spark::Serializer.build { __batched__(__marshal__, 2) }

        return Spark::RDD.new(jrdd, Spark.sc, serializer, deserializer)
      end

      # Unknow
      Spark.logger.warn("Java object '#{object.getClass.name}' was not converted.")
      object
    end

  else
    # Already transfered
    object
  end
end