-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feature] Sink supports LZ4 compression with json format (#354)
Signed-off-by: PengFei Li <[email protected]>
- Loading branch information
Showing
19 changed files
with
676 additions
and
19 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
32 changes: 32 additions & 0 deletions
32
...cks-stream-load-sdk/src/main/java/com/starrocks/data/load/stream/annotation/Evolving.java
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,32 @@ | ||
/* | ||
* Copyright 2021-present StarRocks, Inc. All rights reserved. | ||
* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.starrocks.data.load.stream.annotation; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* APIs that are not stable yet. | ||
*/ | ||
@Documented | ||
@Target({ElementType.TYPE, ElementType.FIELD, ElementType.METHOD}) | ||
public @interface Evolving {} |
59 changes: 59 additions & 0 deletions
59
...ream-load-sdk/src/main/java/com/starrocks/data/load/stream/compress/CompressionCodec.java
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,59 @@ | ||
/* | ||
* Copyright 2021-present StarRocks, Inc. All rights reserved. | ||
* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.starrocks.data.load.stream.compress; | ||
|
||
import com.starrocks.data.load.stream.StreamLoadDataFormat; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
/** Compress data with the specified compression algorithm. */ | ||
public interface CompressionCodec { | ||
|
||
/** | ||
* Create an output stream to compress the raw output stream. | ||
* | ||
* @param rawOutputStream the output stream to compress | ||
* @param contentSize the content size of the raw stream. -1 if the size is unknown | ||
* @return an output stream with compressed data | ||
*/ | ||
OutputStream createCompressionStream(final OutputStream rawOutputStream, long contentSize) throws IOException; | ||
|
||
static Optional<CompressionCodec> createCompressionCodec(StreamLoadDataFormat dataFormat, | ||
Optional<String> compressionType, | ||
Map<String, Object> properties) { | ||
if (!compressionType.isPresent()) { | ||
return Optional.empty(); | ||
} | ||
|
||
if (LZ4FrameCompressionCodec.NAME.equalsIgnoreCase(compressionType.get())) { | ||
if (dataFormat instanceof StreamLoadDataFormat.JSONFormat) { | ||
return Optional.of(LZ4FrameCompressionCodec.create(properties)); | ||
} | ||
} | ||
|
||
throw new UnsupportedOperationException( | ||
String.format("Not support to compress format %s with compression type %s", | ||
dataFormat.name(), compressionType.get())); | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
...load-sdk/src/main/java/com/starrocks/data/load/stream/compress/CompressionHttpEntity.java
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,81 @@ | ||
/* | ||
* Copyright 2021-present StarRocks, Inc. All rights reserved. | ||
* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.starrocks.data.load.stream.compress; | ||
|
||
import com.starrocks.data.load.stream.v2.ChunkHttpEntity; | ||
import org.apache.http.entity.HttpEntityWrapper; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
|
||
/** Wrapping entity that compresses content when writing. */ | ||
public class CompressionHttpEntity extends HttpEntityWrapper { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(CompressionHttpEntity.class); | ||
|
||
private final CompressionCodec compressionCodec; | ||
|
||
public CompressionHttpEntity(ChunkHttpEntity entity, CompressionCodec compressionCodec) { | ||
super(entity); | ||
this.compressionCodec = compressionCodec; | ||
entity.setLogAfterWrite(false); | ||
} | ||
|
||
@Override | ||
public long getContentLength() { | ||
return -1; | ||
} | ||
|
||
@Override | ||
public boolean isChunked() { | ||
// force content chunking | ||
return true; | ||
} | ||
|
||
@Override | ||
public InputStream getContent() throws IOException { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public void writeTo(final OutputStream outStream) throws IOException { | ||
long startTime = System.nanoTime(); | ||
ChunkHttpEntity entity = (ChunkHttpEntity) wrappedEntity; | ||
final CountingOutputStream countingOutputStream = new CountingOutputStream(outStream); | ||
final OutputStream compressOutputStream = | ||
compressionCodec.createCompressionStream(countingOutputStream, entity.getContentLength()); | ||
entity.writeTo(compressOutputStream); | ||
compressOutputStream.close(); | ||
long rawSize = entity.getContentLength(); | ||
long compressSize = countingOutputStream.getCount(); | ||
float compressRatio = compressSize == 0 ? 1 : (float) rawSize / compressSize; | ||
LOG.info("Write entity for table {}, raw/compressed size:{}/{}, compress ratio:{}, time:{}us", | ||
entity.getTableUniqueKey(), rawSize, compressSize, compressRatio, (System.nanoTime() - startTime) / 1000) ; | ||
} | ||
|
||
@Override | ||
public boolean isStreaming() { | ||
return false; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...am-load-sdk/src/main/java/com/starrocks/data/load/stream/compress/CompressionOptions.java
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,29 @@ | ||
/* | ||
* Copyright 2021-present StarRocks, Inc. All rights reserved. | ||
* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.starrocks.data.load.stream.compress; | ||
|
||
import net.jpountz.lz4.LZ4FrameOutputStream; | ||
|
||
public class CompressionOptions { | ||
|
||
public static final String LZ4_BLOCK_SIZE = "compression.lz4.block.size"; | ||
public static final LZ4FrameOutputStream.BLOCKSIZE DEFAULT_LZ4_BLOCK_SIZE = LZ4FrameOutputStream.BLOCKSIZE.SIZE_4MB; | ||
} |
50 changes: 50 additions & 0 deletions
50
...-load-sdk/src/main/java/com/starrocks/data/load/stream/compress/CountingOutputStream.java
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 2021-present StarRocks, Inc. All rights reserved. | ||
* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.starrocks.data.load.stream.compress; | ||
|
||
import java.io.FilterOutputStream; | ||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
|
||
/** Count how many bytes written. */ | ||
public class CountingOutputStream extends FilterOutputStream { | ||
|
||
private long count; | ||
|
||
public CountingOutputStream(OutputStream out) { | ||
super(out); | ||
} | ||
|
||
public long getCount() { | ||
return this.count; | ||
} | ||
|
||
public void write(byte[] b, int off, int len) throws IOException { | ||
this.out.write(b, off, len); | ||
this.count += len; | ||
} | ||
|
||
public void write(int b) throws IOException { | ||
this.out.write(b); | ||
++this.count; | ||
} | ||
|
||
} |
Oops, something went wrong.