|
18 | 18 | package org.apache.hugegraph.loader.direct.loader; |
19 | 19 |
|
20 | 20 | import java.io.IOException; |
| 21 | +import java.nio.ByteBuffer; |
| 22 | +import java.security.SecureRandom; |
21 | 23 | import java.util.Iterator; |
22 | 24 | import java.util.LinkedList; |
23 | 25 | import java.util.List; |
24 | 26 |
|
| 27 | +import java.util.concurrent.atomic.AtomicInteger; |
25 | 28 | import org.apache.hadoop.conf.Configuration; |
26 | 29 | import org.apache.hadoop.fs.FileSystem; |
27 | 30 | import org.apache.hadoop.fs.FsShell; |
@@ -59,8 +62,69 @@ public class HBaseDirectLoader extends DirectLoader<ImmutableBytesWritable, KeyV |
59 | 62 | private SinkToHBase sinkToHBase; |
60 | 63 | private LoadDistributeMetrics loadDistributeMetrics; |
61 | 64 |
|
| 65 | + private static final int RANDOM_VALUE1; |
| 66 | + private static final short RANDOM_VALUE2; |
| 67 | + private static final AtomicInteger NEXT_COUNTER; |
| 68 | + |
62 | 69 | public static final Logger LOG = Log.logger(HBaseDirectLoader.class); |
63 | 70 |
|
| 71 | + static { |
| 72 | + try { |
| 73 | + SecureRandom secureRandom = new SecureRandom(); |
| 74 | + RANDOM_VALUE1 = secureRandom.nextInt(0x01000000); |
| 75 | + RANDOM_VALUE2 = (short) secureRandom.nextInt(0x00008000); |
| 76 | + NEXT_COUNTER = new AtomicInteger(new SecureRandom().nextInt()); |
| 77 | + } catch (Exception e) { |
| 78 | + throw new RuntimeException(e); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + private static byte int3(final int x) { |
| 83 | + return (byte) (x >> 24); |
| 84 | + } |
| 85 | + |
| 86 | + private static byte int2(final int x) { |
| 87 | + return (byte) (x >> 16); |
| 88 | + } |
| 89 | + |
| 90 | + private static byte int1(final int x) { |
| 91 | + return (byte) (x >> 8); |
| 92 | + } |
| 93 | + |
| 94 | + private static byte int0(final int x) { |
| 95 | + return (byte) (x); |
| 96 | + } |
| 97 | + |
| 98 | + private static byte short1(final short x) { |
| 99 | + return (byte) (x >> 8); |
| 100 | + } |
| 101 | + |
| 102 | + private static byte short0(final short x) { |
| 103 | + return (byte) (x); |
| 104 | + } |
| 105 | + |
| 106 | + public static String fileID() { |
| 107 | + long timeStamp = System.currentTimeMillis() / 1000; |
| 108 | + ByteBuffer byteBuffer = ByteBuffer.allocate(12); |
| 109 | + |
| 110 | + byteBuffer.put(int3((int) timeStamp)); |
| 111 | + byteBuffer.put(int2((int) timeStamp)); |
| 112 | + byteBuffer.put(int1((int) timeStamp)); |
| 113 | + byteBuffer.put(int0((int) timeStamp)); |
| 114 | + |
| 115 | + byteBuffer.put(int2(RANDOM_VALUE1)); |
| 116 | + byteBuffer.put(int1(RANDOM_VALUE1)); |
| 117 | + byteBuffer.put(int0(RANDOM_VALUE1)); |
| 118 | + byteBuffer.put(short1(RANDOM_VALUE2)); |
| 119 | + byteBuffer.put(short0(RANDOM_VALUE2)); |
| 120 | + |
| 121 | + byteBuffer.put(int2(NEXT_COUNTER.incrementAndGet())); |
| 122 | + byteBuffer.put(int1(NEXT_COUNTER.incrementAndGet())); |
| 123 | + byteBuffer.put(int0(NEXT_COUNTER.incrementAndGet())); |
| 124 | + |
| 125 | + return Bytes.toHex(byteBuffer.array()); |
| 126 | + } |
| 127 | + |
64 | 128 | public HBaseDirectLoader(LoadOptions loadOptions, |
65 | 129 | InputStruct struct, |
66 | 130 | LoadDistributeMetrics loadDistributeMetrics) { |
@@ -144,8 +208,8 @@ String generateFiles(JavaPairRDD<ImmutableBytesWritable, KeyValue> buildAndSerRd |
144 | 208 |
|
145 | 209 | public String getHFilePath(Configuration conf) throws IOException { |
146 | 210 | FileSystem fs = FileSystem.get(conf); |
147 | | - long timeStr = System.currentTimeMillis(); |
148 | | - String pathStr = fs.getWorkingDirectory().toString() + "/hfile-gen" + "/" + timeStr + "/"; |
| 211 | + String fileID = fileID(); |
| 212 | + String pathStr = fs.getWorkingDirectory().toString() + "/hfile-gen" + "/" + fileID + "/"; |
149 | 213 | Path hfileGenPath = new Path(pathStr); |
150 | 214 | if (fs.exists(hfileGenPath)) { |
151 | 215 | LOG.info("\n Delete the path where the hfile is generated,path {} ", pathStr); |
|
0 commit comments