forked from raystack/firehose
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1e2b0e0
commit 9dafc98
Showing
12 changed files
with
190 additions
and
9 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
8 changes: 8 additions & 0 deletions
8
src/main/java/com/gotocompany/firehose/config/BlobConsumerConfig.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,8 @@ | ||
package com.gotocompany.firehose.config; | ||
|
||
import org.aeonbits.owner.Config; | ||
|
||
public interface BlobConsumerConfig extends Config { | ||
@Config.Key("BLOB_SOURCE_TYPE") | ||
String getSourceBlobBucketName(); | ||
} |
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
20 changes: 20 additions & 0 deletions
20
src/main/java/com/gotocompany/firehose/config/GcsBlobConsumerConfig.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,20 @@ | ||
package com.gotocompany.firehose.config; | ||
|
||
import org.aeonbits.owner.Config; | ||
|
||
public interface GcsBlobConsumerConfig extends Config { | ||
@Config.Key("GCS_BLOB_SOURCE_BUCKET_NAME") | ||
String getSourceBlobBucketName(); | ||
|
||
@Config.Key("GCS_BLOB_SOURCE_SERVICE_ACCOUNT") | ||
String getSourceBlobServiceAccount(); | ||
|
||
@Config.Key("GCS_BLOB_SOURCE_PROJECT_ID") | ||
String getSourceBlobProjectId(); | ||
|
||
@Config.Key("GCS_BLOB_SOURCE_PATH_PREFIX") | ||
String getSourcePathPrefix(); | ||
|
||
@Config.Key("GCS_BLOB_ARCHIVE_PATH") | ||
String getSourceArchivePath(); | ||
} |
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 |
---|---|---|
|
@@ -2,5 +2,6 @@ | |
|
||
public enum KafkaConsumerMode { | ||
ASYNC, | ||
SYNC | ||
SYNC, | ||
DLQ | ||
} |
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
77 changes: 77 additions & 0 deletions
77
src/main/java/com/gotocompany/firehose/consumer/GcsBlobStorageFirehoseConsumer.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,77 @@ | ||
package com.gotocompany.firehose.consumer; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.gotocompany.firehose.config.GcsBlobConsumerConfig; | ||
import com.gotocompany.firehose.message.Message; | ||
import com.gotocompany.firehose.sink.Sink; | ||
import com.gotocompany.firehose.sink.common.blobstorage.BlobStorage; | ||
import com.gotocompany.firehose.tracer.SinkTracer; | ||
import io.opentracing.Span; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.util.ArrayList; | ||
import java.util.Base64; | ||
import java.util.List; | ||
|
||
public class GcsBlobStorageFirehoseConsumer implements FirehoseConsumer { | ||
|
||
private final BlobStorage blobStorage; | ||
private final Sink sink; | ||
private final SinkTracer sinkTracer; | ||
private final GcsBlobConsumerConfig gcsBlobConsumerConfig; | ||
private final ObjectMapper objectMapper; | ||
|
||
public GcsBlobStorageFirehoseConsumer(BlobStorage blobStorage, | ||
Sink sink, | ||
SinkTracer sinkTracer, | ||
GcsBlobConsumerConfig gcsBlobConsumerConfig) { | ||
this.blobStorage = blobStorage; | ||
this.sink = sink; | ||
this.gcsBlobConsumerConfig = gcsBlobConsumerConfig; | ||
this.sinkTracer = sinkTracer; | ||
this.objectMapper = new ObjectMapper(); | ||
} | ||
|
||
@Override | ||
public void process() throws IOException { | ||
List<String> fileNames = blobStorage.list(gcsBlobConsumerConfig.getSourcePathPrefix()); | ||
|
||
for (String fileName : fileNames) { | ||
byte[] content = blobStorage.get(fileName); | ||
List<Message> messages = parseBlobToMessages(content); | ||
List<Span> spans = sinkTracer.startTrace(messages); | ||
sink.pushMessage(messages); | ||
sinkTracer.finishTrace(spans); | ||
} | ||
|
||
} | ||
|
||
private List<Message> parseBlobToMessages(byte[] content) throws IOException { | ||
InputStream inputStream = new ByteArrayInputStream(content); | ||
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); | ||
String line = bufferedReader.readLine(); | ||
List<Message> result = new ArrayList<>(); | ||
while (line != null) { | ||
JsonNode node = objectMapper.readTree(line); | ||
String logKey = node.get("key").asText(); | ||
String logMessage = node.get("value").asText(); | ||
String topic = node.get("topic").asText(); | ||
int partition = node.get("partition").asInt(); | ||
long offset = node.get("offset").asLong(); | ||
Message message = new Message(Base64.getDecoder().decode(logKey), Base64.getDecoder().decode(logMessage), topic, partition, offset); | ||
result.add(message); | ||
line = bufferedReader.readLine(); | ||
} | ||
return result; | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
sink.close(); | ||
} | ||
} |
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
8 changes: 8 additions & 0 deletions
8
src/main/java/com/gotocompany/firehose/sink/common/blobstorage/BlobStorage.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 |
---|---|---|
@@ -1,10 +1,18 @@ | ||
package com.gotocompany.firehose.sink.common.blobstorage; | ||
|
||
import com.google.cloud.storage.Blob; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Abstraction of any storage that store binary bytes as file. | ||
*/ | ||
public interface BlobStorage { | ||
void store(String objectName, String filePath) throws BlobStorageException; | ||
|
||
void store(String objectName, byte[] content) throws BlobStorageException; | ||
|
||
byte[] get(String filePath); | ||
|
||
List<String> list(String prefix); | ||
} |
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