001/* 002 * Copyright (c) 2007-2022 The Cascading Authors. All Rights Reserved. 003 * 004 * Project and contact information: https://cascading.wensel.net/ 005 * 006 * This file is part of the Cascading project. 007 * 008 * Licensed under the Apache License, Version 2.0 (the "License"); 009 * you may not use this file except in compliance with the License. 010 * You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, software 015 * distributed under the License is distributed on an "AS IS" BASIS, 016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 017 * See the License for the specific language governing permissions and 018 * limitations under the License. 019 */ 020 021package cascading.local.tap.neo4j; 022 023import java.io.IOException; 024import java.net.URI; 025import java.util.Properties; 026 027import cascading.flow.FlowProcess; 028import cascading.tap.SinkMode; 029import cascading.tap.Tap; 030import cascading.tuple.TupleEntryCollector; 031import cascading.tuple.TupleEntryIterator; 032import cascading.tuple.TupleEntrySchemeCollector; 033import org.neo4j.driver.v1.AuthToken; 034import org.neo4j.driver.v1.AuthTokens; 035import org.neo4j.driver.v1.Driver; 036import org.neo4j.driver.v1.GraphDatabase; 037import org.neo4j.driver.v1.Session; 038 039/** 040 * 041 */ 042public class Neo4jTap extends Tap<Properties, Void, Session> 043 { 044 public static final String NEO_4_J_USERNAME = "neo4j.username"; 045 public static final String NEO_4_J_PASSWORD = "neo4j.password"; 046 047 private final Properties defaultProperties; 048 private final URI identifier; 049 050 public Neo4jTap( Properties defaultProperties, Neo4jScheme scheme, URI identifier ) 051 { 052 super( scheme, SinkMode.UPDATE ); 053 054 this.defaultProperties = new Properties( defaultProperties ); 055 this.identifier = identifier; 056 } 057 058 protected Driver getDriver( FlowProcess<? extends Properties> flowProcess ) 059 { 060 AuthToken authTokens = AuthTokens.none(); 061 062 String user = flowProcess.getStringProperty( NEO_4_J_USERNAME, defaultProperties.getProperty( NEO_4_J_USERNAME, System.getenv( "NEO4J_USER" ) ) ); 063 String password = flowProcess.getStringProperty( NEO_4_J_PASSWORD, defaultProperties.getProperty( NEO_4_J_PASSWORD, System.getenv( "NEO4J_PASSWORD" ) ) ); 064 065 if( user != null ) 066 authTokens = AuthTokens.basic( user, password ); 067 068 return GraphDatabase.driver( getIdentifier(), authTokens ); 069 } 070 071 @Override 072 public String getIdentifier() 073 { 074 return identifier.toString(); 075 } 076 077 @Override 078 public TupleEntryIterator openForRead( FlowProcess<? extends Properties> flowProcess, Void aVoid ) throws IOException 079 { 080 return null; 081 } 082 083 @Override 084 public TupleEntryCollector openForWrite( FlowProcess<? extends Properties> flowProcess, Session session ) throws IOException 085 { 086 if( session == null ) 087 session = getDriver( flowProcess ).session(); 088 089 return new TupleEntrySchemeCollector<>( flowProcess, this, getScheme(), session, getIdentifier() ); 090 } 091 092 @Override 093 public boolean createResource( Properties conf ) throws IOException 094 { 095 return true; 096 } 097 098 @Override 099 public boolean deleteResource( Properties conf ) throws IOException 100 { 101 return true; 102 } 103 104 @Override 105 public boolean resourceExists( Properties conf ) throws IOException 106 { 107 return true; 108 } 109 110 @Override 111 public long getModifiedTime( Properties conf ) throws IOException 112 { 113 return 0; 114 } 115 }