Skip to content

Instantly share code, notes, and snippets.

@johnymontana
Created March 10, 2016 00:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnymontana/02ae47fc0a29719db045 to your computer and use it in GitHub Desktop.
Save johnymontana/02ae47fc0a29719db045 to your computer and use it in GitHub Desktop.
Importing FEC data into Neo4j. Download data files here: http://www.fec.gov/finance/disclosure/ftpdet.shtml#a2015_2016
// Create schema constraints
CREATE CONSTRAINT ON (c:FECCommittee) ASSERT c.committee_id IS UNIQUE;
CREATE CONSTRAINT ON (t:Treasurer) ASSERT t.name IS UNIQUE;
CREATE CONSTRAINT ON (c:Contributor) ASSERT c.name IS UNIQUE;
CREATE CONSTRAINT ON (o:Occupation) ASSERT o.name IS UNIQUE;
CREATE CONSTRAINT ON (e:Employer) ASSERT e.name IS UNIQUE;
CREATE CONSTRAINT ON (c:City) ASSERT c.name IS UNIQUE;
// FEC Committees
USING PERIODIC COMMIT
LOAD CSV FROM "https://dl.dropboxusercontent.com/u/67572426/fec/cm.txt" AS row FIELDTERMINATOR "|"
WITH row
//WITH replace(row[0], "[", "") AS committee
MERGE (c:FECCommittee {committee_id: replace(row[0], "[", "")})
SET c.name = row[1],
c.designation = row[8],
c.committee_type = row[9],
c.committee_party = row[10],
c.category = row[12]
WITH row WHERE row[2] IS NOT NULL
MERGE (t:Treasurer {name:row[2]})
CREATE UNIQUE (t)-[:TREASURER_FOR]->(c);
// Link candidates to committees
USING PERIODIC COMMIT
LOAD CSV FROM "https://dl.dropboxusercontent.com/u/67572426/fec/ccl.txt" AS row FIELDTERMINATOR "|" WITH row
MATCH (c:FECCommittee) WHERE c.committee_id = row[3]
MATCH (l:Legislator) WHERE l.fecIDs CONTAINS toString(row[0])
CREATE UNIQUE (c)-[:FUNDS]->(l);
// Individual contributions to committees
USING PERIODIC COMMIT
LOAD CSV FROM "https://dl.dropboxusercontent.com/u/67572426/fec/itcont.txt"
AS row FIELDTERMINATOR "|"
WITH row WHERE row[7] IS NOT NULL AND row[12] IS NOT NULL AND row[11] IS NOT NULL AND row[8] IS NOT NULL AND row[9] IS NOT NULL
MATCH (c:FECCommittee) WHERE c.committee_id = replace(row[0], "[", "")
CREATE (con:Contribution {sub_id: replace(row[20], "]", "")})
SET con.amount = toFloat(row[14]),
con.date = row[13]
CREATE UNIQUE (con)-[:MADE_TO]->(c)
MERGE (t:Contributor {name: row[7]})
MERGE (occupation:Occupation {name: row[12]})
MERGE (employer:Employer {name: row[11]})
MERGE (city:City {name: row[8]})
MERGE (state:State {code: row[9]})
CREATE UNIQUE (t)-[:MADE_CONTRIBUTION]->(con)
CREATE UNIQUE (t)-[:HAS_OCCUPATION]->(occupation)
CREATE UNIQUE (t)-[:WORKS_FOR]->(employer)
CREATE UNIQUE (t)-[:LIVES_IN]->(city)
CREATE UNIQUE (city)-[:LOCATED_IN]->(state)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment