SlideShare une entreprise Scribd logo
1  sur  32
Télécharger pour lire hors ligne
Geolocation Databases
in Ruby on Rails
Ireneusz Skrobiś
Lead Developer @ Selleo
Challenge description - what we have?
Challenge description - what we want?
Challenge description - why we want to do that?
Research
Research
GeoLite
ip2notion
ip2location
GeoNames
Research
GeoLite
ip2notion
ip2location
GeoNames
and the winner is:
GeoNames
Problem
countryInfo.txt
allCountries.txt
(locations, states, cities)
Problem
countryInfo.txt
252 entries
allCountries.txt
(locations, states, cities)
Problem
countryInfo.txt
252 entries
allCountries.txt
(locations, states, cities)
11,157,064 entries
Problem
countryInfo.txt
252 entries
allCountries.txt
(locations, states, cities)
11,157,064 entries
1,7GB (!)
Initial implementation
create table geo_names (
geonameid int,
name varchar(200),
fclass char(1),
fcode varchar(10),
population bigint,
country varchar(2),
admin1 varchar(20),
admin2 varchar(80),
admin3 varchar(20),
admin4 varchar(20),
asciiname varchar(200),
alternatenames text,
latitude float,
longitude float,
cc2 varchar(100),
elevation int,
gtopo30 int,
timezone varchar(40),
moddate date
);
Initial implementation
create table countries (
iso_alpha2 char(2),
name varchar(200),
geonameId int,
iso_alpha3 char(3),
iso_numeric integer,
fips_code varchar(3),
capital varchar(200),
areainsqkm double precision,
population integer,
continent varchar(2),
tld varchar(10),
currencycode varchar(3),
currencyname varchar(20),
phone varchar(20),
postalcode varchar(100),
postalcoderegex varchar(200),
languages varchar(200),
neighbors varchar(50),
equivfipscode varchar(3)
);
Initial implementation
COPY countries
(iso_alpha2,iso_alpha3,iso_numeric,fips_code,name,capital,areainsqkm,
population,continent,tld,currencycode,currencyname,phone,postalcode,
postalcoderegex,languages,geonameid,neighbors,equivfipscode)
FROM
'#{Rails.root.join('db', 'files', 'countryInfo.txt').to_s}' null as ''
CSV DELIMITER 't' HEADER;
Initial implementation
COPY geo_names
(geonameid,name,asciiname,alternatenames,latitude,longitude,fclass,
fcode,country,cc2,admin1,admin2,admin3,admin4,population,elevation,
gtopo30,timezone,moddate)
FROM
'#{Rails.root.join('db', 'files', 'allCountries.txt').to_s}' null as ''
CSV DELIMITER 't' HEADER;
Initial implementation
# get all administrative regions for country code
where(country: code, fcode: 'ADM1').order(:name)
# get all cities/villages country code and administrative region
where(country: code, admin1: adm.admin1, fclass: 'P')
.where.not(population: 0)
.order(:name)
Findings
we don’t need all fields
we don’t need all entries
Database adjustment
START
COUNT: 11,157,064 DB: 1,748,198,652 TIME: 2206ms
Database adjustment
START
COUNT: 11,157,064 DB: 1,748,198,652 TIME: 2206ms
REMOVE COLUMNS
Database adjustment
START
COUNT: 11,157,064 DB: 1,748,198,652 TIME: 2206ms
REMOVE COLUMNS
geo_names:
admin2, admin3, admin4, asciiname, alternatenames, latitude, longitude, cc2,
elevation, gtopo30, timezone, moddate
countries:
iso_alpha3, iso_numeric, fips_code, capital, areainsqkm, population,
continent, tld, currencycode, currencyname, phone, postalcode,
postalcoderegex, languages, neighbors, equivfipscode
Database adjustment
START
COUNT: 11,157,064 DB: 1,748,198,652 TIME: 2206ms
REMOVE COLUMNS
COUNT: 11,157,064 DB: 409,655,812 TIME: 2197ms
Database adjustment
START
COUNT: 11,157,064 DB: 1,748,198,652 TIME: 2206ms
REMOVE COLUMNS
COUNT: 11,157,064 DB: 409,655,812 TIME: 2197ms
GeoName.where.not(fclass: %w(A P)).delete_all
Database adjustment
START
COUNT: 11,157,064 DB: 1,748,198,652 TIME: 2206ms
REMOVE COLUMNS
COUNT: 11,157,064 DB: 409,655,812 TIME: 2197ms
GeoName.where.not(fclass: %w(A P)).delete_all
COUNT: 4,729,998 DB: 160,005,106 TIME: 1310ms
Database adjustment
START
COUNT: 11,157,064 DB: 1,748,198,652 TIME: 2206ms
REMOVE COLUMNS
COUNT: 11,157,064 DB: 409,655,812 TIME: 2197ms
GeoName.where.not(fclass: %w(A P)).delete_all
COUNT: 4,729,998 DB: 160,005,106 TIME: 1310ms
GeoName.where(fclass: 'P', population: 0).delete_all
Database adjustment
START
COUNT: 11,157,064 DB: 1,748,198,652 TIME: 2206ms
REMOVE COLUMNS
COUNT: 11,157,064 DB: 409,655,812 TIME: 2197ms
GeoName.where.not(fclass: %w(A P)).delete_all
COUNT: 4,729,998 DB: 160,005,106 TIME: 1310ms
GeoName.where(fclass: 'P', population: 0).delete_all
COUNT: 723,681 DB: 27,268,183 TIME: 854ms
Database adjustment
START
COUNT: 11,157,064 DB: 1,748,198,652 TIME: 2206ms
REMOVE COLUMNS
COUNT: 11,157,064 DB: 409,655,812 TIME: 2197ms
GeoName.where.not(fclass: %w(A P)).delete_all
COUNT: 4,729,998 DB: 160,005,106 TIME: 1310ms
GeoName.where(fclass: 'P', population: 0).delete_all
COUNT: 723,681 DB: 27,268,183 TIME: 854ms
GeoName.where(fclass: 'A').where.not(fcode: 'ADM1').delete_all
Database adjustment
START
COUNT: 11,157,064 DB: 1,748,198,652 TIME: 2206ms
REMOVE COLUMNS
COUNT: 11,157,064 DB: 409,655,812 TIME: 2197ms
GeoName.where.not(fclass: %w(A P)).delete_all
COUNT: 4,729,998 DB: 160,005,106 TIME: 1310ms
GeoName.where(fclass: 'P', population: 0).delete_all
COUNT: 723,681 DB: 27,268,183 TIME: 854ms
GeoName.where(fclass: 'A').where.not(fcode: 'ADM1').delete_all
COUNT: 367,782 DB: 13,644,454 TIME: 770ms
Database adjustment
START
COUNT: 11,157,064 DB: 1,748,198,652 TIME: 2206ms
REMOVE COLUMNS
COUNT: 11,157,064 DB: 409,655,812 TIME: 2197ms
GeoName.where.not(fclass: %w(A P)).delete_all
COUNT: 4,729,998 DB: 160,005,106 TIME: 1310ms
GeoName.where(fclass: 'P', population: 0).delete_all
COUNT: 723,681 DB: 27,268,183 TIME: 854ms
GeoName.where(fclass: 'A').where.not(fcode: 'ADM1').delete_all
COUNT: 367,782 DB: 13,644,454 TIME: 61 ms (after restart)
Final implementation
psql project_dev_db
COPY countries TO
'/Users/irek/rails_workspace/battleriff/db/files/countries.csv'
DELIMITER E't' CSV HEADER;
COPY geo_names TO
'/Users/irek/rails_workspace/battleriff/db/files/geo_names.csv'
DELIMITER E't' CSV HEADER;
Final implementation
class AddGeoNamesTables <
ActiveRecord::Migration
def up
execute <<-SQL
create table geo_names (
geonameid int,
name varchar(200),
fclass char(1),
fcode varchar(10),
population bigint,
country varchar(2),
admin1 varchar(20)
);
create table countries (
iso_alpha2 char(2),
name varchar(200),
geonameid int
);
SQL
end
def down
drop_table :countries
drop_table :geo_names
end
end
Final implementation
namespace :geo_names do
desc "Setup ALL data needed for countries/states/cities selection"
task setup_all: :environment do
ActiveRecord::Base.connection.execute <<-SQL
copy countries (iso_alpha2,name,geonameid)
from '#{Rails.root.join('db', 'files', 'countries.csv').to_s}'
null as '' CSV DELIMITER 't' HEADER;
copy geo_names (geonameid,name,fclass,fcode,population,country, admin1)
from '#{Rails.root.join('db', 'files', 'geo_names.csv').to_s}'
null as '' CSV DELIMITER 't' HEADER;
SQL
end
end
Thank you!
Live long and prosper :)
Ireneusz Skrobiś
Lead Developer @ Selleo

