Example #1
0
def createGamesTable(db):

    try:
        hostStatusDate = GlobalAllIndex("hostStatusDate", parts=[HashKey("HostId"),
                                                                RangeKey("StatusDate")])
        opponentStatusDate  = GlobalAllIndex("opponentStatusDate", parts=[HashKey("OpponentId"),
                                                                RangeKey("StatusDate")]) 

        #global secondary indexes
        GSI = [hostStatusDate, opponentStatusDate]

        gamesTable = Table.create("Games",
                    schema=[HashKey("GameId")],
                    throughput={
                        'read':1,
                        'write':1
                    },
                    global_indexes=GSI,
                    connection=db)

    except JSONResponseError, jre:
        try:
            gamesTable = Table("Games", connection=db)
        except Exception, e:
            print "Games Table doesn't exist."
Example #2
0
 def create(self):
     Table.create(self.table_name,
                  schema=[
                      HashKey(Tweet.tweet_user_id),
                      RangeKey(Tweet.tweet_id),
                  ],
                  throughput=standard_throughput,
                  indexes={
                      AllIndex(self.index_timestamp,
                               parts=[
                                   HashKey(Tweet.tweet_user_id),
                                   RangeKey(Tweet.ts_ms)
                               ])
                  },
                  global_indexes={
                      GlobalAllIndex(self.index_site,
                                     parts=[
                                         HashKey(keys.entity_site),
                                         RangeKey(Tweet.tweet_id)
                                     ],
                                     throughput=standard_throughput),
                      GlobalAllIndex(self.index_league,
                                     parts=[
                                         HashKey(keys.entity_league),
                                         RangeKey(Tweet.tweet_id)
                                     ],
                                     throughput=standard_throughput),
                      GlobalAllIndex(self.index_team,
                                     parts=[
                                         HashKey(keys.entity_team),
                                         RangeKey(Tweet.tweet_id)
                                     ],
                                     throughput=standard_throughput)
                  })
Example #3
0
    def __init__(self, connection, debug=False):
        super(DatabaseManager, self).__init__()

        self.connection = connection

        tables_list = self.connection.list_tables()

        if TABLE_NAME in tables_list.get("TableNames"):
            self.table = Table(table_name=TABLE_NAME,
                               connection=self.connection)
        else:
            self.table = Table.create(TABLE_NAME, [
                HashKey("entry_type"),
                RangeKey("date_created", data_type=NUMBER)
            ],
                                      indexes=[
                                          AllIndex("DateJoinedIndex",
                                                   parts=[
                                                       HashKey("entry_type"),
                                                       RangeKey(
                                                           "date_created",
                                                           data_type=NUMBER)
                                                   ])
                                      ],
                                      connection=self.connection)
Example #4
0
    def _get_scheduled_jobs(self, dynamodb_connection):  # noqa
        """
        WARNING -- this method requires cleanup; the user must remember to
        delete the table once complete.  For example:

        >>> NEW_JOB = {'log_version': 'ad_click', 'log_schema_version': '1'}
        >>> def cool_test_fn(dynamodb_connection):
        >>>     tsj = TestScheduledJobs()
        >>>     table, scheduled_jobs = tsj._get_scheduled_jobs(dynamodb_connection)
        >>>     assert scheduled_jobs.put(**NEW_JOB)
        >>>     yield scheduled_jobs
        >>>     assert table.delete()  # THIS IS THE KEY CLEANUP!!

        """
        avro_schema = get_avro_schema('mycroft/avro/scheduled_jobs.json')
        index_load_status = GlobalAllIndex(
            ScheduledJobs.INDEX_LOAD_STATUS,
            parts=[HashKey('load_status')])
        index_et_status = GlobalAllIndex(
            ScheduledJobs.INDEX_ET_STATUS,
            parts=[HashKey('et_status')])
        index_load_status = GlobalAllIndex(
            ScheduledJobs.INDEX_LOAD_STATUS,
            parts=[HashKey('load_status')])
        index_log_name_and_log_schema_version = GlobalAllIndex(
            ScheduledJobs.INDEX_LOG_NAME_AND_LOG_SCHEMA_VERSION,
            parts=[HashKey('log_name'), RangeKey('log_schema_version')])
        table = Table.create(
            'ScheduledJobs',
            schema=NAME_TO_SCHEMA['scheduled_jobs'],
            connection=dynamodb_connection,
            global_indexes=[index_et_status, index_load_status,
                            index_log_name_and_log_schema_version])
        return table, ScheduledJobs(persistence_object=table, avro_schema_object=avro_schema)
Example #5
0
def createDynamoObject():
    try:
        users = Table.create(
            'data',
            schema=[HashKey('id')],
            global_indexes=[
                GlobalAllIndex('EverythingIndex', parts=[HashKey('name')])
            ],
            connection=boto.dynamodb2.connect_to_region('us-west-2'))
    except boto.exception.JSONResponseError:
        users = Table('data',
                      connection=boto.dynamodb2.connect_to_region('us-west-2'))
        print "1) Table 'data' already created."
    #On first Run this wont insert data because of delay to create table on aws server side.
    try:
        users.put_item(
            data={
                'id': '3',
                'type': 'person',
                'name': 'dummy',
                'activities': ['activity one'],
            })
    except:
        print "2) Dummy Data already added."
    return users
Example #6
0
def create_router_table(tablename="router",
                        read_throughput=5,
                        write_throughput=5):
    # type: (str, int, int) -> Table
    """Create a new router table

    The last_connect index is a value used to determine the last month a user
    was seen in. To prevent hot-keys on this table during month switchovers the
    key is determined based on the following scheme:

        (YEAR)(MONTH)(DAY)(HOUR)(0001-0010)

    Note that the random key is only between 1-10 at the moment, if the key is
    still too hot during production the random range can be increased at the
    cost of additional queries during GC to locate expired users.

    """
    return Table.create(
        tablename,
        schema=[HashKey("uaid")],
        throughput=dict(read=read_throughput, write=write_throughput),
        global_indexes=[
            GlobalKeysOnlyIndex(
                'AccessIndex',
                parts=[HashKey('last_connect', data_type=NUMBER)],
                throughput=dict(read=5, write=5))
        ],
    )
Example #7
0
    def test_gsi(self):
        users = Table.create('gsi_users',
                             schema=[
                                 HashKey('user_id'),
                             ],
                             throughput={
                                 'read': 5,
                                 'write': 3,
                             },
                             global_indexes=[
                                 GlobalKeysOnlyIndex(
                                     'StuffIndex',
                                     parts=[HashKey('user_id')],
                                     throughput={
                                         'read': 2,
                                         'write': 1,
                                     }),
                             ])
        self.addCleanup(users.delete)

        # Wait for it.
        time.sleep(60)

        users.update(throughput={
            'read': 3,
            'write': 4
        },
                     global_indexes={'StuffIndex': {
                         'read': 1,
                         'write': 2
                     }})

        # Wait again for the changes to finish propagating.
        time.sleep(150)
 def tableCreateKwargs(self):
     return dict(
         schema=[
             HashKey("instance_id"),
             RangeKey("date_hour"),
         ],
         throughput={
             "read": (taurus.engine.config.getint(
                 "dynamodb", "instance_data_hourly_throughput_read")),
             "write": (taurus.engine.config.getint(
                 "dynamodb", "instance_data_hourly_throughput_write"))
         },
         global_indexes=[
             GlobalAllIndex("taurus.instance_data_hourly-date_hour_index",
                            parts=[HashKey("date"),
                                   RangeKey("hour")],
                            throughput={
                                "read":
                                taurus.engine.config.getint(
                                    "dynamodb",
                                    "instance_data_hourly_throughput_read"),
                                "write":
                                taurus.engine.config.getint(
                                    "dynamodb",
                                    "instance_data_hourly_throughput_write")
                            })
         ])
Example #9
0
    def test_settings(self):
        self.assertEqual(len(self.app.config['DYNAMO_TABLES']), 2)
        self.assertEqual(self.app.config['AWS_ACCESS_KEY_ID'],
                         environ.get('AWS_ACCESS_KEY_ID'))
        self.assertEqual(self.app.config['AWS_SECRET_ACCESS_KEY'],
                         environ.get('AWS_SECRET_ACCESS_KEY'))
        self.assertEqual(
            self.app.config['AWS_REGION'],
            environ.get('AWS_REGION') or self.dynamo.DEFAULT_REGION)

        # Test DynamoDB local settings.
        app = Flask(__name__)
        app.config['DEBUG'] = True
        app.config['DYNAMO_TABLES'] = [
            Table('%s-phones' % self.prefix, schema=[HashKey('number')]),
            Table('%s-users' % self.prefix, schema=[HashKey('username')]),
        ]
        app.config['DYNAMO_ENABLE_LOCAL'] = True

        self.assertRaises(ConfigurationError, Dynamo, app)

        app.config['DYNAMO_LOCAL_HOST'] = 'localhost'

        self.assertRaises(ConfigurationError, Dynamo, app)

        app.config['DYNAMO_LOCAL_PORT'] = 8000
        self.assertIsInstance(Dynamo(app), object)
    def createFreshTable(self):
        """
        Create a fresh empty distance table.
        """

        # delete existing table if it exists
        try:
            self.__getTable().delete()
            time.sleep(10)
        except:
            pass
            # do nothing. Maybe there was no existing table

        # create new table
        tableConnectionParams = parseConnectionString(self.tableConnString)
        return Table.create(tableConnectionParams['name'],
                            schema=[HashKey('from'),
                                    RangeKey('to')],
                            throughput={
                                'read': 1,
                                'write': 2,
                            },
                            global_indexes=[
                                GlobalAllIndex(
                                    'reverseIndex',
                                    parts=[HashKey('to'),
                                           RangeKey('from')],
                                    throughput={
                                        'read': 1,
                                        'write': 2,
                                    })
                            ],
                            connection=getDbConnection(tableConnectionParams))
Example #11
0
def createDynamoObject(name):
    try:
        users = Table.create(name, schema=[HashKey('id')],
                             throughput={'read': db_read_cap,
                             'write': db_write_cap},
                             global_indexes=[GlobalAllIndex('EverythingIndex'
                             , parts=[HashKey('name')])],
                             connection=boto.dynamodb2.connect_to_region(AWS_REGION))
    except:
        users = Table(name,
                      connection=boto.dynamodb2.connect_to_region('us-west-2'
                      ))
        print "1) Table 'data' already created for table: " + name

  # On first Run this wont insert data because of delay to create table on aws server side.

    try:
        users.put_item(data={
            'id': '3',
            'type': 'person',
            'name': 'dummy',
            'activities': ['activity one'],
            })
    except:
        print '2) Dummy Data already added for tabe: ' + name
    return users
    def _create_table(self, table_name):
        """ Create a table and return the table reference """
        hash_key = self.hash_key()
        range_key = self.range_key()

        if range_key:
            self.logger.info(
                "Creating table with hash key: {}, range key: {}".format(
                    hash_key, range_key))
            schema = [HashKey(hash_key), RangeKey(range_key)]
        else:
            self.logger.info(
                "Creating table with hash key: {}".format(hash_key))
            schema = [HashKey(hash_key)]

        new_table = Table.create(table_name,
                                 schema=schema,
                                 connection=self._conn)

        # Wait for our new table to be created
        status = 'CREATING'
        while status == 'CREATING':
            sleep(0.5)
            status = self._get_table_status(new_table)
            self.logger.debug("Table status is {}".format(status))

        return new_table
    def test_sanity_test_table_task(self, mock_config):
        mock_config.get_config.return_value.get.return_value = AWS_ACCESS_KEY
        t = TestSanityTestDynamoDBTableTask()

        # mock s3 location for writing output token
        s3_client = S3Client(AWS_ACCESS_KEY, AWS_SECRET_KEY)
        s3_client.s3.create_bucket('mybucket')

        # create table
        table_name = 'dynamo_table1'
        schema = [HashKey('my_hash', data_type=STRING)]
        indexes = [
            AllIndex('IndexName',
                     parts=[
                         HashKey('my_hash', data_type=STRING),
                         RangeKey('range_index', data_type=NUMBER)
                     ])
        ]
        throughput = {'read': 2, 'write': 4}
        client = DynamoDBClient(aws_access_key_id=AWS_ACCESS_KEY,
                                aws_secret_access_key=AWS_SECRET_KEY)
        client.create_table(table_name, schema, throughput, indexes=indexes)

        self.assertRaises(DynamoDBTaskException,
                          luigi.build([t], local_scheduler=True))
Example #14
0
    def createTable(isLocal, localPort):
        """Used to create table for Dyanmo DB"""
        SessionTable.LOCAL_PORT = localPort
        secondaryIndex = [
            GlobalAllIndex('expiration-index',
                           parts=[HashKey('expiration', data_type=NUMBER)],
                           throughput={
                               'read': 5,
                               'write': 5
                           })
        ]
        if isLocal:
            try:
                Table.create(SessionTable.TABLE_NAME,
                             schema=[HashKey(SessionTable.KEY_NAME)],
                             global_indexes=secondaryIndex,
                             connection=SessionTable.getLocalConnection())
            except exceptions.JSONResponseError as jre:
                if jre.status == 400 and "preexisting" in jre.message.lower():
                    #table already exists
                    pass

        else:
            Table.create(SessionTable.TABLE_NAME,
                         schema=[HashKey(SessionTable.KEY_NAME)],
                         global_indexes=secondaryIndex)
Example #15
0
    def test_gsi_with_just_hash_key(self):
        # GSI allows for querying off of different keys. This is behavior we
        # previously disallowed (due to standard & LSI queries).
        # See https://forums.aws.amazon.com/thread.jspa?threadID=146212&tstart=0
        users = Table.create('gsi_query_users',
                             schema=[HashKey('user_id')],
                             throughput={
                                 'read': 5,
                                 'write': 3,
                             },
                             global_indexes=[
                                 GlobalIncludeIndex(
                                     'UsernameIndex',
                                     parts=[
                                         HashKey('username'),
                                     ],
                                     includes=['user_id', 'username'],
                                     throughput={
                                         'read': 3,
                                         'write': 1,
                                     })
                             ])
        self.addCleanup(users.delete)

        # Wait for it.
        time.sleep(60)

        users.put_item(
            data={
                'user_id': '7',
                'username': '******',
                'first_name': 'John',
                'last_name': 'Doe',
            })
        users.put_item(
            data={
                'user_id': '24',
                'username': '******',
                'first_name': 'Alice',
                'last_name': 'Expert',
            })
        users.put_item(
            data={
                'user_id': '35',
                'username': '******',
                'first_name': 'Jane',
                'last_name': 'Doe',
            })

        # Try the main key. Should be fine.
        rs = users.query_2(user_id__eq='24')
        results = sorted([user['username'] for user in rs])
        self.assertEqual(results, ['alice'])

        # Now try the GSI. Also should work.
        rs = users.query_2(username__eq='johndoe', index='UsernameIndex')
        results = sorted([user['username'] for user in rs])
        self.assertEqual(results, ['johndoe'])
Example #16
0
def Init():
    """
    Connect to DynamoDB Local. If you want to connect to the real DynamoDB set the 'local'
    variable below to Fals, but make sure either you have a .boto file or you
    pass both the aws_access_key_id and aws_secret_access_key parameters to the create
    (this code fetches them from settings.cfg).
    """
    local = True

    if local:
        # Connect to local DynamoDB server. Make sure you have that running first.
        conn = DynamoDBConnection(host='localhost',
                                  port=8001,
                                  aws_secret_access_key='anything',
                                  is_secure=False)
    else:
        # Connect to the real DynamoDB.
        config = ConfigParser.RawConfigParser()
        config.read("settings.cfg")
        id = config.get('DynamoDB', 'aws_access_key_id')
        key = config.get('DynamoDB', 'aws_secret_access_key')

        conn = boto.dynamodb2.connect_to_region('us-west-2',
                                                aws_access_key_id=id,
                                                aws_secret_access_key=key)

    # Get a list of all tables from DynamoDB.
    tables = conn.list_tables()
    #print "Tables:", tables
    """
    If there isn't an employees table then create it. The table has a primary key of the
    employee type and id, allowing you to query them. It has a secondary index on the 
    employee type and title, allowing you to query them as well.
    """

    if 'employees' not in tables['TableNames']:
        # Create table of employees
        print "Creating new table"
        employees = Table.create(
            'employees',
            schema=[HashKey('etype'), RangeKey('id')],
            indexes=[
                AllIndex('TitleIndex',
                         parts=[HashKey('etype'),
                                RangeKey('title')])
            ],
            connection=conn)
        # Wait for table to be created (DynamoDB has eventual consistency)
        while True:
            time.sleep(5)
            try:
                conn.describe_table('employees')
            except Exception, e:
                print e
            else:
                break
Example #17
0
def get_table():
  return Table(table_name, schema=[
    HashKey('CreatedAt'),
    RangeKey('Count'),
  ], global_indexes=[
     GlobalAllIndex('CountsIndex', parts=[
      HashKey('EventType',data_type=STRING),
      RangeKey('Timestamp',data_type=STRING)
  ])
  ])
def get_schema_param(hash_key_name, hash_key_type, range_key_name, range_key_type):
    if range_key_name:
        schema = [
            HashKey(hash_key_name, DYNAMO_TYPE_MAP.get(hash_key_type, DYNAMO_TYPE_MAP[DYNAMO_TYPE_DEFAULT])),
            RangeKey(range_key_name, DYNAMO_TYPE_MAP.get(range_key_type, DYNAMO_TYPE_MAP[DYNAMO_TYPE_DEFAULT]))
        ]
    else:
        schema = [
            HashKey(hash_key_name, DYNAMO_TYPE_MAP.get(hash_key_type, DYNAMO_TYPE_MAP[DYNAMO_TYPE_DEFAULT]))
        ]
    return schema
Example #19
0
    def init_db(self):
        tables = {}
        clients_schema = [HashKey('clientId')]  # ClientId can be hostname (if unique)
        status_schema = [HashKey('clientId'), RangeKey('gsize')]
        graphs_schema = [HashKey('gsize'), RangeKey('clientId')]
        tables['clients'] = clients_schema
        tables['status'] = status_schema
        tables['graphs'] = graphs_schema

        for name in tables.keys():
            self.create_table(name, schema=tables[name])
Example #20
0
def create_or_update_dynamo_table(connection, module):
    table_name = module.params.get('name')
    hash_key_name = module.params.get('hash_key_name')
    hash_key_type = module.params.get('hash_key_type')
    range_key_name = module.params.get('range_key_name')
    range_key_type = module.params.get('range_key_type')
    read_capacity = module.params.get('read_capacity')
    write_capacity = module.params.get('write_capacity')

    if range_key_name:
        schema = [
            HashKey(hash_key_name, DYNAMO_TYPE_MAP.get(hash_key_type)),
            RangeKey(range_key_name, DYNAMO_TYPE_MAP.get(range_key_type))
        ]
    else:
        schema = [HashKey(hash_key_name, DYNAMO_TYPE_MAP.get(hash_key_type))]
    throughput = {'read': read_capacity, 'write': write_capacity}

    result = dict(
        region=module.params.get('region'),
        table_name=table_name,
        hash_key_name=hash_key_name,
        hash_key_type=hash_key_type,
        range_key_name=range_key_name,
        range_key_type=range_key_type,
        read_capacity=read_capacity,
        write_capacity=write_capacity,
    )

    try:
        table = Table(table_name, connection=connection)

        if dynamo_table_exists(table):
            result['changed'] = update_dynamo_table(
                table, throughput=throughput, check_mode=module.check_mode)
        else:
            if not module.check_mode:
                Table.create(table_name,
                             connection=connection,
                             schema=schema,
                             throughput=throughput)
            result['changed'] = True

        if not module.check_mode:
            result['table_status'] = table.describe()['Table']['TableStatus']

    except BotoServerError:
        result[
            'msg'] = 'Failed to create/update dynamo table due to error: ' + traceback.format_exc(
            )
        module.fail_json(**result)
    else:
        module.exit_json(**result)
Example #21
0
class RacingCrewCollection(DBTable):
    table_name = 'racing_crew'
    item_class = Race
    global_indexes = [
        GlobalAllIndex('race-index', parts=[HashKey('race')]),
        GlobalAllIndex('event-index', parts=[HashKey('event')])
    ]

    def __init__(self, dbconn):
        super(RacingCrewCollection,
              self).__init__(RacingCrewCollection.table_name,
                             DBTable.simple_schema,
                             RacingCrewCollection.global_indexes, dbconn)
Example #22
0
    def __init__(self, db_conn, url_file):
        """Initialize the crawler with a connection to the database to populate
        and with the file containing the list of seed URLs to begin indexing."""
        self._url_queue = []
        self._doc_id_cache = {}
        self._word_id_cache = {}
        # Data structure for inverted index, lexicon and document index
        self._inverted_lexicon = {}
        self._inverted_index = {}
        self._document_index = {}
        self._page_rank_list = []
        self._page_rank = {}
        self._seen = set()
        self._sorted_resolved_inverted_index = {}
        # Initialize database for crawler
        self._lexicon_db = Table('lexicon',
                                 schema=[
                                     HashKey('word_id', data_type=NUMBER),
                                 ])
        self._document_index_db = Table('document_index',
                                        schema=[
                                            HashKey('doc_id',
                                                    data_type=NUMBER),
                                        ])
        self._inverted_index_db = Table('inverted_index',
                                        schema=[
                                            HashKey('word_id',
                                                    data_type=NUMBER),
                                        ])
        self._page_rank_db = Table('page_rank',
                                   schema=[
                                       HashKey('doc_id', data_type=NUMBER),
                                   ])
        self._sorted_resolved_inverted_index_db = Table(
            'sorted_resolved_inverted_index',
            schema=[
                HashKey('word', data_type=STRING),
            ])

        # TODO remove me in real version
        self._mock_next_doc_id = 1
        self._mock_next_word_id = 1

        # get all urls into the queue
        try:
            with open(url_file, 'r') as f:
                for line in f:
                    self._url_queue.append((self._fix_url(line.strip(),
                                                          ""), 0))
        except IOError:
            pass
Example #23
0
    def create(connection):
        """Create dynamodb tables.
        """
        Table.create(config.table_name('metric_names'),
                     schema=[HashKey('domain'), RangeKey('name')],
                     throughput=Schema.metric_names_tp, connection=connection)
        Table.create(config.table_name('tag_names'),
                     schema=[HashKey('domain'), RangeKey('name')],
                     throughput=Schema.tag_names_tp, connection=connection)
        Table.create(config.table_name('tag_values'),
                     schema=[HashKey('domain'), RangeKey('value')],
                     throughput=Schema.tag_values_tp, connection=connection)

        DatapointsSchema.create(connection)
Example #24
0
def sendtodynamo_cnn(cnnjson):
    ''' Send json to DynamoDB
  Assumes that article timestamps have been deduped to avoid collisions
  '''

    conn = connect_to_region('us-west-2',
                             aws_access_key_id=AWS_ACCESS_KEY_ID,
                             aws_secret_access_key=AWS_SECRET_ACCESS_KEY)

    hashkey = "CNN"  # primary key to be used for DynamoDB table

    try:
        table = Table('CNN', connection=conn)
        table.describe()
    except boto.exception.JSONResponseError:
        print "Creating table"
        table = Table.create(
            'CNN',
            schema=[HashKey('source'),
                    RangeKey('tstamp', data_type=NUMBER)],
            throughput={
                'read': 25,
                'write': 25
            },
            indexes=[
                GlobalAllIndex('showidx',
                               parts=[HashKey('show')],
                               throughput={
                                   'read': 10,
                                   'write': 5
                               })
            ])

    iteration = 0
    for article in cnnjson:
        # Iterate through list of articles and upload to table
        rangekey = float(article['timestamp'])
        rowdata = {
            'source': hashkey,
            'tstamp': rangekey,
            'cnnShow': article['show']
        }
        for key in article.keys():
            rowdata[key] = article[key]
        item = table.put_item(data=rowdata)
        iteration += 1
        if iteration % 100 == 0:
            print "Uploaded " + iteration + " articles"

    return None
Example #25
0
class Audit(DBTable):
    table_name = 'audit'
    schema = [HashKey('timeStamp'), RangeKey('user')]
    global_indexes = [GlobalAllIndex('race-index', parts=[HashKey('user')])]

    def __init__(self, dbconn):
        self.logger = logging.getLogger('audit')

        super(Audit, self).__init__(Audit.table_name, Audit.schema,
                                    Audit.global_indexes, dbconn)

    def info(self, message):
        self.logger.info(message)
        self.insert(data={'message': message, 'timestamp': time.time()})
 def __getMappingsTable(self):
   return getDbTableWithSchemaAndGlobalIndexes(
     self.docClusterMappingTable,
     [
       HashKey('clusterId'),
       RangeKey('docId')
     ],
     [
       GlobalAllIndex('docId-clusterId-index', parts=[
         HashKey('docId'),
         RangeKey('clusterId')
       ])
     ]
   )
Example #27
0
def create_router_table(tablename="router",
                        read_throughput=5,
                        write_throughput=5):
    """Create a new router table"""
    return Table.create(
        tablename,
        schema=[HashKey("uaid")],
        throughput=dict(read=read_throughput, write=write_throughput),
        global_indexes=[
            GlobalKeysOnlyIndex(
                'AccessIndex',
                parts=[HashKey('last_connect', data_type=NUMBER)],
                throughput=dict(read=5, write=5))
        ],
    )
Example #28
0
 def create(self):
     Table.create(self.table_name,
                  schema=[
                      HashKey(keys.entity_twitter_id),
                      RangeKey(time_keys.ts_add, data_type=NUMBER),
                  ],
                  throughput=standard_throughput)
Example #29
0
def createDdbTable(region="us-east-1", table="credential-store"):
    '''
    create the secret store table in DDB in the specified region
    '''
    d_conn = boto.dynamodb2.connect_to_region(region)
    if table in d_conn.list_tables()['TableNames']:
        print("Credential Store table already exists")
        return
    print("Creating table...")
    secrets = Table.create(table,
                           schema=[
                               HashKey('name', data_type=STRING),
                               RangeKey('version', data_type=STRING)
                           ],
                           throughput={
                               'read': 1,
                               'write': 1
                           },
                           connection=d_conn)
    timeout = 1
    while secrets.describe()['Table']['TableStatus'] != "ACTIVE":
        print("Waiting for table to be created...")
        time.sleep(timeout)
        timeout = timeout * 2 if timeout < 8 else timeout
    print("Table has been created. "
          "Go read the README about how to create your KMS key")
def test_failed_overwrite():
    table = Table.create('messages',
                         schema=[
                             HashKey('id'),
                         ],
                         throughput={
                             'read': 7,
                             'write': 3,
                         })

    data1 = {'id': '123', 'data': '678'}
    table.put_item(data=data1)

    data2 = {'id': '123', 'data': '345'}
    table.put_item(data=data2, overwrite=True)

    data3 = {'id': '123', 'data': '812'}
    table.put_item.when.called_with(
        data=data3).should.throw(ConditionalCheckFailedException)

    returned_item = table.lookup('123')
    dict(returned_item).should.equal(data2)

    data4 = {'id': '124', 'data': 812}
    table.put_item(data=data4)

    returned_item = table.lookup('124')
    dict(returned_item).should.equal(data4)
Example #31
0
    def create_dynamo_schema(self, connection, tablenames=None, test=False,
                             wait=False, throughput=None):
        """
        Create all Dynamo tables for this model

        Parameters
        ----------
        connection : :class:`~boto.dynamodb2.layer1.DynamoDBConnection`
        tablenames : list, optional
            List of tables that already exist. Will call 'describe' if not
            provided.
        test : bool, optional
            If True, don't actually create the table (default False)
        wait : bool, optional
            If True, block until table has been created (default False)
        throughput : dict, optional
            The throughput of the table and global indexes. Has the keys 'read'
            and 'write'. To specify throughput for global indexes, add the name
            of the index as a key and another 'read', 'write' dict as the
            value.

        Returns
        -------
        table : str
            Table name that was created, or None if nothing created

        """
        if self.abstract:
            return None
        if tablenames is None:
            tablenames = connection.list_tables()['TableNames']
        if self.ddb_tablename in tablenames:
            return None
        elif test:
            return self.ddb_tablename

        attrs = []
        indexes = []
        global_indexes = []
        hash_key = None
        raw_attrs = {}

        if throughput is not None:
            table_throughput = throughput
        else:
            table_throughput = self.throughput
        table_throughput = {
            'ReadCapacityUnits': table_throughput['read'],
            'WriteCapacityUnits': table_throughput['write'],
        }

        hash_key = HashKey(self.hash_key.name,
                           data_type=self.hash_key.ddb_data_type)
        schema = [hash_key.schema()]
        for name, field in self.fields.iteritems():
            if field.hash_key:
                f = hash_key
            elif field.range_key:
                f = RangeKey(name, data_type=field.ddb_data_type)
                schema.append(f.schema())
            elif field.index:
                idx = field.get_boto_index(hash_key)
                f = idx.parts[1]
                indexes.append(idx.schema())
            elif any(map(lambda x: name in x, self.global_indexes)):
                f = BaseSchemaField(name, data_type=field.ddb_data_type)
            else:
                continue
            attrs.append(f.definition())
            raw_attrs[name] = f

        for gindex in self.global_indexes:
            index = gindex.get_boto_index(self.fields)
            if throughput is not None and gindex.name in throughput:
                index.throughput = throughput[gindex.name]
            global_indexes.append(index.schema())

        # Make sure indexes & global indexes either have data or are None
        indexes = indexes or None
        global_indexes = global_indexes or None
        if not test:
            connection.create_table(
                attrs, self.ddb_tablename, schema, table_throughput,
                local_secondary_indexes=indexes,
                global_secondary_indexes=global_indexes)
            if wait:
                desc = connection.describe_table(self.ddb_tablename)
                while desc['Table']['TableStatus'] != 'ACTIVE':
                    time.sleep(1)
                    desc = connection.describe_table(self.ddb_tablename)

        return self.ddb_tablename