-
Notifications
You must be signed in to change notification settings - Fork 323
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Maciej Obuchowski <obuchowski.maciej@gmail.com>
- Loading branch information
1 parent
7c6ddd8
commit cf37442
Showing
19 changed files
with
450 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package marquez.api; | ||
|
||
import com.codahale.metrics.annotation.ExceptionMetered; | ||
import com.codahale.metrics.annotation.ResponseMetered; | ||
import com.codahale.metrics.annotation.Timed; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.NonNull; | ||
import lombok.Value; | ||
import marquez.api.exceptions.NamespaceNotFoundException; | ||
import marquez.common.models.NamespaceName; | ||
import marquez.service.ServiceFactory; | ||
import marquez.service.models.LineageEvent; | ||
import marquez.service.models.Namespace; | ||
import marquez.service.models.NamespaceMeta; | ||
import marquez.service.models.RawLineageEvent; | ||
|
||
import javax.validation.Valid; | ||
import javax.validation.constraints.Min; | ||
import javax.ws.rs.Consumes; | ||
import javax.ws.rs.DefaultValue; | ||
import javax.ws.rs.GET; | ||
import javax.ws.rs.PUT; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.PathParam; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.QueryParam; | ||
import javax.ws.rs.core.Response; | ||
|
||
import java.util.List; | ||
|
||
import static javax.ws.rs.core.MediaType.APPLICATION_JSON; | ||
|
||
@Path("/api/v1") | ||
public class EventResource extends BaseResource { | ||
public EventResource(@NonNull final ServiceFactory serviceFactory) { | ||
super(serviceFactory); | ||
} | ||
|
||
@Timed | ||
@ResponseMetered | ||
@ExceptionMetered | ||
@GET | ||
@Path("/events") | ||
@Produces(APPLICATION_JSON) | ||
public Response get( | ||
@QueryParam("limit") @DefaultValue("100") @Min(value = 0) int limit, | ||
@QueryParam("offset") @DefaultValue("0") @Min(value = 0) int offset) { | ||
final List<RawLineageEvent> events = | ||
eventService.getAll(limit, offset); | ||
return Response.ok(new Events(events)).build(); | ||
} | ||
|
||
@Timed | ||
@ResponseMetered | ||
@ExceptionMetered | ||
@GET | ||
@Path("/events/{namespace}") | ||
@Produces(APPLICATION_JSON) | ||
public Response getByNamespace( | ||
@PathParam("namespace") String namespace, | ||
@QueryParam("limit") @DefaultValue("100") @Min(value = 0) int limit, | ||
@QueryParam("offset") @DefaultValue("0") @Min(value = 0) int offset) { | ||
final List<RawLineageEvent> event = | ||
eventService.getByNamespace(namespace, limit, offset); | ||
return Response.ok(new Events(event)).build(); | ||
} | ||
|
||
@Value | ||
static class Events { | ||
@NonNull | ||
@JsonProperty("events") | ||
List<RawLineageEvent> value; | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package marquez.db; | ||
|
||
|
||
import marquez.db.mappers.RawLineageEventMapper; | ||
import marquez.service.models.RawLineageEvent; | ||
import org.jdbi.v3.sqlobject.config.RegisterRowMapper; | ||
import org.jdbi.v3.sqlobject.customizer.Bind; | ||
import org.jdbi.v3.sqlobject.statement.SqlQuery; | ||
import java.util.List; | ||
|
||
@RegisterRowMapper(RawLineageEventMapper.class) | ||
public interface EventDao extends BaseDao { | ||
|
||
@SqlQuery(""" | ||
SELECT * | ||
FROM lineage_events | ||
ORDER BY event_time DESC | ||
LIMIT :limit | ||
OFFSET :offset""") | ||
List<RawLineageEvent> getAll(int limit, int offset); | ||
|
||
|
||
/** | ||
* This is a "hack" to get inputs/outputs namespace from jsonb column: | ||
* <a href="https://github.com/jdbi/jdbi/issues/1510#issuecomment-485423083">explanation</a> | ||
*/ | ||
@SqlQuery(""" | ||
SELECT le.* | ||
FROM lineage_events le, jsonb_array_elements(coalesce(le.event -> 'inputs', '[]'::jsonb) || coalesce(le.event -> 'outputs', '[]'::jsonb)) AS ds | ||
WHERE le.job_namespace = :namespace | ||
OR ds ->> 'namespace' = :namespace | ||
LIMIT :limit | ||
OFFSET :offset""") | ||
List<RawLineageEvent> getByNamespace(@Bind("namespace") String namespace, @Bind("limit") int limit, @Bind("offset") int offset); | ||
} |
53 changes: 53 additions & 0 deletions
53
api/src/main/java/marquez/db/mappers/RawLineageEventMapper.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,53 @@ | ||
package marquez.db.mappers; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import lombok.extern.slf4j.Slf4j; | ||
import marquez.common.Utils; | ||
import marquez.db.Columns; | ||
import marquez.service.models.LineageEvent; | ||
import marquez.service.models.RawLineageEvent; | ||
import org.jdbi.v3.core.mapper.RowMapper; | ||
import org.jdbi.v3.core.statement.StatementContext; | ||
|
||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static marquez.db.Columns.stringOrThrow; | ||
import static marquez.db.Columns.zonedDateTimeOrThrow; | ||
|
||
@Slf4j | ||
public class RawLineageEventMapper implements RowMapper<RawLineageEvent> { | ||
@Override | ||
public RawLineageEvent map(ResultSet rs, StatementContext ctx) throws SQLException { | ||
String rawEvent = stringOrThrow(rs, Columns.EVENT); | ||
Map<String, Object> run = Collections.emptyMap(); | ||
Map<String, Object> job = Collections.emptyMap(); | ||
|
||
List<Object> inputs = Collections.emptyList(); | ||
List<Object> outputs = Collections.emptyList(); | ||
try { | ||
ObjectMapper mapper = Utils.getMapper(); | ||
Map<String, Object> event = mapper.readValue(rawEvent, Map.class); | ||
run = (Map<String, Object>) event.getOrDefault("run", Collections.emptyMap()); | ||
job = (Map<String, Object>) event.getOrDefault("job", Collections.emptyMap()); | ||
inputs = (List<Object>) event.getOrDefault("inputs", Collections.emptyList()); | ||
outputs = (List<Object>) event.getOrDefault("outputs", Collections.emptyList()); | ||
} catch (JsonProcessingException e) { | ||
log.error("Failed to process json", e); | ||
} | ||
|
||
return RawLineageEvent.builder() | ||
.eventTime(zonedDateTimeOrThrow(rs, Columns.EVENT_TIME)) | ||
.eventType(stringOrThrow(rs, Columns.EVENT_TYPE)) | ||
.run(run) | ||
.job(job) | ||
.inputs(inputs) | ||
.outputs(outputs) | ||
.producer(stringOrThrow(rs, Columns.PRODUCER)) | ||
.build(); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package marquez.service; | ||
|
||
import lombok.NonNull; | ||
import marquez.db.BaseDao; | ||
|
||
public class EventService extends DelegatingDaos.DelegatingEventDao { | ||
public EventService(@NonNull BaseDao baseDao) { | ||
super(baseDao.createEventDao()); | ||
} | ||
} |
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.