Contenu connexe

Tendances

Mashup caravan android-talks
Mashup caravan android-talksMashup caravan android-talks
Mashup caravan android-talks
honjo2
 

Tendances (20)

Advanced Data Visualization in R- Somes Examples.
Advanced Data Visualization in R- Somes Examples.Advanced Data Visualization in R- Somes Examples.
Advanced Data Visualization in R- Somes Examples.
 
imager package in R and examples..
imager package in R and examples..imager package in R and examples..
imager package in R and examples..
 
SwiftGirl20170904 - Apple Map
SwiftGirl20170904 - Apple MapSwiftGirl20170904 - Apple Map
SwiftGirl20170904 - Apple Map
 
MongoDB GeoSpatial Feature
MongoDB GeoSpatial FeatureMongoDB GeoSpatial Feature
MongoDB GeoSpatial Feature
 
Geolocation and Mapping in PhoneGap applications
Geolocation and Mapping in PhoneGap applicationsGeolocation and Mapping in PhoneGap applications
Geolocation and Mapping in PhoneGap applications
 
Keeping Track of Moving Things: MapKit and CoreLocation in Depth
Keeping Track of Moving Things: MapKit and CoreLocation in DepthKeeping Track of Moving Things: MapKit and CoreLocation in Depth
Keeping Track of Moving Things: MapKit and CoreLocation in Depth
 
