-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* update readme and workflow (#38) * update readme (#34) * fix workflow * update version to 3.0.0 (#33) * add edge reader by ngql * update option for ngql Co-authored-by: Anqi <anqi.wang@vesoft.com>
- Loading branch information
Showing
13 changed files
with
282 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
152 changes: 152 additions & 0 deletions
152
...tor/src/main/scala/com/vesoft/nebula/connector/reader/NebulaNgqlEdgePartitionReader.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
package com.vesoft.nebula.connector.reader | ||
|
||
import com.vesoft.nebula.Value | ||
import com.vesoft.nebula.client.graph.data.{Relationship, ResultSet, ValueWrapper} | ||
import com.vesoft.nebula.connector.NebulaUtils.NebulaValueGetter | ||
import com.vesoft.nebula.connector.nebula.GraphProvider | ||
import com.vesoft.nebula.connector.{NebulaOptions, NebulaUtils} | ||
import org.apache.spark.sql.catalyst.InternalRow | ||
import org.apache.spark.sql.catalyst.expressions.SpecificInternalRow | ||
import org.apache.spark.sql.sources.v2.reader.InputPartitionReader | ||
import org.apache.spark.sql.types.StructType | ||
import org.slf4j.{Logger, LoggerFactory} | ||
|
||
import scala.collection.JavaConversions.asScalaBuffer | ||
import scala.collection.mutable | ||
import scala.collection.mutable.ListBuffer | ||
|
||
/** | ||
* create reader by ngql | ||
*/ | ||
class NebulaNgqlEdgePartitionReader extends InputPartitionReader[InternalRow] { | ||
|
||
private val LOG: Logger = LoggerFactory.getLogger(this.getClass) | ||
|
||
private var nebulaOptions: NebulaOptions = _ | ||
private var graphProvider: GraphProvider = _ | ||
private var schema: StructType = _ | ||
private var resultSet: ResultSet = _ | ||
private var edgeIterator: Iterator[ListBuffer[ValueWrapper]] = _ | ||
|
||
def this(nebulaOptions: NebulaOptions, schema: StructType) { | ||
this() | ||
this.schema = schema | ||
this.nebulaOptions = nebulaOptions | ||
this.graphProvider = new GraphProvider( | ||
nebulaOptions.getGraphAddress, | ||
nebulaOptions.timeout, | ||
nebulaOptions.enableGraphSSL, | ||
nebulaOptions.sslSignType, | ||
nebulaOptions.caSignParam, | ||
nebulaOptions.selfSignParam | ||
) | ||
// add exception when session build failed | ||
graphProvider.switchSpace(nebulaOptions.user, nebulaOptions.passwd, nebulaOptions.spaceName) | ||
resultSet = graphProvider.submit(nebulaOptions.ngql) | ||
edgeIterator = query() | ||
} | ||
|
||
def query(): Iterator[ListBuffer[ValueWrapper]] = { | ||
val edges: ListBuffer[ListBuffer[ValueWrapper]] = new ListBuffer[ListBuffer[ValueWrapper]] | ||
val properties = nebulaOptions.getReturnCols | ||
for (i <- 0 until resultSet.rowsSize()) { | ||
val rowValues = resultSet.rowValues(i).values() | ||
for (j <- 0 until rowValues.size()) { | ||
val value = rowValues.get(j) | ||
val valueType = value.getValue.getSetField | ||
if (valueType == Value.EVAL) { | ||
val relationship = value.asRelationship() | ||
if (checkLabel(relationship)) { | ||
edges.append(convertToEdge(relationship, properties)) | ||
} | ||
} else if (valueType == Value.LVAL) { | ||
val list: mutable.Buffer[ValueWrapper] = value.asList() | ||
edges.appendAll( | ||
list.toStream.filter(e => checkLabel(e.asRelationship())) | ||
.map(e => convertToEdge(e.asRelationship(), properties)) | ||
) | ||
} else { | ||
LOG.error(s"Exception convert edge type ${valueType} ") | ||
throw new RuntimeException(" convert value type failed"); | ||
} | ||
} | ||
} | ||
edges.iterator | ||
} | ||
|
||
def checkLabel(relationship: Relationship): Boolean = { | ||
this.nebulaOptions.label.equals(relationship.edgeName()) | ||
} | ||
|
||
def convertToEdge(relationship: Relationship, properties: List[String]): ListBuffer[ValueWrapper] = { | ||
val edge: ListBuffer[ValueWrapper] = new ListBuffer[ValueWrapper] | ||
edge.append(relationship.srcId()) | ||
edge.append(relationship.dstId()) | ||
edge.append(new ValueWrapper(new Value(3, relationship.ranking()), "utf-8")) | ||
if (properties == null || properties.isEmpty) | ||
return edge | ||
else { | ||
for (i <- properties.indices) { | ||
edge.append(relationship.properties().get(properties(i))) | ||
} | ||
} | ||
edge | ||
} | ||
|
||
|
||
override def next(): Boolean = { | ||
edgeIterator.hasNext | ||
} | ||
|
||
override def get(): InternalRow = { | ||
val getters: Array[NebulaValueGetter] = NebulaUtils.makeGetters(schema) | ||
val mutableRow = new SpecificInternalRow(schema.fields.map(x => x.dataType)) | ||
|
||
val edge = edgeIterator.next(); | ||
for (i <- getters.indices) { | ||
val value: ValueWrapper = edge(i) | ||
var resolved = false | ||
if (value.isNull) { | ||
mutableRow.setNullAt(i) | ||
resolved = true | ||
} | ||
if (value.isString) { | ||
getters(i).apply(value.asString(), mutableRow, i) | ||
resolved = true | ||
} | ||
if (value.isDate) { | ||
getters(i).apply(value.asDate(), mutableRow, i) | ||
resolved = true | ||
} | ||
if (value.isTime) { | ||
getters(i).apply(value.asTime(), mutableRow, i) | ||
resolved = true | ||
} | ||
if (value.isDateTime) { | ||
getters(i).apply(value.asDateTime(), mutableRow, i) | ||
resolved = true | ||
} | ||
if (value.isLong) { | ||
getters(i).apply(value.asLong(), mutableRow, i) | ||
} | ||
if (value.isBoolean) { | ||
getters(i).apply(value.asBoolean(), mutableRow, i) | ||
} | ||
if (value.isDouble) { | ||
getters(i).apply(value.asDouble(), mutableRow, i) | ||
} | ||
if (value.isGeography) { | ||
getters(i).apply(value.asGeography(), mutableRow, i) | ||
} | ||
if (value.isDuration) { | ||
getters(i).apply(value.asDuration(), mutableRow, i) | ||
} | ||
} | ||
mutableRow | ||
|
||
} | ||
|
||
override def close(): Unit = { | ||
graphProvider.close(); | ||
} | ||
} |
Oops, something went wrong.