ฉันจะสร้างส่วนหน้าสำหรับการสืบค้นฐานข้อมูล Redshift ได้อย่างไร (หวังว่าจะใช้ Rails)

ดังนั้นฉันจึงมีฐานข้อมูล Redshift ที่มีตารางเพียงพอซึ่งรู้สึกว่าคุ้มค่ากับเวลาของฉันในการสร้างส่วนหน้าเพื่อทำให้การสืบค้นง่ายกว่าการพิมพ์คำสั่ง SQL เล็กน้อย

ตามหลักการแล้ว ฉันสามารถทำได้โดยเชื่อมต่อฐานข้อมูลกับแอป Rails (เพราะฉันมีประสบการณ์เล็กน้อยกับ Rails) ฉันไม่แน่ใจว่าจะเชื่อมต่อฐานข้อมูล Redshift ระยะไกลกับแอปพลิเคชัน Rails ในเครื่องได้อย่างไร หรือจะทำให้ activerecord ทำงานกับ redshift ได้อย่างไร

ใครมีข้อเสนอแนะ/แหล่งข้อมูลเพื่อช่วยฉันในการเริ่มต้น? ฉันเปิดรับตัวเลือกอื่นๆ เพื่อเชื่อมต่อฐานข้อมูล Redshift กับส่วนหน้า หากมีตัวเลือกที่สร้างไว้ล่วงหน้าง่ายกว่า Rails


person johncorser    schedule 15.07.2014    source แหล่งที่มา
comment
ดูด้านล่าง แค่ต้องทำสิ่งเดียวกันกับโปรเจ็กต์ของฉันเอง   -  person harrisjb    schedule 18.07.2014


คำตอบ (3)


#app/models/data_warehouse.rb
class DataWarehouse < ActiveRecord::Base                      
  establish_connection "redshift_staging"
  #or, if you want to have a db per environment
  #establish_connection "redshift_#{Rails.env}"
end

โปรดทราบว่าเรากำลังเชื่อมต่อกับ 5439 ไม่ใช่ค่าเริ่มต้น 5432 ดังนั้นฉันจึงระบุพอร์ต นอกจากนี้ ฉันระบุสคีมา เบต้า ซึ่งเป็นสิ่งที่เราใช้สำหรับการรวมที่ไม่เสถียรของเรา คุณอาจมี db ที่แตกต่างกันตามสภาพแวดล้อมตามที่กล่าวไว้ข้างต้น หรือ ใช้สคีมาต่างๆ และรวมไว้ในพาธการค้นหาสำหรับ ActiveRecord

#config/database.yml
redshift_staging:                                                          
  adapter: postgresql                                                      
  encoding: utf8                                                           
  database: db03                                                         
  port: 5439                                                               
  pool: 5                                                                  
  schema_search_path: 'beta'                                                                                          
  username: admin                                                        
  password: supersecretpassword                                               
  host: db03.myremotehost.us  #your remote host here, might be an aws url from Redshift admin console 

###ตัวเลือก 2 การเชื่อมต่อ PG โดยตรง

  class DataWarehouse < ActiveRecord::Base                      

    attr_accessor :conn                                                       

    def initialize                                                            
      @conn = PG.connect(                                                     
       database: 'db03',                                                   
       port: 5439,                                                           
       pool: 5,                                                              
       schema_search_path: 'beta',                                           
       username: 'admin',                                                  
       password: 'supersecretpassword',                                         
       host: 'db03.myremotehost.us'                                               
      )                                                                       
    end    
  end


