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.util.Properties; 025 026import cascading.flow.FlowProcess; 027import cascading.scheme.SinkCall; 028import cascading.scheme.SourceCall; 029import cascading.tap.Tap; 030import cascading.tuple.Fields; 031import cascading.tuple.TupleEntry; 032import com.fasterxml.jackson.databind.JsonNode; 033import org.neo4j.driver.v1.Session; 034import org.neo4j.driver.v1.StatementResult; 035import org.slf4j.Logger; 036import org.slf4j.LoggerFactory; 037 038/** 039 * 040 */ 041public class Neo4jJSONScheme extends Neo4jScheme 042 { 043 private static final Logger LOG = LoggerFactory.getLogger( Neo4jJSONScheme.class ); 044 045 private final JSONGraphSpec graphSpec; 046 047 public Neo4jJSONScheme( Fields sinkFields, JSONGraphSpec graphSpec ) 048 { 049 super( Fields.UNKNOWN, sinkFields ); 050 this.graphSpec = graphSpec; 051 052 if( !sinkFields.isDeclarator() || sinkFields.size() > 1 ) 053 throw new IllegalArgumentException( "sink fields must be size one, got: " + sinkFields.print() ); 054 } 055 056 @Override 057 public boolean isSource() 058 { 059 return false; 060 } 061 062 @Override 063 public void sourceConfInit( FlowProcess<? extends Properties> flowProcess, Tap<Properties, Void, Session> tap, Properties conf ) 064 { 065 throw new UnsupportedOperationException(); 066 } 067 068 @Override 069 public boolean source( FlowProcess<? extends Properties> flowProcess, SourceCall<Context, Void> sourceCall ) throws IOException 070 { 071 throw new UnsupportedOperationException(); 072 } 073 074 @Override 075 public void sinkConfInit( FlowProcess<? extends Properties> flowProcess, Tap<Properties, Void, Session> tap, Properties conf ) 076 { 077 078 } 079 080 @Override 081 public void sinkPrepare( FlowProcess<? extends Properties> flowProcess, SinkCall<Context, Session> sinkCall ) throws IOException 082 { 083 sinkCall.setContext( new Context<>( new Neo4jJSONStatement( graphSpec ) ) ); 084 } 085 086 @Override 087 public void sink( FlowProcess<? extends Properties> flowProcess, SinkCall<Context, Session> sinkCall ) throws IOException 088 { 089 Session session = sinkCall.getOutput(); 090 Neo4jStatement<JsonNode> statement = sinkCall.getContext().statement; 091 TupleEntry entry = sinkCall.getOutgoingEntry(); 092 093 JsonNode node = (JsonNode) entry.getObject( 0 ); 094 095 session.writeTransaction( tx -> 096 { 097 StatementResult result = statement.runStatement( tx, node ); 098 099 if( LOG.isDebugEnabled() ) 100 LOG.debug( "cypher results: {}", result.summary() ); 101 102 return true; 103 } ); 104 } 105 }