-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
abstracted the event writer implementation and implemented a new Kine…
…sisEventWriter
- Loading branch information
1 parent
3f569ff
commit ee0c742
Showing
5 changed files
with
165 additions
and
54 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
33 changes: 33 additions & 0 deletions
33
src/main/java/com/dellemc/sdp/demo/music/KinesisEventWriter.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,33 @@ | ||
package com.dellemc.sdp.demo.music; | ||
|
||
import com.amazonaws.auth.profile.ProfileCredentialsProvider; | ||
import com.amazonaws.services.kinesis.AmazonKinesis; | ||
import com.amazonaws.services.kinesis.AmazonKinesisClientBuilder; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
public class KinesisEventWriter implements SongEventGenerator.EventWriter, AutoCloseable { | ||
SongEventGenerator.Config config; | ||
AmazonKinesis kinesisClient; | ||
|
||
public KinesisEventWriter(SongEventGenerator.Config config) { | ||
this.config = config; | ||
kinesisClient = AmazonKinesisClientBuilder.standard().withCredentials(new ProfileCredentialsProvider(config.getAwsProfile())).build(); | ||
} | ||
|
||
@Override | ||
public void writeEvent(String routingKey, String body) { | ||
kinesisClient.putRecord(config.getStream(), ByteBuffer.wrap(body.getBytes(StandardCharsets.UTF_8)), routingKey); | ||
} | ||
|
||
@Override | ||
public synchronized void close() { | ||
try { | ||
if (kinesisClient != null) kinesisClient.shutdown(); | ||
} catch (Throwable t) { | ||
t.printStackTrace(); | ||
} | ||
kinesisClient = null; | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
src/main/java/com/dellemc/sdp/demo/music/PravegaEventWriter.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,80 @@ | ||
package com.dellemc.sdp.demo.music; | ||
|
||
import io.pravega.client.ClientConfig; | ||
import io.pravega.client.EventStreamClientFactory; | ||
import io.pravega.client.admin.StreamManager; | ||
import io.pravega.client.stream.EventStreamWriter; | ||
import io.pravega.client.stream.EventWriterConfig; | ||
import io.pravega.client.stream.ScalingPolicy; | ||
import io.pravega.client.stream.StreamConfiguration; | ||
import io.pravega.client.stream.impl.UTF8StringSerializer; | ||
import io.pravega.keycloak.client.PravegaKeycloakCredentials; | ||
|
||
import java.net.URI; | ||
|
||
public class PravegaEventWriter implements SongEventGenerator.EventWriter, AutoCloseable { | ||
SongEventGenerator.Config config; | ||
EventStreamClientFactory clientFactory; | ||
EventStreamWriter<String> writer; | ||
|
||
public PravegaEventWriter(SongEventGenerator.Config config) { | ||
this.config = config; | ||
|
||
// create stream | ||
ClientConfig clientConfig = createClientConfig(); | ||
createStream(clientConfig); | ||
|
||
// create writer | ||
clientFactory = EventStreamClientFactory.withScope(config.getScope(), clientConfig); | ||
writer = clientFactory.createEventWriter( | ||
config.getStream(), new UTF8StringSerializer(), EventWriterConfig.builder().build()); | ||
} | ||
|
||
@Override | ||
public void writeEvent(String routingKey, String body) { | ||
writer.writeEvent(routingKey, body); | ||
} | ||
|
||
@Override | ||
public synchronized void close() { | ||
try { | ||
if (writer != null) writer.close(); | ||
} catch (Throwable t) { | ||
t.printStackTrace(); | ||
} | ||
writer = null; | ||
try { | ||
if (clientFactory != null) clientFactory.close(); | ||
} catch (Throwable t) { | ||
t.printStackTrace(); | ||
} | ||
clientFactory = null; | ||
} | ||
|
||
ClientConfig createClientConfig() { | ||
ClientConfig.ClientConfigBuilder builder = ClientConfig.builder(); | ||
builder.controllerURI(URI.create(config.getControllerEndpoint())); | ||
|
||
// Keycloak means we are using Streaming Data Platform | ||
if (config.isUseKeycloak()) { | ||
builder.credentials(new PravegaKeycloakCredentials()); | ||
} | ||
|
||
return builder.build(); | ||
} | ||
|
||
void createStream(ClientConfig clientConfig) { | ||
try (StreamManager streamManager = StreamManager.create(clientConfig)) { | ||
|
||
// create the scope | ||
if (!config.isUseKeycloak()) // can't create a scope in SDP | ||
streamManager.createScope(config.getScope()); | ||
|
||
// create the stream | ||
StreamConfiguration streamConfiguration = StreamConfiguration.builder() | ||
.scalingPolicy(ScalingPolicy.byEventRate(5, 2, 2)) | ||
.build(); | ||
streamManager.createStream(config.getScope(), config.getStream(), streamConfiguration); | ||
} | ||
} | ||
} |
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