-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* log-exporter: export action and event in parquet format
Signed-off-by: neo <1100909+neowu@users.noreply.github.com>
- Loading branch information
Showing
15 changed files
with
312 additions
and
79 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
65 changes: 65 additions & 0 deletions
65
ext/log-exporter/src/main/java/core/log/domain/ActionLogSchema.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,65 @@ | ||
package core.log.domain; | ||
|
||
import core.framework.log.message.ActionLogMessage; | ||
import core.framework.log.message.PerformanceStatMessage; | ||
import core.framework.util.Maps; | ||
import org.apache.avro.LogicalTypes; | ||
import org.apache.avro.Schema; | ||
import org.apache.avro.SchemaBuilder; | ||
import org.apache.avro.generic.GenericData; | ||
|
||
import java.util.Map; | ||
|
||
public class ActionLogSchema { | ||
public final Schema schema; | ||
|
||
public ActionLogSchema() { | ||
schema = SchemaBuilder.record("action") | ||
.fields() | ||
.requiredString("id") | ||
.name("date").type().optional().type(LogicalTypes.timestampMicros().addToSchema(Schema.create(Schema.Type.LONG))) | ||
.requiredString("app") | ||
.requiredString("host") | ||
.requiredString("result") | ||
.requiredString("action") | ||
.name("correlation_ids").type().optional().array().items().stringType() | ||
.name("client").type().optional().array().items().stringType() | ||
.name("ref_ids").type().optional().array().items().stringType() | ||
.optionalString("error_code") | ||
.optionalString("error_message") | ||
.requiredLong("elapsed") | ||
.name("context").type().optional().type(SchemaBuilder.map().values().stringType()) | ||
.name("stats").type().optional().map().values().doubleType() | ||
.name("perf_stats").type().optional().map().values().longType() | ||
.endRecord(); | ||
} | ||
|
||
public GenericData.Record record(ActionLogMessage message) { | ||
var record = new GenericData.Record(schema); | ||
record.put("id", message.id); | ||
record.put("date", message.date); | ||
record.put("app", message.app); | ||
record.put("host", message.host); | ||
record.put("result", message.result); | ||
record.put("action", message.action); | ||
record.put("correlation_ids", message.correlationIds); | ||
record.put("client", message.clients); | ||
record.put("ref_ids", message.refIds); | ||
record.put("error_code", message.errorCode); | ||
record.put("error_message", message.errorMessage); | ||
record.put("elapsed", message.elapsed); | ||
record.put("context", message.context); | ||
record.put("stats", message.stats); | ||
Map<String, Long> perfStats = Maps.newHashMapWithExpectedSize(message.performanceStats.size() * 3); | ||
for (Map.Entry<String, PerformanceStatMessage> entry : message.performanceStats.entrySet()) { | ||
String key = entry.getKey(); | ||
PerformanceStatMessage stat = entry.getValue(); | ||
perfStats.put(key + ".count", (long) stat.count); | ||
perfStats.put(key + ".total_elapsed", stat.totalElapsed); | ||
if (stat.readEntries != null) perfStats.put(key + ".read_entries", (long) stat.readEntries); | ||
if (stat.writeEntries != null) perfStats.put(key + ".write_entries", (long) stat.writeEntries); | ||
} | ||
record.put("perf_stats", message.performanceStats); | ||
return record; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
ext/log-exporter/src/main/java/core/log/domain/EventSchema.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,46 @@ | ||
package core.log.domain; | ||
|
||
import core.framework.log.message.EventMessage; | ||
import org.apache.avro.LogicalTypes; | ||
import org.apache.avro.Schema; | ||
import org.apache.avro.SchemaBuilder; | ||
import org.apache.avro.generic.GenericData; | ||
|
||
public class EventSchema { | ||
public final Schema schema; | ||
|
||
public EventSchema() { | ||
schema = SchemaBuilder.record("event") | ||
.fields() | ||
.requiredString("id") | ||
.name("date").type().optional().type(LogicalTypes.timestampMicros().addToSchema(Schema.create(Schema.Type.LONG))) | ||
.requiredString("app") | ||
.name("received_time").type().optional().type(LogicalTypes.timestampMicros().addToSchema(Schema.create(Schema.Type.LONG))) | ||
.requiredString("result") | ||
.requiredString("action") | ||
.optionalString("error_code") | ||
.optionalString("error_message") | ||
.requiredLong("elapsed") | ||
.name("context").type().optional().type(SchemaBuilder.map().values().stringType()) | ||
.name("stats").type().optional().map().values().doubleType() | ||
.name("info").type().optional().type(SchemaBuilder.map().values().stringType()) | ||
.endRecord(); | ||
} | ||
|
||
public GenericData.Record record(EventMessage message) { | ||
var record = new GenericData.Record(schema); | ||
record.put("id", message.id); | ||
record.put("date", message.date); | ||
record.put("app", message.app); | ||
record.put("received_time", message.receivedTime); | ||
record.put("result", message.result); | ||
record.put("action", message.action); | ||
record.put("error_code", message.errorCode); | ||
record.put("error_message", message.errorMessage); | ||
record.put("elapsed", message.elapsed); | ||
record.put("context", message.context); | ||
record.put("stats", message.stats); | ||
record.put("info", message.info); | ||
return record; | ||
} | ||
} |
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
54 changes: 18 additions & 36 deletions
54
ext/log-exporter/src/main/java/core/log/kafka/ActionLogMessageHandler.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,64 +1,46 @@ | ||
package core.log.kafka; | ||
|
||
import core.framework.inject.Inject; | ||
import core.framework.internal.json.JSONWriter; | ||
import core.framework.kafka.BulkMessageHandler; | ||
import core.framework.kafka.Message; | ||
import core.framework.log.message.ActionLogMessage; | ||
import core.log.domain.ActionLogEntry; | ||
import core.log.domain.ActionLogSchema; | ||
import core.log.service.ArchiveService; | ||
import org.apache.avro.file.DataFileWriter; | ||
import org.apache.avro.generic.GenericData; | ||
import org.apache.avro.specific.SpecificDatumWriter; | ||
|
||
import java.io.BufferedOutputStream; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.time.LocalDateTime; | ||
import java.time.LocalDate; | ||
import java.util.List; | ||
|
||
import static java.nio.file.StandardOpenOption.APPEND; | ||
import static java.nio.file.StandardOpenOption.CREATE; | ||
|
||
/** | ||
* @author neo | ||
*/ | ||
public class ActionLogMessageHandler implements BulkMessageHandler<ActionLogMessage> { | ||
private final JSONWriter<ActionLogEntry> writer = new JSONWriter<>(ActionLogEntry.class); | ||
|
||
@Inject | ||
ArchiveService archiveService; | ||
@Inject | ||
ActionLogSchema schema; | ||
|
||
@Override | ||
public void handle(List<Message<ActionLogMessage>> messages) throws IOException { | ||
LocalDateTime now = LocalDateTime.now(); | ||
LocalDate now = LocalDate.now(); | ||
|
||
Path path = archiveService.initializeLogFilePath(archiveService.actionLogPath(now.toLocalDate())); | ||
try (BufferedOutputStream stream = new BufferedOutputStream(Files.newOutputStream(path, CREATE, APPEND), 3 * 1024 * 1024)) { | ||
for (Message<ActionLogMessage> message : messages) { | ||
ActionLogEntry entry = entry(message.value); | ||
Path path = archiveService.localActionLogFilePath(now); | ||
archiveService.createParentDir(path); | ||
|
||
stream.write(writer.toJSON(entry)); | ||
stream.write('\n'); | ||
try (DataFileWriter<GenericData.Record> writer = new DataFileWriter<>(new SpecificDatumWriter<>(schema.schema))) { | ||
if (!Files.exists(path)) { | ||
writer.create(schema.schema, path.toFile()); | ||
} else { | ||
writer.appendTo(path.toFile()); | ||
} | ||
for (Message<ActionLogMessage> message : messages) { | ||
writer.append(schema.record(message.value)); | ||
} | ||
} | ||
} | ||
|
||
private ActionLogEntry entry(ActionLogMessage message) { | ||
var entry = new ActionLogEntry(); | ||
entry.id = message.id; | ||
entry.date = message.date; | ||
entry.app = message.app; | ||
entry.host = message.host; | ||
entry.result = message.result; | ||
entry.action = message.action; | ||
entry.correlationIds = message.correlationIds; | ||
entry.clients = message.clients; | ||
entry.refIds = message.refIds; | ||
entry.errorCode = message.errorCode; | ||
entry.errorMessage = message.errorMessage; | ||
entry.elapsed = message.elapsed; | ||
entry.context = message.context; | ||
entry.stats = message.stats; | ||
entry.performanceStats = message.performanceStats; | ||
return entry; | ||
} | ||
} |
29 changes: 17 additions & 12 deletions
29
ext/log-exporter/src/main/java/core/log/kafka/EventMessageHandler.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
Oops, something went wrong.