Mashup caravan android-talks
Mashup caravan android-talksMashup caravan android-talks
Mashup caravan android-talks
 
RHadoop の紹介
RHadoop の紹介RHadoop の紹介
RHadoop の紹介
 
Geo Spatial Plot using R
Geo Spatial Plot using R Geo Spatial Plot using R
Geo Spatial Plot using R
 
R meets Hadoop
R meets HadoopR meets Hadoop
R meets Hadoop
 
Data visualization with multiple groups using ggplot2
Data visualization with multiple groups using ggplot2Data visualization with multiple groups using ggplot2
Data visualization with multiple groups using ggplot2
 
Application of Google Earth Engine in Open NAPs
Application of Google Earth Engine in Open NAPsApplication of Google Earth Engine in Open NAPs
Application of Google Earth Engine in Open NAPs
 
Add Some Fun to Your Functional Programming With RXJS
Add Some Fun to Your Functional Programming With RXJSAdd Some Fun to Your Functional Programming With RXJS
Add Some Fun to Your Functional Programming With RXJS
 
Earth Engine on Google Cloud Platform (GCP)
Earth Engine on Google Cloud Platform (GCP)Earth Engine on Google Cloud Platform (GCP)
Earth Engine on Google Cloud Platform (GCP)
 
Data visualization using the grammar of graphics
Data visualization using the grammar of graphicsData visualization using the grammar of graphics
Data visualization using the grammar of graphics
 
