Crunchbase on Neo4j
Join the DZone community and get the full member experience.
Join For Free
neotechnology
was featured on
techcrunch
after
raising a series b round
, and it has an entry on
crunchbase
. if you look at crunchbase closely you’ll notice it’s a graph. who invested in what, who co-invested, what are the common investment themes between investors, how are companies connected by board members, etc. these are questions we can ask of the graph and are well suited for graph databases.
so let’s get to it. this project and dataset are available on github , but if you want to play it on your own you’ll have to register on http://developer.crunchbase.com and get an api key. once you have one you can take a look at the json returned by heading over to the io docs . here is part of the “neo-technology” company record.
{ "name": "neo technology", "permalink": "neo-technology", "crunchbase_url": "http://www.crunchbase.com/company/neo-technology", "homepage_url": "http://www.neotechnology.com", "blog_url": "", "blog_feed_url": "", "twitter_username": "", "category_code": "software", "number_of_employees": null, "founded_year": 2007, "founded_month": null, "founded_day": null, "deadpooled_year": null, "deadpooled_month": null, "deadpooled_day": null, "deadpooled_url": null, "tag_list": "graphs, open-soure-graphs, graph-systems, commercial-graphs, graph-database", "alias_list": "", "email_address": "", "phone_number": "", "description": "graph database", ...
i’m not going to map all of crunchbase, but i’ll grab a few things of interest. i’ll import companies, people, financial organizations, and tags as nodes and employee, investor, tagged, and competitor connections as relationships into neo4j .
i am using the wonderful crunchbase gem by tyler cunnion . i start by getting all the entities i am interested in (for example here is people):
all_people = crunchbase::person.all all_people.each do |ap| begin next unless ap.permalink file = "crunchbase/people/#{ap.permalink}" if file.exist?(file) person = marshal::load(file.open(file, 'r')) else person = ap.entity file.open(file, 'wb') { |fp| fp.write(marshal::dump(person)) } end people << {:name => "#{person.first_name || ""} #{person.last_name || ""}", :permalink => person.permalink, :crunchbase_url => person.crunchbase_url || "", :homepage_url => person.homepage_url || "", :overview => person.overview || "" } rescue exception => e puts e.message end end
i am saving the json returned from the crunchbase api to a file in case something goes wrong and i need to restart my import. neo4j doesn’t like null values for properties, so just in case i use an empty string if no value is found.
i will be using a fulltext lucene index to allow people to find their favorite company, person, tag, or financial organization by permalink, so i create that first.
neo = neography::rest.new neo.create_node_index("node_index", "fulltext", "lucene")
then i will be creating the nodes for these entities (while saving the node id returned in a hash):
people_nodes = {} people.each_slice(100) do |slice| commands = [] slice.each_with_index do |person, index| commands << [:create_unique_node, "node_index", "permalink", person[:permalink], person] end batch_results = neo.batch *commands batch_results.each do |result| people_nodes[result["body"]["data"]["permalink"]] = result["body"]["self"].split('/').last end end
i am using the neo4j rest batch method sending 100 commands at a time.
i then create the relationships to each other. for example employees of companies:
employees.each_slice(100) do |slice| commands = [] slice.each do |employee| commands << [:create_relationship, employee[:type], people_nodes[employee[:from]], company_nodes[employee[:to]], employee[:properties]] end batch_results = neo.batch *commands end
for the front end, i need two basic methods. one will return all the nodes and relationships types connected to one node:
cypher = "start me=node(#{params[:id]}) match me -[r?]- related return me, r, related" connections = neo.execute_query(cypher)["data"]
a second query will handle full text search and format the output to a json hash that jquery autocomplete can work with:
get '/search' do content_type :json neo = neography::rest.new cypher = "start me=node:node_index({query}) return id(me), me.name order by me.name limit 15" query = "permalink:*#{params[:term]}* or name:*#{params[:term]}*" neo.execute_query(cypher, {:query => query })["data"]. map{|x| { label: x[1], value: x[0] } }.to_json end
notice how i am passing in the whole lucene query as a parameter? this is the right way to do it. i indexed people, companies, and financial organizations by permalink, but indexed tags by name. i use an “or” clause to grab them both.
you can skip the graph building step if you just want to use the data as it existed the day of this blog post since it is available on the github repository .
you can clone the repository, create a heroku app, and get this up and running in no time.
git clone https://github.com/maxdemarzi/neo_crunch.git cd neo_crunch heroku apps:create heroku addons:add neo4j:try git push heroku master
then go to your heroku apps
https://dashboard.heroku.com/apps/
find your app, and go to your neo4j add-on.
in the back-up and restore section click “choose file”, find the crunchbase.zip file and click submit. it will take a little while to upload the data, but once you will be able to navigate to your application and see this:
click the image above to see a live version of it.
Published at DZone with permission of Max De Marzi, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
Security Challenges for Microservice Applications in Multi-Cloud Environments
-
Auto-Scaling Kinesis Data Streams Applications on Kubernetes
-
Google Becomes A Java Developer's Best Friend: Instantiations Developer Tools Relaunched For Free
-
Top 10 Pillars of Zero Trust Networks
Comments