Class: JrubyMahout::PostgresManager
- Inherits:
-
Object
- Object
- JrubyMahout::PostgresManager
- Defined in:
- lib/jruby_mahout/postgres_manager.rb
Instance Attribute Summary collapse
-
#data_model ⇒ Object
Returns the value of attribute data_model.
-
#data_source ⇒ Object
Returns the value of attribute data_source.
-
#statement ⇒ Object
Returns the value of attribute statement.
Instance Method Summary collapse
- #close_data_source ⇒ Object
- #create_statement ⇒ Object
- #create_table(table_name) ⇒ Object
- #delete_record(table_name, record) ⇒ Object
- #delete_table(table_name) ⇒ Object
-
#initialize(params) ⇒ PostgresManager
constructor
A new instance of PostgresManager.
- #setup_data_model(params) ⇒ Object
- #upsert_record(table_name, record) ⇒ Object
Constructor Details
#initialize(params) ⇒ PostgresManager
Returns a new instance of PostgresManager.
13 14 15 16 17 18 19 20 |
# File 'lib/jruby_mahout/postgres_manager.rb', line 13 def initialize(params) @data_source = PGPoolingDataSource.new() @data_source.setUser(params[:username]) @data_source.setPassword(params[:password]) @data_source.setServerName(params[:host]) @data_source.setPortNumber(params[:port]) @data_source.setDatabaseName(params[:db_name]) end |
Instance Attribute Details
#data_model ⇒ Object
Returns the value of attribute data_model.
11 12 13 |
# File 'lib/jruby_mahout/postgres_manager.rb', line 11 def data_model @data_model end |
#data_source ⇒ Object
Returns the value of attribute data_source.
11 12 13 |
# File 'lib/jruby_mahout/postgres_manager.rb', line 11 def data_source @data_source end |
#statement ⇒ Object
Returns the value of attribute statement.
11 12 13 |
# File 'lib/jruby_mahout/postgres_manager.rb', line 11 def statement @statement end |
Instance Method Details
#close_data_source ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/jruby_mahout/postgres_manager.rb', line 39 def close_data_source begin @data_source.close() rescue Exception => e puts e end end |
#create_statement ⇒ Object
30 31 32 33 34 35 36 37 |
# File 'lib/jruby_mahout/postgres_manager.rb', line 30 def create_statement begin connection = @data_source.getConnection() @statement = connection.createStatement() rescue Exception => e puts e end end |
#create_table(table_name) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/jruby_mahout/postgres_manager.rb', line 64 def create_table(table_name) begin @statement.executeUpdate(" CREATE TABLE #{table_name} ( user_id BIGINT NOT NULL, item_id BIGINT NOT NULL, rating int NOT NULL, created TIMESTAMP DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (user_id, item_id) ); ") @statement.executeUpdate("CREATE INDEX #{table_name}_user_id_index ON #{table_name} (user_id);") @statement.executeUpdate("CREATE INDEX #{table_name}_item_id_index ON #{table_name} (item_id);") rescue java.sql.SQLException => e puts e end end |
#delete_record(table_name, record) ⇒ Object
56 57 58 59 60 61 62 |
# File 'lib/jruby_mahout/postgres_manager.rb', line 56 def delete_record(table_name, record) begin @statement.execute("DELETE FROM #{table_name} WHERE user_id=#{record[:user_id]} AND item_id=#{record[:item_id]};") rescue java.sql.SQLException => e puts e end end |
#delete_table(table_name) ⇒ Object
82 83 84 85 86 87 88 89 90 |
# File 'lib/jruby_mahout/postgres_manager.rb', line 82 def delete_table(table_name) begin @statement.executeUpdate("DROP INDEX IF EXISTS #{table_name}_user_id_index;") @statement.executeUpdate("DROP INDEX IF EXISTS #{table_name}_item_id_index;") @statement.executeUpdate("DROP TABLE IF EXISTS #{table_name};") rescue java.sql.SQLException => e puts e end end |
#setup_data_model(params) ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/jruby_mahout/postgres_manager.rb', line 22 def setup_data_model(params) begin @data_model = PostgreSQLJDBCDataModel.new(@data_source, params[:table_name], "user_id", "item_id", "rating", "created") rescue Exception => e puts e end end |
#upsert_record(table_name, record) ⇒ Object
47 48 49 50 51 52 53 54 |
# File 'lib/jruby_mahout/postgres_manager.rb', line 47 def upsert_record(table_name, record) begin @statement.execute("UPDATE #{table_name} SET user_id=#{record[:user_id]}, item_id=#{record[:item_id]}, rating=#{record[:rating]} WHERE user_id=#{record[:user_id]} AND item_id=#{record[:item_id]};") @statement.execute("INSERT INTO #{table_name} (user_id, item_id, rating) SELECT #{record[:user_id]}, #{record[:item_id]}, #{record[:rating]} WHERE NOT EXISTS (SELECT 1 FROM #{table_name} WHERE user_id=#{record[:user_id]} AND item_id=#{record[:item_id]});") rescue java.sql.SQLException => e puts e end end |