Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support custom partitioner for nebula when generate sst files (#49)
* support custom partitioner for nebula when generate sst files * support custom partitioner for nebula when generate sst files * exclude jackson-core * add test
- Loading branch information
Showing
24 changed files
with
281 additions
and
88 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
26 changes: 26 additions & 0 deletions
26
exchange-common/src/main/scala/com/vesoft/exchange/common/utils/NebulaPartitioner.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,26 @@ | ||
/* Copyright (c) 2021 vesoft inc. All rights reserved. | ||
* | ||
* This source code is licensed under Apache 2.0 License. | ||
*/ | ||
|
||
package com.vesoft.exchange.common.utils | ||
|
||
import java.nio.{ByteBuffer, ByteOrder} | ||
import org.apache.spark.Partitioner | ||
|
||
class NebulaPartitioner(partitions: Int) extends Partitioner { | ||
require(partitions >= 0, s"Number of partitions ($partitions) cannot be negative.") | ||
|
||
override def numPartitions: Int = partitions | ||
|
||
override def getPartition(key: Any): Int = { | ||
var part = ByteBuffer | ||
.wrap(key.asInstanceOf[Array[Byte]], 0, 4) | ||
.order(ByteOrder.nativeOrder) | ||
.getInt >> 8 | ||
if (part <= 0) { | ||
part = part + partitions | ||
} | ||
part - 1 | ||
} | ||
} |
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
50 changes: 50 additions & 0 deletions
50
exchange-common/src/test/scala/com/vesoft/exchange/common/writer/FileBaseWriterSuite.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,50 @@ | ||
/* Copyright (c) 2021 vesoft inc. All rights reserved. | ||
* | ||
* This source code is licensed under Apache 2.0 License. | ||
*/ | ||
|
||
package com.vesoft.exchange.common.writer | ||
|
||
import com.vesoft.exchange.common.config.{FileBaseSinkConfigEntry, SinkCategory} | ||
import org.apache.spark.sql.{Dataset, Encoders, Row, SparkSession} | ||
import org.junit.Test | ||
|
||
class FileBaseWriterSuite { | ||
|
||
@Test | ||
def writeSstFilesSuite(): Unit = { | ||
val spark = SparkSession.builder().master("local").getOrCreate() | ||
import spark.implicits._ | ||
// generate byte[] key using encoder's getVertexKey, space:"test", tag: "person" | ||
val key1 = "01a40200310000000000000000000000000000000000000002000000" // id: "1" | ||
val key2 = "01170000320000000000000000000000000000000000000002000000" // id: "2" | ||
val key3 = "01fe0000330000000000000000000000000000000000000002000000" // id: "3" | ||
val key4 = "01a90300340000000000000000000000000000000000000002000000" // id: "4" | ||
val key5 = "01220200350000000000000000000000000000000000000002000000" // id: "5" | ||
val value = "abc" | ||
// construct test dataset | ||
val data: Dataset[(Array[Byte], Array[Byte])] = spark.sparkContext | ||
.parallelize( | ||
List(key1.getBytes(), key2.getBytes(), key3.getBytes(), key4.getBytes(), key5.getBytes())) | ||
.map(line => (line, value.getBytes())) | ||
.toDF("key", "value") | ||
.map { row => | ||
(row.getAs[Array[Byte]](0), row.getAs[Array[Byte]](1)) | ||
}(Encoders.tuple(Encoders.BINARY, Encoders.BINARY)) | ||
|
||
val generateSstFile = new GenerateSstFile | ||
|
||
val fileBaseConfig = | ||
FileBaseSinkConfigEntry(SinkCategory.SST, "/tmp", "/tmp/remote", None) | ||
val batchFailure = spark.sparkContext.longAccumulator(s"batchFailure.test}") | ||
|
||
data | ||
.toDF("key", "value") | ||
.sortWithinPartitions("key") | ||
.foreachPartition { iterator: Iterator[Row] => | ||
generateSstFile.writeSstFiles(iterator, fileBaseConfig, 10, null, batchFailure) | ||
} | ||
assert(batchFailure.value == 0) | ||
} | ||
|
||
} |
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
Oops, something went wrong.