Mi primer map reduce
Mi primer map reduceMi primer map reduce
Mi primer map reduce
 
MapReduce DesignPatterns
MapReduce DesignPatternsMapReduce DesignPatterns
MapReduce DesignPatterns
 
Mi primer map reduce
Mi primer map reduceMi primer map reduce
Mi primer map reduce
 
CLUSTERGRAM
CLUSTERGRAMCLUSTERGRAM
CLUSTERGRAM
 
ggtimeseries-->ggplot2 extensions
ggtimeseries-->ggplot2 extensions ggtimeseries-->ggplot2 extensions
ggtimeseries-->ggplot2 extensions
 

Similaire à Geolocation Databases in Ruby on Rails

Storm - As deep into real-time data processing as you can get in 30 minutes.
Storm - As deep into real-time data processing as you can get in 30 minutes.Storm - As deep into real-time data processing as you can get in 30 minutes.
Storm - As deep into real-time data processing as you can get in 30 minutes.
Dan Lynn
 

Similaire à Geolocation Databases in Ruby on Rails (20)

GeoMesa on Apache Spark SQL with Anthony Fox
GeoMesa on Apache Spark SQL with Anthony FoxGeoMesa on Apache Spark SQL with Anthony Fox
GeoMesa on Apache Spark SQL with Anthony Fox
 
Introduction To PostGIS
Introduction To PostGISIntroduction To PostGIS
Introduction To PostGIS
 
Going Native: Leveraging the New JSON Native Datatype in Oracle 21c
Going Native: Leveraging the New JSON Native Datatype in Oracle 21cGoing Native: Leveraging the New JSON Native Datatype in Oracle 21c
Going Native: Leveraging the New JSON Native Datatype in Oracle 21c
 
Handling Real-time Geostreams
Handling Real-time GeostreamsHandling Real-time Geostreams
Handling Real-time Geostreams
 
Handling Real-time Geostreams
Handling Real-time GeostreamsHandling Real-time Geostreams
Handling Real-time Geostreams
 
GeoMesa on Spark SQL: Extracting Location Intelligence from Data
GeoMesa on Spark SQL: Extracting Location Intelligence from DataGeoMesa on Spark SQL: Extracting Location Intelligence from Data
GeoMesa on Spark SQL: Extracting Location Intelligence from Data
 
Redis 101
Redis 101Redis 101
Redis 101
 
Data Time Travel by Delta Time Machine
Data Time Travel by Delta Time MachineData Time Travel by Delta Time Machine
Data Time Travel by Delta Time Machine
 
IT Days - Parse huge JSON files in a streaming way.pptx
IT Days - Parse huge JSON files in a streaming way.pptxIT Days - Parse huge JSON files in a streaming way.pptx
IT Days - Parse huge JSON files in a streaming way.pptx
 
ETL for Pros: Getting Data Into MongoDB
ETL for Pros: Getting Data Into MongoDBETL for Pros: Getting Data Into MongoDB
ETL for Pros: Getting Data Into MongoDB
 
クラウドDWHとしても進化を続けるPivotal Greenplumご紹介
クラウドDWHとしても進化を続けるPivotal Greenplumご紹介クラウドDWHとしても進化を続けるPivotal Greenplumご紹介
クラウドDWHとしても進化を続けるPivotal Greenplumご紹介
 
Oracle JSON treatment evolution - from 12.1 to 18 AOUG-2018
Oracle JSON treatment evolution - from 12.1 to 18 AOUG-2018Oracle JSON treatment evolution - from 12.1 to 18 AOUG-2018
Oracle JSON treatment evolution - from 12.1 to 18 AOUG-2018
 
Getting Started with MongoDB and NodeJS
Getting Started with MongoDB and NodeJSGetting Started with MongoDB and NodeJS
Getting Started with MongoDB and NodeJS
 
Hive dirty/beautiful hacks in TD
Hive dirty/beautiful hacks in TDHive dirty/beautiful hacks in TD
Hive dirty/beautiful hacks in TD
 
Realm - Phoenix Mobile Festival
Realm - Phoenix Mobile FestivalRealm - Phoenix Mobile Festival
Realm - Phoenix Mobile Festival
 
huhu
huhuhuhu
huhu
 
Storm - As deep into real-time data processing as you can get in 30 minutes.
Storm - As deep into real-time data processing as you can get in 30 minutes.Storm - As deep into real-time data processing as you can get in 30 minutes.
Storm - As deep into real-time data processing as you can get in 30 minutes.
 
Where20 2008 Ruby Tutorial
Where20 2008 Ruby TutorialWhere20 2008 Ruby Tutorial
Where20 2008 Ruby Tutorial
 
Monitoring MongoDB (MongoUK)
Monitoring MongoDB (MongoUK)Monitoring MongoDB (MongoUK)
Monitoring MongoDB (MongoUK)
 
Hypertable - massively scalable nosql database
Hypertable - massively scalable nosql databaseHypertable - massively scalable nosql database
Hypertable - massively scalable nosql database
 

Plus de Ireneusz Skrobiś

Plus de Ireneusz Skrobiś (8)

Bugs and non-technical client
Bugs and non-technical clientBugs and non-technical client
Bugs and non-technical client
 
PayPal Subscriptions in Ruby on Rails application
PayPal Subscriptions in Ruby on Rails applicationPayPal Subscriptions in Ruby on Rails application
PayPal Subscriptions in Ruby on Rails application
 
New features in Ruby 2.5
New features in Ruby 2.5New features in Ruby 2.5
New features in Ruby 2.5
 
How to introduce a new developer to a project.
How to introduce a new developer to a project.How to introduce a new developer to a project.
How to introduce a new developer to a project.
 
New features in Ruby 2.4
New features in Ruby 2.4New features in Ruby 2.4
New features in Ruby 2.4
 
Geocoding with Rails and Twitter Typeahead
Geocoding with Rails and Twitter TypeaheadGeocoding with Rails and Twitter Typeahead
Geocoding with Rails and Twitter Typeahead
 
LOL vs Dota2: Battle Of APIs
LOL vs Dota2: Battle Of APIsLOL vs Dota2: Battle Of APIs
LOL vs Dota2: Battle Of APIs
 
What to do when there is no API
What to do when there is no APIWhat to do when there is no API
What to do when there is no API
 

Dernier

Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
Epec Engineered Technologies
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
mphochane1998
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
AldoGarca30
 

Dernier (20)

Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
Engineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planesEngineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planes
 
Wadi Rum luxhotel lodge Analysis case study.pptx
Wadi Rum luxhotel lodge Analysis case study.pptxWadi Rum luxhotel lodge Analysis case study.pptx
Wadi Rum luxhotel lodge Analysis case study.pptx
 
Moment Distribution Method For Btech Civil
Moment Distribution Method For Btech CivilMoment Distribution Method For Btech Civil
Moment Distribution Method For Btech Civil
 
PE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and propertiesPE 459 LECTURE 2- natural gas basic concepts and properties
PE 459 LECTURE 2- natural gas basic concepts and properties
 
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
 
Computer Networks Basics of Network Devices
Computer Networks  Basics of Network DevicesComputer Networks  Basics of Network Devices
Computer Networks Basics of Network Devices
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
 
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxHOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech students
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
 
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptxA CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdf
 
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
NO1 Top No1 Amil Baba In Azad Kashmir, Kashmir Black Magic Specialist Expert ...
 
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
Unit 4_Part 1 CSE2001 Exception Handling and Function Template and Class Temp...
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
 
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsFEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
 
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptxOrlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
Orlando’s Arnold Palmer Hospital Layout Strategy-1.pptx
 

Geolocation Databases in Ruby on Rails