{"id":316575,"date":"2021-01-19T09:00:24","date_gmt":"2021-01-19T09:00:24","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=316575"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=316575","title":{"rendered":"AWS Athena: GEOIP lookups"},"content":{"rendered":"\n<div class=\"post__text post__text_v2\" id=\"post-content-body\">\n<p><em>PS. \u042d\u0442\u043e \u043f\u0435\u0440\u0435\u0432\u043e\u0434 \u043c\u043e\u0435\u0439 \u0441\u0442\u0430\u0442\u044c\u0438 \u043d\u0430 \u0430\u043d\u0433\u043b\u0438\u0439\u0441\u043a\u043e\u043c. \u0414\u0430\u0432\u043d\u043e \u044f \u043d\u0435 \u043f\u0438\u0441\u0430\u043b \u043d\u0430 \u0425\u0430\u0431\u0440\u0435. \u0421\u0440\u0430\u0437\u0443 \u043f\u0440\u043e\u0449\u0443 \u043f\u0440\u043e\u0449\u0435\u043d\u0438\u044f, \u043c\u043d\u043e\u0433\u043e \u043d\u0430 \u0440\u0443\u0441\u0441\u043a\u043e\u043c \u043d\u0435 \u043f\u0438\u0448\u0443. \u041d\u0435 \u0441\u043a\u0430\u0436\u0443 \u0447\u0442\u043e \u0443 \u043c\u0435\u043d\u044f \u0438 \u0430\u043d\u0433\u043b\u0438\u0439\u0441\u043a\u0438\u0439 \u0448\u0438\u043a\u0430\u0440\u043d\u044b\u0439. \u041d\u043e \u043a \u0441\u043e\u0436\u0430\u043b\u0435\u043d\u0438\u044e \u043f\u0440\u043e\u0436\u0438\u0432\u0430\u043d\u0438\u0435 \u0437\u0430 \u0440\u0443\u0431\u0435\u0436\u043e\u043c \u0443\u0445\u0443\u0434\u0448\u0430\u0435\u0442 \u043c\u043e\u0439 \u0440\u0443\u0441\u0441\u043a\u0438\u0439 \u0438 \u043c\u0435\u0434\u043b\u0435\u043d\u043d\u043e \u0440\u0430\u0437\u0432\u0438\u0432\u0430\u0435\u0442 \u0430\u043d\u0433\u043b\u0438\u0439\u0441\u043a\u0438\u0439. <\/em><\/p>\n<p>\u0415\u0441\u043b\u0438 \u0432\u044b \u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0435\u0441\u044c AWS Athena \u0434\u043b\u044f \u0430\u043d\u0430\u043b\u0438\u0437\u0430 \u043b\u043e\u0433\u043e\u0432, \u0442\u043e \u0447\u0430\u0441\u0442\u043e \u0445\u043e\u0447\u0435\u0442\u0441\u044f \u043d\u0430\u0439\u0442\u0438 \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a IP \u0430\u0434\u0440\u0435\u0441\u043e\u0432. \u041a \u0441\u043e\u0436\u0430\u043b\u0435\u043d\u0438\u044e AWS Athena \u043d\u0435 \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u044d\u0442\u043e\u0433\u043e \u0438\u0437 \u043a\u043e\u0440\u043e\u0431\u043a\u0438. \u041a \u0441\u0447\u0430\u0441\u0442\u044c\u044e MaxMind \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445 GeoIP \u0442\u0430\u0431\u043b\u0438\u0446, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043c\u0435\u0441\u0442\u043e\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043f\u043e IP \u0430\u0434\u0440\u0435\u0441\u0430\u043c. \u0415\u0441\u0442\u044c \u043f\u043b\u0430\u0442\u043d\u0430\u044f \u0438 \u0431\u0435\u0441\u043f\u043b\u0430\u0442\u043d\u0430\u044f \u0432\u0435\u0440\u0441\u0438\u044f.<\/p>\n<p>\u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u043f\u043e\u043a\u0430\u0436\u0443 \u043a\u0430\u043a \u0441\u043e\u0437\u0434\u0430\u0442\u044c AWS Lambda \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043a\u0430\u0436\u0434\u0443\u044e \u043d\u0435\u0434\u0435\u043b\u044e \u0431\u0443\u0434\u0435\u0442 \u0441\u043a\u0430\u0447\u0438\u0432\u0430\u0442\u044c \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u044e\u044e \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445 \u0441 MaxMind \u043d\u0430 S3. \u042d\u0442\u0443 \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432 AWS Athena \u0434\u043b\u044f \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f SQL \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0434\u043b\u044f \u0430\u043d\u0430\u043b\u0438\u0437\u0430, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432\u0435\u0431 \u043b\u043e\u0433\u043e\u0432.<\/p>\n<h2>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0430\u043a\u043a\u0430\u0443\u043d\u0442\u0430 \u043d\u0430 MaxMind<\/h2>\n<p>\u0414\u043b\u044f \u0441\u043a\u0430\u0447\u0438\u0432\u0430\u043d\u0438\u044f \u0434\u0430\u0436\u0435 <a href=\"https:\/\/dev.maxmind.com\/geoip\/geoip2\/geolite2\/\" rel=\"noopener noreferrer nofollow\">\u0431\u0435\u0441\u043f\u043b\u0430\u0442\u043d\u044b\u0445 \u0431\u0430\u0437 \u0434\u0430\u043d\u043d\u044b\u0445<\/a> GeoLite 2 \u0441 MaxMind \u0432\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0431\u0443\u0434\u0435\u0442 <a href=\"https:\/\/www.maxmind.com\/\" rel=\"noopener noreferrer nofollow\">\u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0430\u043a\u043a\u0430\u0443\u043d\u0442<\/a>. \u041f\u043e\u0441\u043b\u0435 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0430\u043a\u043a\u0430\u0443\u043d\u0442\u0430, \u0432 Services \u043c\u043e\u0436\u043d\u043e \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c Service Key. \u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u0435 \u0435\u0433\u043e. \u041c\u044b \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0444\u043e\u0440\u043c\u0430\u0442 <a href=\"https:\/\/dev.maxmind.com\/geoip\/geoip2\/geoip2-city-country-csv-databases\/\" rel=\"noopener noreferrer nofollow\">GeoLite2-City-CSV<\/a>. <\/p>\n<p>\u041f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 Service Key \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u0442\u044c \u0441\u043a\u0430\u0447\u0430\u0442\u044c \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445 \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 <code>curl<\/code><\/p>\n<pre><code>curl -o GeoLite2-City-CSV.zip \\   'https:\/\/download.maxmind.com\/app\/geoip_download?edition_id=GeoLite2-City-CSV&amp;license_key={{YOUR_LICENSE_KEY}}&amp;suffix=zip'<\/code><\/pre>\n<blockquote>\n<p>\u041f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0435 \u0438\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0438 \u043f\u043e \u0441\u043a\u0430\u0447\u0438\u0432\u0430\u043d\u0438\u044e GeoIP \u0431\u0430\u0437 \u0434\u0430\u043d\u043d\u044b\u0445 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0439\u0442\u0438 <a href=\"https:\/\/dev.maxmind.com\/geoip\/geoip-direct-downloads\/\" rel=\"noopener noreferrer nofollow\">\u0442\u0443\u0442<\/a>.<\/p>\n<\/blockquote>\n<h2>AWS Lambda \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0434\u043b\u044f \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f GeoIP \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u043d\u0430 S3<\/h2>\n<p>\u0414\u043b\u044f \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0433\u043e \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u044f \u0441\u043e\u0437\u0434\u0430\u043b S3 Bucket <code>s3:\/\/app.loshadki.data<\/code>, \u0433\u0434\u0435 \u044f \u043f\u043b\u0430\u043d\u0438\u0440\u0443\u044e \u0440\u0430\u0437\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445 GeoIP. \u0420\u0430\u0437\u043c\u0435\u0449\u0430\u0442\u044c \u0434\u0432\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u044f \u0431\u0443\u0434\u0443 \u043f\u043e \u043f\u0443\u0442\u044f\u043c<\/p>\n<ul>\n<li>\n<p><code>s3:\/\/app.loshadki.datadata\/geoip_blocks\/data.csv.gz <\/code>&#8212; \u0431\u0430\u0437\u0430 IP \u043c\u0430\u0441\u043e\u043a \u0438 \u0438\u0445 GEO \u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u0435<\/p>\n<\/li>\n<li>\n<p><code>s3:\/\/app.loshadki.datadata\/geoip_locations\/data.csv.gz<\/code> &#8212; \u0440\u0430\u0441\u0448\u0438\u0444\u0440\u043e\u0432\u043a\u0430 GEO  \u0432 \u0430\u0434\u0440\u0435\u0441\u0430 (\u0421\u0442\u0440\u0430\u043d\u044b, \u0413\u043e\u0440\u043e\u0434\u0430).<\/p>\n<\/li>\n<\/ul>\n<p>\u0421\u043e\u0437\u0434\u0430\u0439\u0442\u0435 \u043d\u043e\u0432\u0443\u044e Lambda \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u044f \u043d\u0430\u0437\u0432\u0430\u043b \u0441\u0432\u043e\u044e <code>GeoIP-Table-Update<\/code>, \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e <code>python:3.8<\/code>.<\/p>\n<p>\u042f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e Environment Variables \u0434\u043b\u044f \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043d\u0430\u0441\u0442\u0440\u043e\u0435\u043a \u0444\u0443\u043d\u043a\u0446\u0438\u0438:<\/p>\n<ul>\n<li>\n<p><code>MAXMIND_GEOIP_LICENSE<\/code> &#8212; \u0432\u0430\u0448 Service Key \u043e\u0442 MaxMind.<\/p>\n<\/li>\n<li>\n<p><code>S3_BUCKET_NAME<\/code> &#8212; S3 Bucket, \u043a\u0443\u0434\u0430 \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u0442\u0435\u0441\u044c \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0442\u044c \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445 (\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e <code>app.loshadki.data<\/code>).<\/p>\n<\/li>\n<li>\n<p><code>S3_BUCKET_PREFIX<\/code> &#8212; \u043f\u0440\u0435\u0444\u0438\u043a\u0441 \u0434\u043b\u044f \u0431\u0430\u0437 \u0434\u0430\u043d\u043d\u044b\u0445, \u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e <code>data<\/code><\/p>\n<\/li>\n<\/ul>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/4f8\/9f5\/4f6\/4f89f54f695c973a45bc762c041fb913.png\" width=\"834\" height=\"280\"><figcaption><\/figcaption><\/figure>\n<p>\u042d\u0442\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043c\u043e\u0436\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c\u0441\u044f \u0434\u043e\u043b\u0433\u043e\u0435 \u0432\u0440\u0435\u043c\u044f. \u0412\u044b\u0441\u0442\u0430\u0432\u0438\u0442\u0435 Timeout \u0432 5 \u043c\u0438\u043d\u0443\u0442. \u042f \u0442\u0430\u043a \u0436\u0435 \u0438\u0437\u043c\u0435\u043d\u0438\u043b Memory \u0432 256MB, \u0442\u0430\u043a \u043a\u0430\u043a \u0441 \u0431\u043e\u043b\u044c\u0448\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u044c\u044e \u0432\u044b\u0434\u0435\u043b\u044f\u0435\u0442\u0441\u044f \u0442\u0430\u043a \u0436\u0435 \u0431\u043e\u043b\u044c\u0448\u0435 CPU, \u0438 \u044f \u0447\u0430\u0441\u0442\u043e \u0437\u0430\u043c\u0435\u0447\u0430\u043b \u0447\u0442\u043e \u0434\u0435\u0448\u0435\u0432\u043b\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0441 \u0431\u043e\u043b\u0435\u0435 \u0431\u044b\u0441\u0442\u0440\u044b\u043c CPU, \u0434\u0430\u0436\u0435 \u0435\u0441\u043b\u0438 \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0432\u0441\u044f \u043f\u0430\u043c\u044f\u0442\u044c. \u041c\u044b \u0431\u0443\u0434\u0435\u043c \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0442\u044c \u044d\u0442\u0443 \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0440\u0430\u0437 \u0432 \u043d\u0435\u0434\u0435\u043b\u044e, \u0442\u0430\u043a \u0447\u0442\u043e \u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u0440\u0430\u0437\u043d\u0438\u0446\u044b \u043d\u0435 \u0431\u0443\u0434\u0435\u0442, \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u043f\u043b\u0430\u0442\u0438\u0442\u044c.<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/ef7\/221\/5af\/ef72215af57f50323a5cdf28ef002ca6.png\" width=\"422\" height=\"466\"><figcaption><\/figcaption><\/figure>\n<p>\u0414\u043b\u044f \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u044d\u0442\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0440\u0430\u0437 \u0432 \u043d\u0435\u0434\u0435\u043b\u044e \u0441\u043e\u0437\u0434\u0430\u0439\u0442\u0435 trigger. \u0412\u044b\u0431\u0435\u0440\u0435\u0442\u0435 <em>EventBridge (Cloud Watch Events)<\/em>, \u0441\u043e\u0437\u0434\u0430\u0439\u0442\u0435 \u043d\u043e\u0432\u043e\u0435 \u043f\u0440\u0430\u0432\u0438\u043b\u043e <code>upload-geoip-to-s3-weekly<\/code> \u0441 \u043f\u0440\u0430\u0432\u0438\u043b\u043e\u043c <code>rate(7 days)<\/code>.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/ba4\/6b3\/fbf\/ba46b3fbf8c21d780b785eaa71480e34.png\" width=\"833\" height=\"612\"><figcaption><\/figcaption><\/figure>\n<p>\u0414\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b AWS Lambda \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043c\u043e\u0433\u043b\u0430 \u043f\u0438\u0441\u0430\u0442\u044c \u043d\u0430 S3, \u043c\u044b \u0442\u0430\u043a \u0436\u0435 \u0434\u043e\u043b\u0436\u043d\u044b \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0440\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u0438\u044f, \u044f \u043e\u0431\u044b\u0447\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u044e Role \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u0443\u044e \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438. \u0414\u043e\u0431\u0430\u0432\u044c\u0442\u0435 \u0440\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u0438\u0435<\/p>\n<pre><code>{   \"Version\": \"2012-10-17\",   \"Statement\": [     {       \"Effect\": \"Allow\",       \"Action\": \"s3:PutObject\",       \"Resource\": \"arn:aws:s3:::app.loshadki.data\/data\/*\"     }   ] }<\/code><\/pre>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/4a9\/46b\/f69\/4a946bf69e3bab5ceb3beb2e92fe6bb3.png\" width=\"829\" height=\"333\"><figcaption><\/figcaption><\/figure>\n<h2>\u0418\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434 \u0444\u0443\u043d\u043a\u0446\u0438\u0438<\/h2>\n<p>\u0418\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043d\u0438\u0436\u0443. \u0412\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u0440\u043e\u0441\u0442\u043e \u0441\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438 \u0432\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0435\u0433\u043e \u0432 \u0440\u0435\u0434\u0430\u043a\u0442\u043e\u0440, \u0441\u0434\u0435\u043b\u0430\u0442\u044c Deploy \u0438 \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c. \u0424\u0443\u043d\u043a\u0446\u0438\u044e \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043c\u0438\u043d\u0443\u0442. \u041f\u043e \u043e\u043a\u043e\u043d\u0447\u0430\u043d\u0438\u0438, \u0435\u0441\u043b\u0438 \u0432\u0441\u0435 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u0432\u0435\u0440\u043d\u044b, \u0432\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0443\u0432\u0438\u0434\u0435\u0442\u044c \u0444\u0430\u0439\u043b\u044b \u043d\u0430 S3.<\/p>\n<pre><code>import os import os.path import urllib.request import shutil import zipfile import tempfile import gzip import boto3  def lambda_handler(event, context):     with tempfile.TemporaryDirectory() as tmpdirname:         zipfilename = os.path.join(tmpdirname, 'GeoLite2-City-CSV.zip')          print('step 1 - download geolite ip database')         download_geo_ip(tmpdirname, zipfilename)         print('step 2 - unzip all files')         unzip_all(tmpdirname, zipfilename)         print('step 3 - gzip files')         gzip_files(tmpdirname)         print('step 4 - upload to s3')         upload_to_s3(tmpdirname)      return  def download_geo_ip(tmpdirname, zipfilename):     geoip_url = 'https:\/\/download.maxmind.com\/app\/geoip_download?edition_id=GeoLite2-City-CSV&amp;license_key={}&amp;suffix=zip'.         format(os.getenv('MAXMIND_GEOIP_LICENSE'))      with urllib.request.urlopen(geoip_url) as response, open(zipfilename, 'wb') as output:         shutil.copyfileobj(response, output)   def unzip_all(tmpdirname, zipfilename):     # unzip all, but without the directories, to easily find the files     with zipfile.ZipFile(zipfilename, 'r') as z:         for member in z.namelist():             filename = os.path.basename(member)              # if a directory, skip             if not filename:                 continue              # copy file (taken from zipfile's extract)             with z.open(member) as zobj:                 with open(os.path.join(tmpdirname, filename), \"wb\") as targetobj:                     shutil.copyfileobj(zobj, targetobj)   def gzip_files(tmpdirname):     for filename in ['GeoLite2-City-Blocks-IPv4.csv', 'GeoLite2-City-Locations-en.csv']:         file_path = os.path.join(tmpdirname, filename)         with open(file_path, 'rb') as f_in,                 gzip.open(file_path + '.gz', 'wb') as f_out:             shutil.copyfileobj(f_in, f_out)   def upload_to_s3(tmpdirname):     s3_bucket_name = os.getenv('S3_BUCKET_NAME')     s3_bucket_prefix = os.getenv('S3_BUCKET_PREFIX')      s3_client = boto3.client('s3')     s3_client.upload_file(         os.path.join(tmpdirname, 'GeoLite2-City-Blocks-IPv4.csv.gz'),         s3_bucket_name,         os.path.join(s3_bucket_prefix, 'geoip_blocks\/data.csv.gz')     )     s3_client.upload_file(         os.path.join(tmpdirname, 'GeoLite2-City-Locations-en.csv.gz'),         s3_bucket_name,         os.path.join(s3_bucket_prefix, 'geoip_locations\/data.csv.gz')     )<\/code><\/pre>\n<h2>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0442\u0430\u0431\u043b\u0438\u0446 AWS Athena<\/h2>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0432 AWS Athena \u043d\u0430 \u0431\u0430\u0437\u0435 CSV \u0444\u0430\u0439\u043b\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u044b \u0442\u043e\u043b\u044c\u043a\u043e \u0447\u0442\u043e \u0441\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043b\u0438 \u043d\u0430 S3.<\/p>\n<p>\u041f\u0435\u0440\u0432\u0430\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u044d\u0442\u043e IP \u0430\u0434\u0440\u0435\u0441\u0430 (\u043d\u0435 \u0437\u0430\u0431\u0443\u0434\u044c\u0442\u0435 \u043f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u0440\u0430\u0441\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043d\u0430 S3, \u0433\u0434\u0435 \u0432\u044b \u0445\u0440\u0430\u043d\u0438\u0442\u0435 CSV \u0444\u0430\u0439\u043b\u044b)<\/p>\n<pre><code>CREATE EXTERNAL TABLE IF NOT EXISTS default.geoip_blocks (   network STRING,   geoname_id INT,   registered_country_geoname_id INT,   represented_country_geoname_id INT,   is_anonymous_proxy INT,   is_satellite_provider INT,   postal_code STRING,   latitude DOUBLE,   longitude DOUBLE,   accuracy_radius INT ) ROW FORMAT DELIMITED   FIELDS TERMINATED BY ','   LINES TERMINATED BY '\\n' LOCATION 's3:\/\/app.loshadki.data\/data\/geoip_blocks\/' TBLPROPERTIES ('skip.header.line.count'='1');<\/code><\/pre>\n<p>\u0412\u0442\u043e\u0440\u0430\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u044d\u0442\u043e \u0440\u0430\u0441\u0448\u0438\u0444\u0440\u043e\u0432\u043a\u0430 \u043a \u0441\u0442\u0440\u0430\u043d\u0430\u043c \u0438 \u0433\u043e\u0440\u043e\u0434\u0430\u043c (\u043e\u043f\u044f\u0442\u044c \u043d\u0435 \u0437\u0430\u0431\u0443\u0434\u044c\u0442\u0435 \u043f\u043e\u043c\u0435\u043d\u044f\u0442\u044c S3 \u043f\u0443\u0442\u044c)<\/p>\n<pre><code>CREATE EXTERNAL TABLE IF NOT EXISTS default.geoip_locations (   geoname_id INT,   locale_code STRING,   continent_code STRING,   continent_name STRING,   country_iso_code STRING,   country_name STRING,   subdivision_1_iso_code STRING,   subdivision_1_name STRING,   subdivision_2_iso_code STRING,   subdivision_2_name STRING,   city_name STRING,   metro_code STRING,   time_zone STRING,   is_in_european_union INT ) ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.OpenCSVSerde' WITH SERDEPROPERTIES (    'separatorChar' = ',',    'quoteChar' = '\\\"',    'escapeChar' = '\\\\' ) LOCATION 's3:\/\/app.loshadki.data\/data\/geoip_locations\/' TBLPROPERTIES ('skip.header.line.count'='1');<\/code><\/pre>\n<p>\u041c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u0442\u044c \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u0441\u0442\u043e\u0439 SQL \u0437\u0430\u043f\u0440\u043e\u0441<\/p>\n<pre><code>select *  from  default.geoip_blocks t1   inner join default.geoip_locations t2 on t1.geoname_id = t2.geoname_id limit 10<\/code><\/pre>\n<h2>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0442\u0430\u0431\u043b\u0438\u0446 \u0434\u043b\u044f \u043f\u043e\u0438\u0441\u043a\u0430 \u0430\u0434\u0440\u0435\u0441\u0430 \u043f\u043e IP \u0430\u0434\u0440\u0435\u0441\u0443 (CIDR lookup)<\/h2>\n<p>\u0422\u0430\u0431\u043b\u0438\u0446\u0430 <code>geoip_blocks<\/code> \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442 \u0430\u0434\u0440\u0435\u0441\u0430 \u0431\u043b\u043e\u043a\u0430\u043c\u0438 \u0432 CIDR \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u043a\u0435, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 <code>1.0.0.0\/24<\/code>, \u0447\u0442\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442 \u0432\u0441\u0435 \u0430\u0434\u0440\u0435\u0441\u0430 \u043e\u0442 <code>1.0.0.0<\/code> \u0434\u043e <code>1.0.0.255<\/code>. \u0422\u0435\u043a\u0443\u0449\u0430\u044f \u0432\u0435\u0440\u0441\u0438\u044f Presto <a href=\"https:\/\/prestodb.io\/docs\/current\/functions\/ip.html\" rel=\"noopener noreferrer nofollow\">\u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442<\/a> \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0434\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0435\u0441\u043b\u0438 IP \u0430\u0434\u0440\u0435\u0441 \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u0434\u043b\u044f CIDR \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u043a\u0438. \u041d\u043e \u043a \u0441\u043e\u0436\u0430\u043b\u0435\u043d\u0438\u044e AWS Athena (<a href=\"https:\/\/docs.aws.amazon.com\/athena\/latest\/ug\/engine-versions-reference.html\" rel=\"noopener noreferrer nofollow\">\u0434\u0430\u0436\u0435 \u0432\u0435\u0440\u0441\u0438\u044f 2<\/a>) \u0434\u043e \u0441\u0438\u0445 \u043f\u043e\u0440 \u043d\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u044d\u0442\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u0442\u0430\u043a \u043a\u0430\u043a \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 Presto <a href=\"https:\/\/prestodb.io\/docs\/0.217\/index.html\" rel=\"noopener noreferrer nofollow\">0.217<\/a>. \u041d\u043e \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043d\u0430\u0439\u0442\u0438 \u0434\u0440\u0443\u0433\u043e\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438.<\/p>\n<p>\u041e\u0434\u0438\u043d \u0438\u0437 \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u044d\u0442\u043e \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435 IP \u0430\u0434\u0440\u0435\u0441\u043e\u0432 \u0432 Integer, \u0447\u0442\u043e\u0431\u044b \u0431\u044b\u043b\u0430 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u0432\u0440\u043e\u0434\u0435 <code>ip_start &lt;= ip_address &lt;= ip_end<\/code>. \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c <a href=\"https:\/\/en.wikipedia.org\/wiki\/IPv4#Address_representations\" rel=\"noopener noreferrer nofollow\">IP \u0430\u0434\u0440\u0435\u0441 \u0432 Integer<\/a> \u043e\u0447\u0435\u043d\u044c \u043b\u0435\u0433\u043a\u043e, \u0444\u043e\u0440\u043c\u0443\u043b\u0430 \u043f\u0440\u043e\u0441\u0442\u0430\u044f <code>ipv4[1]*256*256*256 + ipv4[2]*256*256 + ipv4[3]*256 + ipv4[4]<\/code>. \u041d\u0443 \u0438 \u043c\u0430\u0441\u043a\u0443 <code>\/24<\/code> \u043f\u0440\u043e\u0441\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0432 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 IP \u0430\u0434\u0440\u0435\u0441 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430.<\/p>\n<p>\u0421\u0430\u043c\u044b\u0439 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u044d\u0442\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c View \u043d\u0430 \u0431\u0430\u0437\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u044b <code>geoip_blocks<\/code><\/p>\n<pre><code>CREATE OR REPLACE VIEW geoip_blocks_int AS select         cast(ip[1] as BIGINT)*256*256*256 + cast(ip[2] as BIGINT)*256*256 + cast(ip[3] as BIGINT)*256 + cast(ip[4] as BIGINT) as ip_start,         (             bitwise_or(cast(ip[1] as BIGINT), bitwise_and(255, cast(power(2, greatest(8 - range, 0)) as BIGINT)-1))             )*256*256*256 +         (             bitwise_or(cast(ip[2] as BIGINT), bitwise_and(255, cast(power(2, greatest(16 - range, 0)) as BIGINT)-1))             )*256*256 +         (             bitwise_or(cast(ip[3] as BIGINT), bitwise_and(255, cast(power(2, greatest(24 - range, 0)) as BIGINT)-1))             )*256+         (             bitwise_or(cast(ip[4] as BIGINT), bitwise_and(255, cast(power(2, greatest(32 - range, 0)) as BIGINT)-1))             ) as ip_end,         network,         geoname_id,         registered_country_geoname_id,         represented_country_geoname_id,         cast(is_anonymous_proxy as BOOLEAN) as is_anonymous_proxy,         cast(is_satellite_provider as BOOLEAN) as is_satellite_provider,         postal_code,         latitude,         longitude,         accuracy_radius from     (         select             network,             geoname_id,             registered_country_geoname_id,             represented_country_geoname_id,             is_anonymous_proxy,             is_satellite_provider,             postal_code,             latitude,             longitude,             accuracy_radius,             split(network_array[1], '.') as ip,             cast(network_array[2] as BIGINT) as range         from             (                 select                     network,                     geoname_id,                     registered_country_geoname_id,                     represented_country_geoname_id,                     is_anonymous_proxy,                     is_satellite_provider,                     postal_code,                     latitude,                     longitude,                     accuracy_radius,                     split(network, '\/') as network_array                 from default.geoip_blocks             )     )<\/code><\/pre>\n<h2>\u041f\u0440\u043e\u0431\u0443\u0435\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b<\/h2>\n<p>\u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u0442\u044c \u043d\u0430\u0439\u0442\u0438 \u043c\u0435\u0441\u0442\u043e\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u0435 IP \u0430\u0434\u0440\u0435\u0441\u0430 <code>1.1.1.1<\/code>. \u041d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0432 Integer \u043e\u043f\u044f\u0442\u044c.<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/c84\/9f0\/c4f\/c849f0c4f51468c4e8b596b04a3a124a.png\" width=\"1231\" height=\"669\"><figcaption><\/figcaption><\/figure>\n<pre><code>with ips as (     select         (                 cast(ip_array[1] as BIGINT)*256*256*256 +                 cast(ip_array[2] as BIGINT)*256*256 +                 cast(ip_array[3] as BIGINT)*256 +                 cast(ip_array[4] as BIGINT)             ) as ip_int,         ip     from (              select                  '1.1.1.1' as ip,                  split('1.1.1.1', '.') as ip_array          ) as source ) select     ips.ip,     locations.continent_name,     locations.country_name,     locations.city_name,     locations.time_zone from     ips as ips         left join geoip_blocks_int as blocks on blocks.ip_start &lt;= ips.ip_int and ips.ip_int &lt;= blocks.ip_end         left join geoip_locations as locations on blocks.geoname_id = locations.geoname_id<\/code><\/pre>\n<p>\u041d\u0443 \u0438 \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0431\u043e\u043b\u0435\u0435 \u0441\u043b\u043e\u0436\u043d\u044b\u0439 SQL \u0437\u0430\u043f\u0440\u043e\u0441, \u0435\u0441\u043b\u0438 \u0443 \u0432\u0430\u0441 \u0445\u0440\u0430\u043d\u044f\u0442\u0441\u044f \u043b\u043e\u0433\u0438 \u043e\u0442 CloudFront, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c \u0441\u0430\u043c\u044b\u0435 \u043f\u043e\u043f\u0443\u043b\u044f\u0440\u043d\u044b\u0435 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b \u0441 \u0433\u0440\u0443\u043f\u043f\u0438\u0440\u043e\u0432\u043a\u043e\u0439 \u043f\u043e \u0441\u0442\u0440\u0430\u043d\u0430\u043c \u0438 \u0433\u043e\u0440\u043e\u0434\u0430\u043c.<\/p>\n<pre><code>with access_logs as (   select     uri,     (       cast(split(ip, '.')[1] as BIGINT)*256*256*256 +        cast(split(ip, '.')[2] as BIGINT)*256*256 +        cast(split(ip, '.')[3] as BIGINT)*256 +        cast(split(ip, '.')[4] as BIGINT)     ) as ip_int   from (     select  uri,       case xforwarded_for         when '-' then request_ip         else xforwarded_for       end as ip     from access_logs_yesterday     where        sc_content_type = 'text\/html'        and status = 200        and method = 'GET'       and not regexp_like(url_decode(user_agent), '(bot|spider)')   ) ) select     count(*) as count,     access_logs.uri as uri,     locations.continent_name,     locations.country_name,     locations.city_name,     locations.time_zone from     access_logs     left join geoip_blocks_int as blocks on        blocks.ip_start &lt;= access_logs.ip_int and access_logs.ip_int &lt;= blocks.ip_end     left join geoip_locations as locations on blocks.geoname_id = locations.geoname_id group by 2, 3, 4, 5, 6 order by 1<\/code><\/pre>\n<h2>\u0427\u0442\u043e \u0434\u0430\u043b\u044c\u0448\u0435?<\/h2>\n<p>\u0412\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u043b\u043e\u043d\u043a\u0438 <code>postal_code<\/code> \u0438\u043b\u0438 <code>city_name<\/code> \u0432\u043c\u0435\u0441\u0442\u0435 \u0441 <code>country_name<\/code> \u0432\u043c\u0435\u0441\u0442\u0435 \u0441 AWS QuickSight \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043e\u0442\u0447\u0435\u0442\u043e\u0432. \u042f \u0442\u0430\u043a \u0436\u0435 \u0441\u043e\u0437\u0434\u0430\u043b \u0434\u043b\u044f \u0441\u0435\u0431\u044f CloudWatch Alert, \u0435\u0441\u043b\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0443\u043f\u0430\u0434\u0435\u0442 \u0431\u043e\u043b\u044c\u0448\u0435 2\u0445 \u0440\u0430\u0437, \u0447\u0442\u043e\u0431\u044b \u0437\u043d\u0430\u0442\u044c \u0435\u0441\u043b\u0438 \u0447\u0442\u043e-\u0442\u043e \u0441\u043b\u043e\u043c\u0430\u043b\u043e\u0441\u044c.<\/p>\n<\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/538034\/\"> https:\/\/habr.com\/ru\/post\/538034\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text_v2\" id=\"post-content-body\">\n<p><em>PS. \u042d\u0442\u043e \u043f\u0435\u0440\u0435\u0432\u043e\u0434 \u043c\u043e\u0435\u0439 \u0441\u0442\u0430\u0442\u044c\u0438 \u043d\u0430 \u0430\u043d\u0433\u043b\u0438\u0439\u0441\u043a\u043e\u043c. \u0414\u0430\u0432\u043d\u043e \u044f \u043d\u0435 \u043f\u0438\u0441\u0430\u043b \u043d\u0430 \u0425\u0430\u0431\u0440\u0435. \u0421\u0440\u0430\u0437\u0443 \u043f\u0440\u043e\u0449\u0443 \u043f\u0440\u043e\u0449\u0435\u043d\u0438\u044f, \u043c\u043d\u043e\u0433\u043e \u043d\u0430 \u0440\u0443\u0441\u0441\u043a\u043e\u043c \u043d\u0435 \u043f\u0438\u0448\u0443. \u041d\u0435 \u0441\u043a\u0430\u0436\u0443 \u0447\u0442\u043e \u0443 \u043c\u0435\u043d\u044f \u0438 \u0430\u043d\u0433\u043b\u0438\u0439\u0441\u043a\u0438\u0439 \u0448\u0438\u043a\u0430\u0440\u043d\u044b\u0439. \u041d\u043e \u043a \u0441\u043e\u0436\u0430\u043b\u0435\u043d\u0438\u044e \u043f\u0440\u043e\u0436\u0438\u0432\u0430\u043d\u0438\u0435 \u0437\u0430 \u0440\u0443\u0431\u0435\u0436\u043e\u043c \u0443\u0445\u0443\u0434\u0448\u0430\u0435\u0442 \u043c\u043e\u0439 \u0440\u0443\u0441\u0441\u043a\u0438\u0439 \u0438 \u043c\u0435\u0434\u043b\u0435\u043d\u043d\u043e \u0440\u0430\u0437\u0432\u0438\u0432\u0430\u0435\u0442 \u0430\u043d\u0433\u043b\u0438\u0439\u0441\u043a\u0438\u0439. <\/em><\/p>\n<p>\u0415\u0441\u043b\u0438 \u0432\u044b \u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0435\u0441\u044c AWS Athena \u0434\u043b\u044f \u0430\u043d\u0430\u043b\u0438\u0437\u0430 \u043b\u043e\u0433\u043e\u0432, \u0442\u043e \u0447\u0430\u0441\u0442\u043e \u0445\u043e\u0447\u0435\u0442\u0441\u044f \u043d\u0430\u0439\u0442\u0438 \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a IP \u0430\u0434\u0440\u0435\u0441\u043e\u0432. \u041a \u0441\u043e\u0436\u0430\u043b\u0435\u043d\u0438\u044e AWS Athena \u043d\u0435 \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u044d\u0442\u043e\u0433\u043e \u0438\u0437 \u043a\u043e\u0440\u043e\u0431\u043a\u0438. \u041a \u0441\u0447\u0430\u0441\u0442\u044c\u044e MaxMind \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445 GeoIP \u0442\u0430\u0431\u043b\u0438\u0446, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u043c\u0435\u0441\u0442\u043e\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043f\u043e IP \u0430\u0434\u0440\u0435\u0441\u0430\u043c. \u0415\u0441\u0442\u044c \u043f\u043b\u0430\u0442\u043d\u0430\u044f \u0438 \u0431\u0435\u0441\u043f\u043b\u0430\u0442\u043d\u0430\u044f \u0432\u0435\u0440\u0441\u0438\u044f.<\/p>\n<p>\u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u043f\u043e\u043a\u0430\u0436\u0443 \u043a\u0430\u043a \u0441\u043e\u0437\u0434\u0430\u0442\u044c AWS Lambda \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043a\u0430\u0436\u0434\u0443\u044e \u043d\u0435\u0434\u0435\u043b\u044e \u0431\u0443\u0434\u0435\u0442 \u0441\u043a\u0430\u0447\u0438\u0432\u0430\u0442\u044c \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u044e\u044e \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445 \u0441 MaxMind \u043d\u0430 S3. \u042d\u0442\u0443 \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432 AWS Athena \u0434\u043b\u044f \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f SQL \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u0434\u043b\u044f \u0430\u043d\u0430\u043b\u0438\u0437\u0430, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432\u0435\u0431 \u043b\u043e\u0433\u043e\u0432.<\/p>\n<h2>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0430\u043a\u043a\u0430\u0443\u043d\u0442\u0430 \u043d\u0430 MaxMind<\/h2>\n<p>\u0414\u043b\u044f \u0441\u043a\u0430\u0447\u0438\u0432\u0430\u043d\u0438\u044f \u0434\u0430\u0436\u0435 <a href=\"https:\/\/dev.maxmind.com\/geoip\/geoip2\/geolite2\/\" rel=\"noopener noreferrer nofollow\">\u0431\u0435\u0441\u043f\u043b\u0430\u0442\u043d\u044b\u0445 \u0431\u0430\u0437 \u0434\u0430\u043d\u043d\u044b\u0445<\/a> GeoLite 2 \u0441 MaxMind \u0432\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0431\u0443\u0434\u0435\u0442 <a href=\"https:\/\/www.maxmind.com\/\" rel=\"noopener noreferrer nofollow\">\u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0430\u043a\u043a\u0430\u0443\u043d\u0442<\/a>. \u041f\u043e\u0441\u043b\u0435 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0430\u043a\u043a\u0430\u0443\u043d\u0442\u0430, \u0432 Services \u043c\u043e\u0436\u043d\u043e \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c Service Key. \u0421\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u0435 \u0435\u0433\u043e. \u041c\u044b \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0444\u043e\u0440\u043c\u0430\u0442 <a href=\"https:\/\/dev.maxmind.com\/geoip\/geoip2\/geoip2-city-country-csv-databases\/\" rel=\"noopener noreferrer nofollow\">GeoLite2-City-CSV<\/a>. <\/p>\n<p>\u041f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 Service Key \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u0442\u044c \u0441\u043a\u0430\u0447\u0430\u0442\u044c \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445 \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 <code>curl<\/code><\/p>\n<pre><code>curl -o GeoLite2-City-CSV.zip \\   'https:\/\/download.maxmind.com\/app\/geoip_download?edition_id=GeoLite2-City-CSV&amp;license_key={{YOUR_LICENSE_KEY}}&amp;suffix=zip'<\/code><\/pre>\n<blockquote>\n<p>\u041f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0435 \u0438\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0438 \u043f\u043e \u0441\u043a\u0430\u0447\u0438\u0432\u0430\u043d\u0438\u044e GeoIP \u0431\u0430\u0437 \u0434\u0430\u043d\u043d\u044b\u0445 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0439\u0442\u0438 <a href=\"https:\/\/dev.maxmind.com\/geoip\/geoip-direct-downloads\/\" rel=\"noopener noreferrer nofollow\">\u0442\u0443\u0442<\/a>.<\/p>\n<\/blockquote>\n<h2>AWS Lambda \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0434\u043b\u044f \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f GeoIP \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u043d\u0430 S3<\/h2>\n<p>\u0414\u043b\u044f \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0433\u043e \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u044f \u0441\u043e\u0437\u0434\u0430\u043b S3 Bucket <code>s3:\/\/app.loshadki.data<\/code>, \u0433\u0434\u0435 \u044f \u043f\u043b\u0430\u043d\u0438\u0440\u0443\u044e \u0440\u0430\u0437\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445 GeoIP. \u0420\u0430\u0437\u043c\u0435\u0449\u0430\u0442\u044c \u0434\u0432\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u044f \u0431\u0443\u0434\u0443 \u043f\u043e \u043f\u0443\u0442\u044f\u043c<\/p>\n<ul>\n<li>\n<p><code>s3:\/\/app.loshadki.datadata\/geoip_blocks\/data.csv.gz <\/code>&#8212; \u0431\u0430\u0437\u0430 IP \u043c\u0430\u0441\u043e\u043a \u0438 \u0438\u0445 GEO \u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u0435<\/p>\n<\/li>\n<li>\n<p><code>s3:\/\/app.loshadki.datadata\/geoip_locations\/data.csv.gz<\/code> &#8212; \u0440\u0430\u0441\u0448\u0438\u0444\u0440\u043e\u0432\u043a\u0430 GEO  \u0432 \u0430\u0434\u0440\u0435\u0441\u0430 (\u0421\u0442\u0440\u0430\u043d\u044b, \u0413\u043e\u0440\u043e\u0434\u0430).<\/p>\n<\/li>\n<\/ul>\n<p>\u0421\u043e\u0437\u0434\u0430\u0439\u0442\u0435 \u043d\u043e\u0432\u0443\u044e Lambda \u0444\u0443\u043d\u043a\u0446\u0438\u044e, \u044f \u043d\u0430\u0437\u0432\u0430\u043b \u0441\u0432\u043e\u044e <code>GeoIP-Table-Update<\/code>, \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e <code>python:3.8<\/code>.<\/p>\n<p>\u042f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e Environment Variables \u0434\u043b\u044f \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043d\u0430\u0441\u0442\u0440\u043e\u0435\u043a \u0444\u0443\u043d\u043a\u0446\u0438\u0438:<\/p>\n<ul>\n<li>\n<p><code>MAXMIND_GEOIP_LICENSE<\/code> &#8212; \u0432\u0430\u0448 Service Key \u043e\u0442 MaxMind.<\/p>\n<\/li>\n<li>\n<p><code>S3_BUCKET_NAME<\/code> &#8212; S3 Bucket, \u043a\u0443\u0434\u0430 \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u0442\u0435\u0441\u044c \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0442\u044c \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445 (\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e <code>app.loshadki.data<\/code>).<\/p>\n<\/li>\n<li>\n<p><code>S3_BUCKET_PREFIX<\/code> &#8212; \u043f\u0440\u0435\u0444\u0438\u043a\u0441 \u0434\u043b\u044f \u0431\u0430\u0437 \u0434\u0430\u043d\u043d\u044b\u0445, \u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e <code>data<\/code><\/p>\n<\/li>\n<\/ul>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u042d\u0442\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u043c\u043e\u0436\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c\u0441\u044f \u0434\u043e\u043b\u0433\u043e\u0435 \u0432\u0440\u0435\u043c\u044f. \u0412\u044b\u0441\u0442\u0430\u0432\u0438\u0442\u0435 Timeout \u0432 5 \u043c\u0438\u043d\u0443\u0442. \u042f \u0442\u0430\u043a \u0436\u0435 \u0438\u0437\u043c\u0435\u043d\u0438\u043b Memory \u0432 256MB, \u0442\u0430\u043a \u043a\u0430\u043a \u0441 \u0431\u043e\u043b\u044c\u0448\u0435\u0439 \u043f\u0430\u043c\u044f\u0442\u044c\u044e \u0432\u044b\u0434\u0435\u043b\u044f\u0435\u0442\u0441\u044f \u0442\u0430\u043a \u0436\u0435 \u0431\u043e\u043b\u044c\u0448\u0435 CPU, \u0438 \u044f \u0447\u0430\u0441\u0442\u043e \u0437\u0430\u043c\u0435\u0447\u0430\u043b \u0447\u0442\u043e \u0434\u0435\u0448\u0435\u0432\u043b\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0441 \u0431\u043e\u043b\u0435\u0435 \u0431\u044b\u0441\u0442\u0440\u044b\u043c CPU, \u0434\u0430\u0436\u0435 \u0435\u0441\u043b\u0438 \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0432\u0441\u044f \u043f\u0430\u043c\u044f\u0442\u044c. \u041c\u044b \u0431\u0443\u0434\u0435\u043c \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0442\u044c \u044d\u0442\u0443 \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0440\u0430\u0437 \u0432 \u043d\u0435\u0434\u0435\u043b\u044e, \u0442\u0430\u043a \u0447\u0442\u043e \u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u0440\u0430\u0437\u043d\u0438\u0446\u044b \u043d\u0435 \u0431\u0443\u0434\u0435\u0442, \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u043f\u043b\u0430\u0442\u0438\u0442\u044c.<\/p>\n<figure class=\"\"><figcaption><\/figcaption><\/figure>\n<p>\u0414\u043b\u044f \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u044d\u0442\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0440\u0430\u0437 \u0432 \u043d\u0435\u0434\u0435\u043b\u044e \u0441\u043e\u0437\u0434\u0430\u0439\u0442\u0435 trigger. \u0412\u044b\u0431\u0435\u0440\u0435\u0442\u0435 <em>EventBridge (Cloud Watch Events)<\/em>, \u0441\u043e\u0437\u0434\u0430\u0439\u0442\u0435 \u043d\u043e\u0432\u043e\u0435 \u043f\u0440\u0430\u0432\u0438\u043b\u043e <code>upload-geoip-to-s3-weekly<\/code> \u0441 \u043f\u0440\u0430\u0432\u0438\u043b\u043e\u043c <code>rate(7 days)<\/code>.<\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u0414\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b AWS Lambda \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u043c\u043e\u0433\u043b\u0430 \u043f\u0438\u0441\u0430\u0442\u044c \u043d\u0430 S3, \u043c\u044b \u0442\u0430\u043a \u0436\u0435 \u0434\u043e\u043b\u0436\u043d\u044b \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0440\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u0438\u044f, \u044f \u043e\u0431\u044b\u0447\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u044e Role \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u0443\u044e \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438. \u0414\u043e\u0431\u0430\u0432\u044c\u0442\u0435 \u0440\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u0438\u0435<\/p>\n<pre><code>{   \"Version\": \"2012-10-17\",   \"Statement\": [     {       \"Effect\": \"Allow\",       \"Action\": \"s3:PutObject\",       \"Resource\": \"arn:aws:s3:::app.loshadki.data\/data\/*\"     }   ] }<\/code><\/pre>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<h2>\u0418\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434 \u0444\u0443\u043d\u043a\u0446\u0438\u0438<\/h2>\n<p>\u0418\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043d\u0438\u0436\u0443. \u0412\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u0440\u043e\u0441\u0442\u043e \u0441\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438 \u0432\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0435\u0433\u043e \u0432 \u0440\u0435\u0434\u0430\u043a\u0442\u043e\u0440, \u0441\u0434\u0435\u043b\u0430\u0442\u044c Deploy \u0438 \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c. \u0424\u0443\u043d\u043a\u0446\u0438\u044e \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043c\u0438\u043d\u0443\u0442. \u041f\u043e \u043e\u043a\u043e\u043d\u0447\u0430\u043d\u0438\u0438, \u0435\u0441\u043b\u0438 \u0432\u0441\u0435 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438 \u0432\u0435\u0440\u043d\u044b, \u0432\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0443\u0432\u0438\u0434\u0435\u0442\u044c \u0444\u0430\u0439\u043b\u044b \u043d\u0430 S3.<\/p>\n<pre><code>import os import os.path import urllib.request import shutil import zipfile import tempfile import gzip import boto3  def lambda_handler(event, context):     with tempfile.TemporaryDirectory() as tmpdirname:         zipfilename = os.path.join(tmpdirname, 'GeoLite2-City-CSV.zip')          print('step 1 - download geolite ip database')         download_geo_ip(tmpdirname, zipfilename)         print('step 2 - unzip all files')         unzip_all(tmpdirname, zipfilename)         print('step 3 - gzip files')         gzip_files(tmpdirname)         print('step 4 - upload to s3')         upload_to_s3(tmpdirname)      return  def download_geo_ip(tmpdirname, zipfilename):     geoip_url = 'https:\/\/download.maxmind.com\/app\/geoip_download?edition_id=GeoLite2-City-CSV&amp;license_key={}&amp;suffix=zip'.         format(os.getenv('MAXMIND_GEOIP_LICENSE'))      with urllib.request.urlopen(geoip_url) as response, open(zipfilename, 'wb') as output:         shutil.copyfileobj(response, output)   def unzip_all(tmpdirname, zipfilename):     # unzip all, but without the directories, to easily find the files     with zipfile.ZipFile(zipfilename, 'r') as z:         for member in z.namelist():             filename = os.path.basename(member)              # if a directory, skip             if not filename:                 continue              # copy file (taken from zipfile's extract)             with z.open(member) as zobj:                 with open(os.path.join(tmpdirname, filename), \"wb\") as targetobj:                     shutil.copyfileobj(zobj, targetobj)   def gzip_files(tmpdirname):     for filename in ['GeoLite2-City-Blocks-IPv4.csv', 'GeoLite2-City-Locations-en.csv']:         file_path = os.path.join(tmpdirname, filename)         with open(file_path, 'rb') as f_in,                 gzip.open(file_path + '.gz', 'wb') as f_out:             shutil.copyfileobj(f_in, f_out)   def upload_to_s3(tmpdirname):     s3_bucket_name = os.getenv('S3_BUCKET_NAME')     s3_bucket_prefix = os.getenv('S3_BUCKET_PREFIX')      s3_client = boto3.client('s3')     s3_client.upload_file(         os.path.join(tmpdirname, 'GeoLite2-City-Blocks-IPv4.csv.gz'),         s3_bucket_name,         os.path.join(s3_bucket_prefix, 'geoip_blocks\/data.csv.gz')     )     s3_client.upload_file(         os.path.join(tmpdirname, 'GeoLite2-City-Locations-en.csv.gz'),         s3_bucket_name,         os.path.join(s3_bucket_prefix, 'geoip_locations\/data.csv.gz')     )<\/code><\/pre>\n<h2>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0442\u0430\u0431\u043b\u0438\u0446 AWS Athena<\/h2>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0432 AWS Athena \u043d\u0430 \u0431\u0430\u0437\u0435 CSV \u0444\u0430\u0439\u043b\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u044b \u0442\u043e\u043b\u044c\u043a\u043e \u0447\u0442\u043e \u0441\u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043b\u0438 \u043d\u0430 S3.<\/p>\n<p>\u041f\u0435\u0440\u0432\u0430\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u044d\u0442\u043e IP \u0430\u0434\u0440\u0435\u0441\u0430 (\u043d\u0435 \u0437\u0430\u0431\u0443\u0434\u044c\u0442\u0435 \u043f\u043e\u043c\u0435\u043d\u044f\u0442\u044c \u0440\u0430\u0441\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043d\u0430 S3, \u0433\u0434\u0435 \u0432\u044b \u0445\u0440\u0430\u043d\u0438\u0442\u0435 CSV \u0444\u0430\u0439\u043b\u044b)<\/p>\n<pre><code>CREATE EXTERNAL TABLE IF NOT EXISTS default.geoip_blocks (   network STRING,   geoname_id INT,   registered_country_geoname_id INT,   represented_country_geoname_id INT,   is_anonymous_proxy INT,   is_satellite_provider INT,   postal_code STRING,   latitude DOUBLE,   longitude DOUBLE,   accuracy_radius INT ) ROW FORMAT DELIMITED   FIELDS TERMINATED BY ','   LINES TERMINATED BY '\\n' LOCATION 's3:\/\/app.loshadki.data\/data\/geoip_blocks\/' TBLPROPERTIES ('skip.header.line.count'='1');<\/code><\/pre>\n<p>\u0412\u0442\u043e\u0440\u0430\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u044d\u0442\u043e \u0440\u0430\u0441\u0448\u0438\u0444\u0440\u043e\u0432\u043a\u0430 \u043a \u0441\u0442\u0440\u0430\u043d\u0430\u043c \u0438 \u0433\u043e\u0440\u043e\u0434\u0430\u043c (\u043e\u043f\u044f\u0442\u044c \u043d\u0435 \u0437\u0430\u0431\u0443\u0434\u044c\u0442\u0435 \u043f\u043e\u043c\u0435\u043d\u044f\u0442\u044c S3 \u043f\u0443\u0442\u044c)<\/p>\n<pre><code>CREATE EXTERNAL TABLE IF NOT EXISTS default.geoip_locations (   geoname_id INT,   locale_code STRING,   continent_code STRING,   continent_name STRING,   country_iso_code STRING,   country_name STRING,   subdivision_1_iso_code STRING,   subdivision_1_name STRING,   subdivision_2_iso_code STRING,   subdivision_2_name STRING,   city_name STRING,   metro_code STRING,   time_zone STRING,   is_in_european_union INT ) ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.OpenCSVSerde' WITH SERDEPROPERTIES (    'separatorChar' = ',',    'quoteChar' = '\\\"',    'escapeChar' = '\\\\' ) LOCATION 's3:\/\/app.loshadki.data\/data\/geoip_locations\/' TBLPROPERTIES ('skip.header.line.count'='1');<\/code><\/pre>\n<p>\u041c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u0442\u044c \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u0441\u0442\u043e\u0439 SQL \u0437\u0430\u043f\u0440\u043e\u0441<\/p>\n<pre><code>select *  from  default.geoip_blocks t1   inner join default.geoip_locations t2 on t1.geoname_id = t2.geoname_id limit 10<\/code><\/pre>\n<h2>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0442\u0430\u0431\u043b\u0438\u0446 \u0434\u043b\u044f \u043f\u043e\u0438\u0441\u043a\u0430 \u0430\u0434\u0440\u0435\u0441\u0430 \u043f\u043e IP \u0430\u0434\u0440\u0435\u0441\u0443 (CIDR lookup)<\/h2>\n<p>\u0422\u0430\u0431\u043b\u0438\u0446\u0430 <code>geoip_blocks<\/code> \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442 \u0430\u0434\u0440\u0435\u0441\u0430 \u0431\u043b\u043e\u043a\u0430\u043c\u0438 \u0432 CIDR \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u043a\u0435, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 <code>1.0.0.0\/24<\/code>, \u0447\u0442\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442 \u0432\u0441\u0435 \u0430\u0434\u0440\u0435\u0441\u0430 \u043e\u0442 <code>1.0.0.0<\/code> \u0434\u043e <code>1.0.0.255<\/code>. \u0422\u0435\u043a\u0443\u0449\u0430\u044f \u0432\u0435\u0440\u0441\u0438\u044f Presto <a href=\"https:\/\/prestodb.io\/docs\/current\/functions\/ip.html\" rel=\"noopener noreferrer nofollow\">\u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442<\/a> \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0434\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0435\u0441\u043b\u0438 IP \u0430\u0434\u0440\u0435\u0441 \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u0434\u043b\u044f CIDR \u043a\u043e\u0434\u0438\u0440\u043e\u0432\u043a\u0438. \u041d\u043e \u043a \u0441\u043e\u0436\u0430\u043b\u0435\u043d\u0438\u044e AWS Athena (<a href=\"https:\/\/docs.aws.amazon.com\/athena\/latest\/ug\/engine-versions-reference.html\" rel=\"noopener noreferrer nofollow\">\u0434\u0430\u0436\u0435 \u0432\u0435\u0440\u0441\u0438\u044f 2<\/a>) \u0434\u043e \u0441\u0438\u0445 \u043f\u043e\u0440 \u043d\u0435 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u044d\u0442\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u0442\u0430\u043a \u043a\u0430\u043a \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 Presto <a href=\"https:\/\/prestodb.io\/docs\/0.217\/index.html\" rel=\"noopener noreferrer nofollow\">0.217<\/a>. \u041d\u043e \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043d\u0430\u0439\u0442\u0438 \u0434\u0440\u0443\u0433\u043e\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438.<\/p>\n<p>\u041e\u0434\u0438\u043d \u0438\u0437 \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u044d\u0442\u043e \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435 IP \u0430\u0434\u0440\u0435\u0441\u043e\u0432 \u0432 Integer, \u0447\u0442\u043e\u0431\u044b \u0431\u044b\u043b\u0430 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u0432\u0440\u043e\u0434\u0435 <code>ip_start &lt;= ip_address &lt;= ip_end<\/code>. \u041f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c <a href=\"https:\/\/en.wikipedia.org\/wiki\/IPv4#Address_representations\" rel=\"noopener noreferrer nofollow\">IP \u0430\u0434\u0440\u0435\u0441 \u0432 Integer<\/a> \u043e\u0447\u0435\u043d\u044c \u043b\u0435\u0433\u043a\u043e, \u0444\u043e\u0440\u043c\u0443\u043b\u0430 \u043f\u0440\u043e\u0441\u0442\u0430\u044f <code>ipv4[1]*256*256*256 + ipv4[2]*256*256 + ipv4[3]*256 + ipv4[4]<\/code>. \u041d\u0443 \u0438 \u043c\u0430\u0441\u043a\u0443 <code>\/24<\/code> \u043f\u0440\u043e\u0441\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0432 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 IP \u0430\u0434\u0440\u0435\u0441 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u0430.<\/p>\n<p>\u0421\u0430\u043c\u044b\u0439 \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u044d\u0442\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c View \u043d\u0430 \u0431\u0430\u0437\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u044b <code>geoip_blocks<\/code><\/p>\n<pre><code>CREATE OR REPLACE VIEW geoip_blocks_int AS select         cast(ip[1] as BIGINT)*256*256*256 + cast(ip[2] as BIGINT)*256*256 + cast(ip[3] as BIGINT)*256 + cast(ip[4] as BIGINT) as ip_start,         (             bitwise_or(cast(ip[1] as BIGINT), bitwise_and(255, cast(power(2, greatest(8 - range, 0)) as BIGINT)-1))             )*256*256*256 +         (             bitwise_or(cast(ip[2] as BIGINT), bitwise_and(255, cast(power(2, greatest(16 - range, 0)) as BIGINT)-1))             )*256*256 +         (             bitwise_or(cast(ip[3] as BIGINT), bitwise_and(255, cast(power(2, greatest(24 - range, 0)) as BIGINT)-1))             )*256+         (             bitwise_or(cast(ip[4] as BIGINT), bitwise_and(255, cast(power(2, greatest(32 - range, 0)) as BIGINT)-1))             ) as ip_end,         network,         geoname_id,         registered_country_geoname_id,         represented_country_geoname_id,         cast(is_anonymous_proxy as BOOLEAN) as is_anonymous_proxy,         cast(is_satellite_provider as BOOLEAN) as is_satellite_provider,         postal_code,         latitude,         longitude,         accuracy_radius from     (         select             network,             geoname_id,             registered_country_geoname_id,             represented_country_geoname_id,             is_anonymous_proxy,             is_satellite_provider,             postal_code,             latitude,             longitude,             accuracy_radius,             split(network_array[1], '.') as ip,             cast(network_array[2] as BIGINT) as range         from             (                 select                     network,                     geoname_id,                     registered_country_geoname_id,                     represented_country_geoname_id,<\/code><\/pre>\n<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-316575","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/316575","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=316575"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/316575\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=316575"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=316575"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=316575"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}