Skip to content

Instantly share code, notes, and snippets.

@tfuji
Created July 1, 2019 06:43
Show Gist options
  • Save tfuji/cbd18ffe77550803392edde8723dc765 to your computer and use it in GitHub Desktop.
Save tfuji/cbd18ffe77550803392edde8723dc765 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
#
# ruby load_sample.rb biosample/annotated_json dev
require 'elasticsearch'
require 'json'
require 'pp'
#client = Elasticsearch::Client.new({log: true, hosts: { host: 'localhost', user: 'user', password: 'password' }})
client = Elasticsearch::Client.new({log: true, hosts: { host: 'localhost'}})
dir = ARGV[0]
index = ARGV[1] || 'genome_public_sample'
type = 'dm5'
#Dir.glob(dir + "/**/*.json").first(1).each do |file|
Dir.glob(dir + "/**/*.json").each do |file|
puts file
json_data = open(file) do |io|
JSON.load(io)
end
#pp json_data.keys
#pp json_data['@context']
ary =[]
json_data.each do |g|
#next if g['@id'].match(/^http:\/\/purl.jp\/bio\/11\/meo\/MEO_/)
#next if g['@type'].match(/(owl:Ontology|owl:AnnotationProperty)/)
#puts g.to_json
#meos = get_hierarchical_meo( g['@id'])
#sleep(5)
#id = g['@id'].gsub('http://purl.jp/bio/11/meo/','')
puts id = g['identifier']
ary.push("{ \"create\": { \"_index\": \"#{index}\", \"_type\": \"#{type}\", \"_id\": \"#{id}\" }}")
ary.push(g.to_json)
begin
#client.create index: index, type: type, id: id, body: g.to_json
#pp g.to_json
rescue Exception => ex
puts ex.backtrace.first + ": #{ex.message} (#{ex.class})"
end
end
begin
client.bulk body: ary
# pp g.to_json
rescue Exception => ex
puts ex.backtrace.first + ": #{ex.message} (#{ex.class})"
end
end
#client.cluster.health
#puts client.cat.indices
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment