-
Notifications
You must be signed in to change notification settings - Fork 35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
support custom partitioner for nebula when generate sst files #49
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why don't use
repartition
directly?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dataframe doesn't have customed
repartition
function, it's RDD's function.