[DEV] main:0> redshift = DataWarehouse
E, [2014-07-17T11:09:17.758957 #44535] ERROR -- : PG::InsufficientPrivilege: ERROR:  permission denied to set parameter "client_min_messages" to "notice" : SET client_min_messages TO 'notice'
(pry) output error: #<ActiveRecord::StatementInvalid: PG::InsufficientPrivilege: ERROR:  permission denied to set parameter "client_min_messages" to "notice" : SET client_min_messages TO 'notice'>   

อัปเดต:

ฉันลงเอยด้วยตัวเลือกที่ 1 แต่ตอนนี้ใช้อะแดปเตอร์นี้ด้วยเหตุผลหลายประการ:

https://github.com/fiksu/activerecord-redshift-adapter

เหตุผลที่ 1: อะแดปเตอร์ ActiveRecord postgresql ตั้งค่า client_min_messages เหตุผลที่ 2: อะแดปเตอร์ยังพยายามตั้งค่าโซนเวลาด้วย ซึ่ง redshift ไม่อนุญาต (http://docs.aws.amazon.com/redshift/latest/dg/c_redshift-and-postgres-sql.html) เหตุผลที่ 3: แม้ว่าคุณจะเปลี่ยน รหัสใน ActiveRecord สำหรับข้อผิดพลาดสองข้อแรก คุณพบข้อผิดพลาดเพิ่มเติมที่บ่นว่า Redshift ใช้ Postgresql 8.0 เมื่อถึงจุดนั้น ฉันย้ายไปที่อะแดปเตอร์ แล้วจะกลับมาอีกครั้งและอัปเดตหากฉันพบสิ่งที่ดีกว่าในภายหลัง

ฉันเปลี่ยนชื่อตารางเป็น base_aggregate_redshift_tests (สังเกตพหูพจน์) เพื่อให้ ActiveRecord สามารถเชื่อมต่อได้อย่างง่ายดาย หากคุณไม่สามารถเปลี่ยนชื่อตารางใน redshift ได้ ให้ใช้วิธีการ set_table ที่ฉันแสดงความคิดเห็นไว้ด้านล่าง

#Gemfile:
gem 'activerecord4-redshift-adapter', github: 'aamine/activerecord4-redshift-adapter'

ตัวเลือกที่ 1

#config/database.yml
redshift_staging:                                                                                                             
  adapter: redshift                                                                                                           
  encoding: utf8                                                                                                              
  database: db03                                                                                                           
  port: 5439                                                                                                                  
  pool: 5                                                                                                                     
  username: admin                                                                                                
  password: supersecretpassword                                                                                                  
  host: db03.myremotehost.us                                                                                                       
  timeout: 5000   

#app/models/base_aggregates_redshift_test.rb
#Model named to match my tables in Redshift, if you want you can set_table like I have commented out below

class BaseAggregatesRedshiftTest < ActiveRecord::Base
  establish_connection "redshift_staging"
  self.table_name = "beta.base_aggregates_v2"
end

ในคอนโซลโดยใช้ self.table_name -- สังเกตว่ามันจะสืบค้นตารางที่ถูกต้อง เพื่อให้คุณสามารถตั้งชื่อโมเดลของคุณตามที่คุณต้องการ

[DEV] main:0> redshift = BaseAggregatesRedshiftTest.first                                                                    
D, [2014-07-17T15:31:58.678103 #43776] DEBUG -- :   BaseAggregatesRedshiftTest Load (45.6ms)  SELECT "beta"."base_aggregates_v2".* FROM "beta"."base_aggregates_v2" LIMIT 1            

ตัวเลือกที่ 2

#app/models/base_aggregates_redshift_test.rb
class BaseAggregatesRedshiftTest < ActiveRecord::Base
  set_table "beta.base_aggregates_v2"

  ActiveRecord::Base.establish_connection(
    adapter: 'redshift',
    encoding: 'utf8',
    database: 'staging',
    port: '5439',
    pool: '5',
    username: 'admin',
    password: 'supersecretpassword',
    search_schema: 'beta',
    host: 'db03.myremotehost.us',
    timeout: '5000'
  )

end

#in console, abbreviated example of first record, now it's using the new name for my redshift table, just assuming I've got the record at base_aggregates_redshift_tests because I didn't set the table_name

[DEV] main:0> redshift = BaseAggregatesRedshiftTest.first
D, [2014-07-17T15:09:39.388918 #11537] DEBUG -- :   BaseAggregatesRedshiftTest Load (45.3ms)  SELECT "base_aggregates_redshift_tests".* FROM "base_aggregates_redshift_tests" LIMIT 1
#<BaseAggregatesRedshiftTest:0x007fd8c4a12580> {
                                                :truncated_month => Thu, 31 Jan 2013 19:00:00 EST -05:00,
                                                :dma => "Cityville",
                                                :group_id => 9712338,
                                                :dma_id => 9999 
                                                }

โชคดีนะ @johncorser!

person harrisjb    schedule 17.07.2014
comment
ดี. ทำงานได้ดีกับส่วนสำคัญนี้ที่ฉันพบ ซึ่งเปลี่ยน URI เป็น Database.yml คัดลอกเกือบทุกประการ เปลี่ยนอะแดปเตอร์เป็น redshift: gist.github.com/mptre/1654167 - person Peter Ehrlich; 29.01.2015

บทช่วยสอนนี้ช่วยให้คุณตั้งค่าแอป Rails ด้วยอะแดปเตอร์ redshift:

https://www.credible.com/code/setting-up-a-data-warehouse-with-aws-redshift-and-ruby/

โดยสังเขป:

โคลนแอปตัวอย่าง:

git clone [email protected]:tuesy/redshift-ruby-tutorial.git
cd redshift-ruby-tutorial

ตั้งค่าตัวแปร ENV ผ่าน ~/.bashrc (หรือ dotenv):

export REDSHIFT_HOST=redshift-ruby-tutorial.ccmj2nxbsay7.us-east-1.redshift.amazonaws.com
export REDSHIFT_PORT=5439
export REDSHIFT_USER=deploy
export REDSHIFT_PASSWORD=<your password here>
export REDSHIFT_DATABASE=analytics
export REDSHIFT_BUCKET=redshift-ruby-tutorial

ใช้ gem activerecord4-redshift-adapter ใน Gemfile:

'activerecord4-redshift-adapter', '~> 0.2.0' # For Rails 4.2
'activerecord4-redshift-adapter', '~> 0.1.1' # For Rails 4.1

จากนั้นคุณสามารถค้นหา Redshift ได้เหมือนกับที่คุณทำกับโมเดล AR ปกติ:

bundle exec rails c
RedshiftUser.count

(การเปิดเผยข้อมูล: ฉันยังไม่ได้ลองใช้วิธีนี้ แต่อาจจะเร็ว ๆ นี้)

person Benjamin Crouzier    schedule 24.02.2016

คุณอาจต้องการพิจารณา http://www.looker.com/ เป็นส่วนหน้าสำหรับการสำรวจ DB ของคุณ ช่วยให้สามารถบันทึกการสืบค้นได้อย่างง่ายดายและเป็น GUI ที่นักธุรกิจก็สามารถใช้ได้

person SAR622    schedule 30.11.2014