2424import org .apache .commons .logging .Log ;
2525import org .apache .commons .logging .LogFactory ;
2626
27+ import com .mcplusa .kinesis .KinesisConnectorExecutorBase ;
2728import com .mcplusa .kinesis .utils .CloudFormationUtils ;
2829import com .mcplusa .kinesis .utils .DynamoDBUtils ;
2930import com .mcplusa .kinesis .utils .EC2Utils ;
4041import com .amazonaws .services .ec2 .AmazonEC2 ;
4142import com .amazonaws .services .ec2 .AmazonEC2Client ;
4243import com .amazonaws .services .kinesis .connectors .KinesisConnectorConfiguration ;
43- import com .amazonaws .services .kinesis .connectors .KinesisConnectorExecutorBase ;
4444import com .amazonaws .services .redshift .AmazonRedshiftClient ;
4545import com .amazonaws .services .s3 .AmazonS3Client ;
4646
@@ -52,37 +52,6 @@ public abstract class KinesisConnectorExecutor<T, U> extends KinesisConnectorExe
5252
5353 private static final Log LOG = LogFactory .getLog (KinesisConnectorExecutor .class );
5454
55- // Create AWS Resource constants
56- private static final String CREATE_KINESIS_INPUT_STREAM = "createKinesisInputStream" ;
57- private static final String CREATE_KINESIS_OUTPUT_STREAM = "createKinesisOutputStream" ;
58- private static final String CREATE_DYNAMODB_DATA_TABLE = "createDynamoDBDataTable" ;
59- private static final String CREATE_REDSHIFT_CLUSTER = "createRedshiftCluster" ;
60- private static final String CREATE_REDSHIFT_DATA_TABLE = "createRedshiftDataTable" ;
61- private static final String CREATE_REDSHIFT_FILE_TABLE = "createRedshiftFileTable" ;
62- private static final String CREATE_S3_BUCKET = "createS3Bucket" ;
63- private static final String CREATE_ELASTICSEARCH_CLUSTER = "createElasticsearchCluster" ;
64- private static final boolean DEFAULT_CREATE_RESOURCES = false ;
65-
66- // Create Amazon DynamoDB Resource constants
67- private static final String DYNAMODB_KEY = "dynamoDBKey" ;
68- private static final String DYNAMODB_READ_CAPACITY_UNITS = "readCapacityUnits" ;
69- private static final String DYNAMODB_WRITE_CAPACITY_UNITS = "writeCapacityUnits" ;
70- private static final Long DEFAULT_DYNAMODB_READ_CAPACITY_UNITS = 1l ;
71- private static final Long DEFAULT_DYNAMODB_WRITE_CAPACITY_UNITS = 1l ;
72- // Create Amazon Redshift Resource constants
73- private static final String REDSHIFT_CLUSTER_IDENTIFIER = "redshiftClusterIdentifier" ;
74- private static final String REDSHIFT_DATABASE_NAME = "redshiftDatabaseName" ;
75- private static final String REDSHIFT_CLUSTER_TYPE = "redshiftClusterType" ;
76- private static final String REDSHIFT_NUMBER_OF_NODES = "redshiftNumberOfNodes" ;
77- private static final int DEFAULT_REDSHIFT_NUMBER_OF_NODES = 2 ;
78-
79- // Create Amazon S3 Resource constants
80- private static final String S3_BUCKET = "s3Bucket" ;
81-
82- // Elasticsearch Cluster Resource constants
83- private static final String EC2_ELASTICSEARCH_FILTER_NAME = "tag:type" ;
84- private static final String EC2_ELASTICSEARCH_FILTER_VALUE = "elasticsearch" ;
85-
8655 // Create Stream Source constants
8756 private static final String CREATE_STREAM_SOURCE = "createStreamSource" ;
8857 private static final String LOOP_OVER_STREAM_SOURCE = "loopOverStreamSource" ;
@@ -101,6 +70,7 @@ public abstract class KinesisConnectorExecutor<T, U> extends KinesisConnectorExe
10170 * The name of the configuration file to look for on the classpath
10271 */
10372 public KinesisConnectorExecutor (String configFile ) {
73+ // Load configuration properties
10474 InputStream configStream = Thread .currentThread ().getContextClassLoader ().getResourceAsStream (configFile );
10575
10676 if (configStream == null ) {
@@ -116,12 +86,12 @@ public KinesisConnectorExecutor(String configFile) {
11686 throw new IllegalStateException (msg , e );
11787 }
11888 this .config = new KinesisConnectorConfiguration (properties , getAWSCredentialsProvider ());
119- setupAWSResources ();
89+
90+ // Send sample data to AWS Kinesis if specified in the properties file
12091 setupInputStream ();
12192
12293 // Initialize executor with configurations
12394 super .initialize (config );
124- System .out .println ("##Initialized!!" );
12595 }
12696
12797 /**
@@ -135,55 +105,6 @@ public AWSCredentialsProvider getAWSCredentialsProvider() {
135105 return new ClasspathPropertiesFileCredentialsProvider ("SumologicConnector.properties" );
136106 }
137107
138- /**
139- * Setup necessary AWS resources for the samples. By default, the Executor does not create any
140- * AWS resources. The user must specify true for the specific create properties in the
141- * configuration file.
142- */
143- private void setupAWSResources () {
144- if (parseBoolean (CREATE_KINESIS_INPUT_STREAM , DEFAULT_CREATE_RESOURCES , properties )) {
145- KinesisUtils .createInputStream (config );
146- }
147-
148- if (parseBoolean (CREATE_KINESIS_OUTPUT_STREAM , DEFAULT_CREATE_RESOURCES , properties )) {
149- KinesisUtils .createOutputStream (config );
150- }
151-
152- if (parseBoolean (CREATE_DYNAMODB_DATA_TABLE , DEFAULT_CREATE_RESOURCES , properties )) {
153- String key = properties .getProperty (DYNAMODB_KEY );
154- Long readCapacityUnits =
155- parseLong (DYNAMODB_READ_CAPACITY_UNITS , DEFAULT_DYNAMODB_READ_CAPACITY_UNITS , properties );
156- Long writeCapacityUnits =
157- parseLong (DYNAMODB_WRITE_CAPACITY_UNITS , DEFAULT_DYNAMODB_WRITE_CAPACITY_UNITS , properties );
158- createDynamoDBTable (key , readCapacityUnits , writeCapacityUnits );
159- }
160-
161- if (parseBoolean (CREATE_REDSHIFT_CLUSTER , DEFAULT_CREATE_RESOURCES , properties )) {
162- String clusterIdentifier = properties .getProperty (REDSHIFT_CLUSTER_IDENTIFIER );
163- String databaseName = properties .getProperty (REDSHIFT_DATABASE_NAME );
164- String clusterType = properties .getProperty (REDSHIFT_CLUSTER_TYPE );
165- int numberOfNodes = parseInt (REDSHIFT_NUMBER_OF_NODES , DEFAULT_REDSHIFT_NUMBER_OF_NODES , properties );
166- createRedshiftCluster (clusterIdentifier , databaseName , clusterType , numberOfNodes );
167- }
168-
169- if (parseBoolean (CREATE_REDSHIFT_DATA_TABLE , DEFAULT_CREATE_RESOURCES , properties )) {
170- createRedshiftDataTable ();
171- }
172-
173- if (parseBoolean (CREATE_REDSHIFT_FILE_TABLE , DEFAULT_CREATE_RESOURCES , properties )) {
174- createRedshiftFileTable ();
175- }
176-
177- if (parseBoolean (CREATE_S3_BUCKET , DEFAULT_CREATE_RESOURCES , properties )) {
178- String s3Bucket = properties .getProperty (S3_BUCKET );
179- createS3Bucket (s3Bucket );
180- }
181-
182- if (parseBoolean (CREATE_ELASTICSEARCH_CLUSTER , DEFAULT_CREATE_RESOURCES , properties )) {
183- createElasticsearchCluster ();
184- }
185- }
186-
187108 /**
188109 * Helper method to spawn the {@link StreamSource} in a separate thread.
189110 */
@@ -209,174 +130,6 @@ private void setupInputStream() {
209130 }
210131 }
211132
212- /**
213- * Helper method to create the Amazon DynamoDB table.
214- *
215- * @param key
216- * The name of the hashkey field in the Amazon DynamoDB table
217- * @param readCapacityUnits
218- * Read capacity of the Amazon DynamoDB table
219- * @param writeCapacityUnits
220- * Write capacity of the Amazon DynamoDB table
221- */
222- private void createDynamoDBTable (String key , long readCapacityUnits , long writeCapacityUnits ) {
223- LOG .info ("Creating Amazon DynamoDB table " + config .DYNAMODB_DATA_TABLE_NAME );
224- AmazonDynamoDBClient dynamodbClient = new AmazonDynamoDBClient (config .AWS_CREDENTIALS_PROVIDER );
225- dynamodbClient .setEndpoint (config .DYNAMODB_ENDPOINT );
226- DynamoDBUtils .createTable (dynamodbClient ,
227- config .DYNAMODB_DATA_TABLE_NAME ,
228- key ,
229- readCapacityUnits ,
230- writeCapacityUnits );
231- }
232-
233- /**
234- * Helper method to create the Amazon Redshift cluster.
235- *
236- * @param clusterIdentifier
237- * Unique identifier for the name of the Amazon Redshift cluster
238- * @param databaseName
239- * Name for the database in the Amazon Redshift cluster
240- * @param clusterType
241- * dw.hs1.xlarge or dw.hs1.8xlarge
242- * @param numberOfNodes
243- * Number of nodes for the Amazon Redshift cluster
244- */
245- private void createRedshiftCluster (String clusterIdentifier ,
246- String databaseName ,
247- String clusterType ,
248- int numberOfNodes ) {
249- // Make sure the Amazon Redshift cluster is available
250- AmazonRedshiftClient redshiftClient = new AmazonRedshiftClient (config .AWS_CREDENTIALS_PROVIDER );
251- redshiftClient .setEndpoint (config .REDSHIFT_ENDPOINT );
252- LOG .info ("Creating Amazon Redshift cluster " + clusterIdentifier );
253- RedshiftUtils .createCluster (redshiftClient ,
254- clusterIdentifier ,
255- databaseName ,
256- config .REDSHIFT_USERNAME ,
257- config .REDSHIFT_PASSWORD ,
258- clusterType ,
259- numberOfNodes );
260-
261- // Update the Amazon Redshift connection url
262- config .REDSHIFT_URL = RedshiftUtils .getClusterURL (redshiftClient , clusterIdentifier );
263- }
264-
265- /**
266- * Helper method to create the data table in Amazon Redshift.
267- */
268- private void createRedshiftDataTable () {
269- Properties p = new Properties ();
270- p .setProperty ("user" , config .REDSHIFT_USERNAME );
271- p .setProperty ("password" , config .REDSHIFT_PASSWORD );
272- if (RedshiftUtils .tableExists (p , config .REDSHIFT_URL , config .REDSHIFT_DATA_TABLE )) {
273- LOG .info ("Amazon Redshift data table " + config .REDSHIFT_DATA_TABLE + " exists." );
274- return ;
275- }
276- try {
277- LOG .info ("Creating Amazon Redshift data table " + config .REDSHIFT_DATA_TABLE );
278- RedshiftUtils .createRedshiftTable (config .REDSHIFT_URL ,
279- p ,
280- config .REDSHIFT_DATA_TABLE ,
281- getKinesisMessageModelFields ());
282- } catch (SQLException e ) {
283- String msg = "Could not create Amazon Redshift data table " + config .REDSHIFT_DATA_TABLE ;
284- throw new IllegalStateException (msg , e );
285- }
286- }
287-
288- /**
289- * Helper method to create the file table in Amazon Redshift.
290- */
291- private void createRedshiftFileTable () {
292- Properties p = new Properties ();
293- p .setProperty ("user" , config .REDSHIFT_USERNAME );
294- p .setProperty ("password" , config .REDSHIFT_PASSWORD );
295- if (RedshiftUtils .tableExists (p , config .REDSHIFT_URL , config .REDSHIFT_FILE_TABLE )) {
296- LOG .info ("Amazon Redshift file table " + config .REDSHIFT_FILE_TABLE + " exists." );
297- return ;
298- }
299- try {
300- LOG .info ("Creating Amazon Redshift file table " + config .REDSHIFT_FILE_TABLE );
301- RedshiftUtils .createRedshiftTable (config .REDSHIFT_URL , p , config .REDSHIFT_FILE_TABLE , getFileTableFields ());
302- } catch (SQLException e ) {
303- String msg = "Could not create Amazon Redshift file table " + config .REDSHIFT_FILE_TABLE ;
304- throw new IllegalStateException (msg , e );
305- }
306- }
307-
308- /**
309- * Helper method to build the data table.
310- *
311- * @return Fields for the data table
312- */
313- private static List <String > getKinesisMessageModelFields () {
314- List <String > fields = new ArrayList <String >();
315- fields .add ("userid integer not null distkey sortkey" );
316- fields .add ("username char(8)" );
317- fields .add ("firstname varchar(30)" );
318- fields .add ("lastname varchar(30)" );
319- fields .add ("city varchar(30)" );
320- fields .add ("state char(2)" );
321- fields .add ("email varchar(100)" );
322- fields .add ("phone char(14)" );
323- fields .add ("likesports boolean" );
324- fields .add ("liketheatre boolean" );
325- fields .add ("likeconcerts boolean" );
326- fields .add ("likejazz boolean" );
327- fields .add ("likeclassical boolean" );
328- fields .add ("likeopera boolean" );
329- fields .add ("likerock boolean" );
330- fields .add ("likevegas boolean" );
331- fields .add ("likebroadway boolean" );
332- fields .add ("likemusicals boolean" );
333- return fields ;
334- }
335-
336- /**
337- * Helper method to help create the file table.
338- *
339- * @return File table fields
340- */
341- private List <String > getFileTableFields () {
342- List <String > fields = new ArrayList <String >();
343- fields .add (config .REDSHIFT_FILE_KEY_COLUMN + " varchar(255) primary key" );
344- return fields ;
345- }
346-
347- /**
348- * Helper method to create the Amazon S3 bucket.
349- *
350- * @param s3Bucket
351- * The name of the bucket to create
352- */
353- private void createS3Bucket (String s3Bucket ) {
354- AmazonS3Client client = new AmazonS3Client (config .AWS_CREDENTIALS_PROVIDER );
355- client .setEndpoint (config .S3_ENDPOINT );
356- LOG .info ("Creating Amazon S3 bucket " + s3Bucket );
357- S3Utils .createBucket (client , s3Bucket );
358- }
359-
360- /**
361- * Helper method to create Elasticsearch cluster at set correct endpoint.
362- */
363- private void createElasticsearchCluster () {
364- // Create stack if not already up
365- AmazonCloudFormation cloudFormationClient = new AmazonCloudFormationClient (config .AWS_CREDENTIALS_PROVIDER );
366- cloudFormationClient .setRegion (RegionUtils .getRegion (config .REGION_NAME ));
367- CloudFormationUtils .createStackIfNotExists (cloudFormationClient , config );
368-
369- // Update the elasticsearch endpoint to use endpoint in created cluster
370- AmazonEC2 ec2Client = new AmazonEC2Client (config .AWS_CREDENTIALS_PROVIDER );
371- ec2Client .setRegion (RegionUtils .getRegion (config .REGION_NAME ));
372- config .ELASTICSEARCH_ENDPOINT =
373- EC2Utils .getEndpointForFirstActiveInstanceWithTag (ec2Client ,
374- EC2_ELASTICSEARCH_FILTER_NAME ,
375- EC2_ELASTICSEARCH_FILTER_VALUE );
376- if (config .ELASTICSEARCH_ENDPOINT == null || config .ELASTICSEARCH_ENDPOINT .isEmpty ()) {
377- throw new RuntimeException ("Could not find active Elasticsearch endpoint from cluster." );
378- }
379- }
380133
381134 /**
382135 * Helper method used to parse boolean properties.
0 commit comments