DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. Databases
  4. Crunchbase on Neo4j

Crunchbase on Neo4j

Max De Marzi user avatar by
Max De Marzi
·
Nov. 15, 12 · Interview
Like (0)
Save
Tweet
Share
7.29K Views

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.




Database Neo4j

Published at DZone with permission of Max De Marzi, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Load Balancing Pattern
  • Top 5 PHP REST API Frameworks
  • The Importance of Delegation in Management Teams
  • Beginners’ Guide to Run a Linux Server Securely

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: