@@ -294,6 +294,16 @@ private void createTableInternal(
294294 if (!(ifNotExists && tableExistsInternal (namespace , table ))) {
295295 client .createTable (requestBuilder .build ());
296296 waitForTableCreation (namespace , table );
297+ } else if (!metadata .getSecondaryIndexNames ().isEmpty ()) {
298+ for (String indexColumnName : metadata .getSecondaryIndexNames ()) {
299+ createIndex (
300+ namespace ,
301+ table ,
302+ indexColumnName ,
303+ metadata .getColumnDataType (indexColumnName ),
304+ ru ,
305+ true );
306+ }
297307 }
298308 } catch (Exception e ) {
299309 throw new ExecutionException (
@@ -938,46 +948,7 @@ public void createIndex(
938948 }
939949
940950 long ru = Long .parseLong (options .getOrDefault (REQUEST_UNIT , DEFAULT_REQUEST_UNIT ));
941-
942- try {
943- client .updateTable (
944- UpdateTableRequest .builder ()
945- .tableName (getFullTableName (namespace , table ))
946- .attributeDefinitions (
947- AttributeDefinition .builder ()
948- .attributeName (columnName )
949- .attributeType (
950- SECONDARY_INDEX_DATATYPE_MAP .get (metadata .getColumnDataType (columnName )))
951- .build ())
952- .globalSecondaryIndexUpdates (
953- GlobalSecondaryIndexUpdate .builder ()
954- .create (
955- CreateGlobalSecondaryIndexAction .builder ()
956- .indexName (getGlobalIndexName (namespace , table , columnName ))
957- .keySchema (
958- KeySchemaElement .builder ()
959- .attributeName (columnName )
960- .keyType (KeyType .HASH )
961- .build ())
962- .projection (
963- Projection .builder ().projectionType (ProjectionType .ALL ).build ())
964- .provisionedThroughput (
965- ProvisionedThroughput .builder ()
966- .readCapacityUnits (ru )
967- .writeCapacityUnits (ru )
968- .build ())
969- .build ())
970- .build ())
971- .build ());
972- } catch (Exception e ) {
973- throw new ExecutionException (
974- String .format (
975- "Creating the secondary index for the %s column of the %s table failed" ,
976- columnName , getFullTableName (namespace , table )),
977- e );
978- }
979-
980- waitForIndexCreation (namespace , table , columnName );
951+ createIndex (namespace , table , columnName , metadata .getColumnDataType (columnName ), ru , false );
981952
982953 // enable auto scaling
983954 boolean noScaling = Boolean .parseBoolean (options .getOrDefault (NO_SCALING , DEFAULT_NO_SCALING ));
@@ -1007,26 +978,86 @@ columnName, getFullTableName(namespace, table)),
1007978 TableMetadata .newBuilder (tableMetadata ).addSecondaryIndex (columnName ).build ());
1008979 }
1009980
1010- private void waitForIndexCreation (Namespace namespace , String table , String columnName )
981+ private void createIndex (
982+ Namespace namespace ,
983+ String table ,
984+ String columnName ,
985+ DataType dataType ,
986+ Long ru ,
987+ boolean ifNotExists )
1011988 throws ExecutionException {
1012989 try {
1013- String indexName = getGlobalIndexName (namespace , table , columnName );
1014- while (true ) {
1015- Uninterruptibles .sleepUninterruptibly (waitingDurationSecs , TimeUnit .SECONDS );
1016- DescribeTableResponse response =
1017- client .describeTable (
1018- DescribeTableRequest .builder ()
1019- .tableName (getFullTableName (namespace , table ))
1020- .build ());
1021- for (GlobalSecondaryIndexDescription globalSecondaryIndex :
1022- response .table ().globalSecondaryIndexes ()) {
1023- if (globalSecondaryIndex .indexName ().equals (indexName )) {
1024- if (globalSecondaryIndex .indexStatus () == IndexStatus .ACTIVE ) {
1025- return ;
1026- }
990+ if (!(ifNotExists && indexExists (namespace , table , columnName ))) {
991+ client .updateTable (
992+ UpdateTableRequest .builder ()
993+ .tableName (getFullTableName (namespace , table ))
994+ .attributeDefinitions (
995+ AttributeDefinition .builder ()
996+ .attributeName (columnName )
997+ .attributeType (SECONDARY_INDEX_DATATYPE_MAP .get (dataType ))
998+ .build ())
999+ .globalSecondaryIndexUpdates (
1000+ GlobalSecondaryIndexUpdate .builder ()
1001+ .create (
1002+ CreateGlobalSecondaryIndexAction .builder ()
1003+ .indexName (getGlobalIndexName (namespace , table , columnName ))
1004+ .keySchema (
1005+ KeySchemaElement .builder ()
1006+ .attributeName (columnName )
1007+ .keyType (KeyType .HASH )
1008+ .build ())
1009+ .projection (
1010+ Projection .builder ().projectionType (ProjectionType .ALL ).build ())
1011+ .provisionedThroughput (
1012+ ProvisionedThroughput .builder ()
1013+ .readCapacityUnits (ru )
1014+ .writeCapacityUnits (ru )
1015+ .build ())
1016+ .build ())
1017+ .build ())
1018+ .build ());
1019+ waitForIndexCreation (namespace , table , columnName );
1020+ }
1021+ } catch (Exception e ) {
1022+ throw new ExecutionException (
1023+ String .format (
1024+ "Creating the secondary index for the %s column of the %s table failed" ,
1025+ columnName , getFullTableName (namespace , table )),
1026+ e );
1027+ }
1028+ }
1029+
1030+ private boolean indexExists (Namespace namespace , String table , String columnName )
1031+ throws ExecutionException {
1032+ String indexName = getGlobalIndexName (namespace , table , columnName );
1033+ try {
1034+ DescribeTableResponse response =
1035+ client .describeTable (
1036+ DescribeTableRequest .builder ().tableName (getFullTableName (namespace , table )).build ());
1037+ for (GlobalSecondaryIndexDescription globalSecondaryIndex :
1038+ response .table ().globalSecondaryIndexes ()) {
1039+ if (globalSecondaryIndex .indexName ().equals (indexName )) {
1040+ if (globalSecondaryIndex .indexStatus () == IndexStatus .ACTIVE ) {
1041+ return true ;
10271042 }
10281043 }
10291044 }
1045+ return false ;
1046+ } catch (Exception e ) {
1047+ throw new ExecutionException (
1048+ String .format (
1049+ "Checking the secondary index existence for the %s column of the %s table failed" ,
1050+ columnName , getFullTableName (namespace , table )),
1051+ e );
1052+ }
1053+ }
1054+
1055+ private void waitForIndexCreation (Namespace namespace , String table , String columnName )
1056+ throws ExecutionException {
1057+ try {
1058+ do {
1059+ Uninterruptibles .sleepUninterruptibly (waitingDurationSecs , TimeUnit .SECONDS );
1060+ } while (!indexExists (namespace , table , columnName ));
10301061 } catch (Exception e ) {
10311062 throw new ExecutionException (
10321063 String .format (
0 commit comments