Skip to content

Instantly share code, notes, and snippets.

@yujiod
Created March 9, 2014 01:18
Show Gist options
  • Save yujiod/9441591 to your computer and use it in GitHub Desktop.
Save yujiod/9441591 to your computer and use it in GitHub Desktop.
Clone Keen IO data to Elasticsearch
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
require "net/https"
require "uri"
require "json"
require "time"
KEENIO_PROJECT_ID = "XXXXXXXX"
KEENIO_READ_KEY = "YYYYYYYY"
EVENT_COLLECTION = "PageView"
TIMEFRAME = "today"
ELASTICSEARCH_URL = "http://localhost:9200";
TAG_KEY = "@log_name"
TAG_VALUE = "keen.io.pageview"
TYPE = "keen_io"
url = "https://api.keen.io/3.0/projects/#{KEENIO_PROJECT_ID}/queries/extraction?api_key=#{KEENIO_READ_KEY}&event_collection=#{EVENT_COLLECTION}&timeframe=#{TIMEFRAME}"
uri = URI.parse(url)
client = Net::HTTP.new(uri.host, uri.port)
client.use_ssl = true
response = nil
client.start do |http|
response = http.get(uri.request_uri)
end
results = JSON.parse(response.body)
results[EVENT_COLLECTION] = results["result"]
results.delete("result")
results[EVENT_COLLECTION].each do |result|
result[TAG_KEY] = TAG_VALUE
timestamp = Time.parse(result["keen"]["timestamp"])
result["@timestamp"] = timestamp.strftime("%Y-%m-%dT%H:%M:%S%:z")
if !result["geoip"]["lon"].nil? and !result["geoip"]["lat"].nil? then
result["geoip"]["coords"] = [result["geoip"]["lon"].to_f, result["geoip"]["lat"].to_f]
result["geoip"].delete("lon")
result["geoip"].delete("lat")
end
index = "logstash-#{timestamp.strftime("%Y.%m.%d")}"
url = "#{ELASTICSEARCH_URL}/#{index}/#{TYPE}/"
uri = URI.parse(url)
client = Net::HTTP.new(uri.host, uri.port)
client.start do |http|
http.post(uri.request_uri, JSON.dump(result))
p "Sent event on #{timestamp}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment