You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@seatunnel.apache.org by "Hisoka-X (via GitHub)" <gi...@apache.org> on 2023/06/12 08:37:16 UTC

[GitHub] [seatunnel] Hisoka-X commented on a diff in pull request #4772: [Bug] [seatunnel-connectors] [seatunnel-connector-spark-clickhouse] Data cannot be imported to all nodes by configuring split_mode and sharding_key

Hisoka-X commented on code in PR #4772:
URL: https://github.com/apache/seatunnel/pull/4772#discussion_r1226294038


##########
seatunnel-connectors-v2/connector-clickhouse/src/test/java/org/apache/seatunnel/connectors/seatunnel/clickhouse/ClickhouseFactoryTest.java:
##########
@@ -24,12 +24,43 @@
 import org.junit.jupiter.api.Assertions;
 import org.junit.jupiter.api.Test;
 
+import net.jpountz.xxhash.XXHash64;
+import net.jpountz.xxhash.XXHashFactory;
+
+import java.nio.ByteBuffer;
+import java.nio.charset.StandardCharsets;
+import java.util.Arrays;
+
 public class ClickhouseFactoryTest {
+    private static final XXHash64 HASH_INSTANCE = XXHashFactory.fastestInstance().hash64();
 
     @Test
     public void testOptionRule() {
         Assertions.assertNotNull((new ClickhouseSourceFactory()).optionRule());
         Assertions.assertNotNull((new ClickhouseSinkFactory()).optionRule());
         Assertions.assertNotNull((new ClickhouseFileSinkFactory()).optionRule());
     }
+
+    public int getShard(Object shardValue) {
+        int shardWeightCount = 6;
+        int offset =
+                (int)
+                        ((HASH_INSTANCE.hash(
+                                                ByteBuffer.wrap(
+                                                        shardValue
+                                                                .toString()
+                                                                .getBytes(StandardCharsets.UTF_8)),
+                                                0)
+                                        & Long.MAX_VALUE)
+                                % shardWeightCount);
+        return offset;
+    }
+
+    @Test
+    public void testShared() {
+        String a = "a,b,c,d,e,f";
+        for (Object o : Arrays.stream(a.split(",")).toArray()) {
+            System.out.println(getShard(o));

Review Comment:
   Please use `Assertions` to make sure to result are right. Also please provide the test case to make sure to all shard can be selected. Tips: make we can use 10000 keys with random string to invoke `getShard` then get each shard index count which return by `getShard`. I believe when the number of keys more large, the shard index will be more balanced.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@seatunnel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org