Created
October 24, 2020 16:11
-
-
Save thanakijwanavit/ebb24adea0107c02bb72939f6821dea3 to your computer and use it in GitHub Desktop.
pynamodbSample.py
This file contains hidden or 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
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Requirement already satisfied: pynamodb-dax in /home/ec2-user/anaconda3/envs/JupyterSystemEnv/lib/python3.6/site-packages (0.0.4)\n", | |
"Requirement already satisfied: botocore>=1.12.54 in /home/ec2-user/anaconda3/envs/JupyterSystemEnv/lib/python3.6/site-packages (from pynamodb-dax) (1.18.8)\n", | |
"Requirement already satisfied: amazon-dax-client>=1.1.7 in /home/ec2-user/anaconda3/envs/JupyterSystemEnv/lib/python3.6/site-packages (from pynamodb-dax) (1.1.7)\n", | |
"Requirement already satisfied: python-dateutil<3.0.0,>=2.1 in /home/ec2-user/anaconda3/envs/JupyterSystemEnv/lib/python3.6/site-packages (from pynamodb-dax) (2.8.1)\n", | |
"Requirement already satisfied: jmespath<1.0.0,>=0.7.1 in /home/ec2-user/anaconda3/envs/JupyterSystemEnv/lib/python3.6/site-packages (from botocore>=1.12.54->pynamodb-dax) (0.10.0)\n", | |
"Requirement already satisfied: urllib3<1.26,>=1.20 in /home/ec2-user/anaconda3/envs/JupyterSystemEnv/lib/python3.6/site-packages (from botocore>=1.12.54->pynamodb-dax) (1.22)\n", | |
"Requirement already satisfied: six~=1.11 in /home/ec2-user/anaconda3/envs/JupyterSystemEnv/lib/python3.6/site-packages (from amazon-dax-client>=1.1.7->pynamodb-dax) (1.15.0)\n", | |
"Requirement already satisfied: antlr4-python3-runtime==4.7.2; python_version >= \"3.0\" in /home/ec2-user/anaconda3/envs/JupyterSystemEnv/lib/python3.6/site-packages (from amazon-dax-client>=1.1.7->pynamodb-dax) (4.7.2)\n", | |
"\u001b[33mWARNING: You are using pip version 20.2.3; however, version 20.2.4 is available.\n", | |
"You should consider upgrading via the '/home/ec2-user/anaconda3/envs/JupyterSystemEnv/bin/python3.6 -m pip install --upgrade pip' command.\u001b[0m\n" | |
] | |
} | |
], | |
"source": [ | |
"!pip install pynamodb-dax" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"tableName = 'dynamodbDemoVilla-LogTable-1U6Z3WLB40Q82'" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from pynamodb.models import Model\n", | |
"from pynamodb.attributes import (\n", | |
" UnicodeAttribute, NumberAttribute, UnicodeSetAttribute, UTCDateTimeAttribute, JSONAttribute\n", | |
")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 14, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"class Database(Model):\n", | |
" class Meta:\n", | |
" table_name = tableName\n", | |
" region = 'ap-southeast-1'\n", | |
" transaction_id = UnicodeAttribute(hash_key=True)\n", | |
" datetime = NumberAttribute(range_key = True)\n", | |
" data = JSONAttribute()\n", | |
" @classmethod\n", | |
" def fromDict(cls, inputDict):\n", | |
" return cls(\n", | |
" transaction_id = inputDict['transaction_id'],\n", | |
" datetime = datetime.now().timestamp\n", | |
" data = inputDict\n", | |
" )" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from pynamodb.indexes import GlobalSecondaryIndex, AllProjection\n", | |
"from pynamodb.attributes import NumberAttribute\n", | |
"\n", | |
"\n", | |
"class DateIndex(GlobalSecondaryIndex):\n", | |
" \"\"\"\n", | |
" This class represents a global secondary index\n", | |
" \"\"\"\n", | |
" class Meta:\n", | |
" index_name = 'foo-index'\n", | |
" read_capacity_units = 1\n", | |
" write_capacity_units = 1\n", | |
" projection = AllProjection()\n", | |
"\n", | |
" # This attribute is the hash key for the index\n", | |
" # Note that this attribute must also exist\n", | |
" # in the model\n", | |
" view = NumberAttribute(default=0, hash_key=True)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 57, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"digioInputGetNotifyTransaction = {\n", | |
" \"merchant_id\": \"32434\",\n", | |
" \"merchant_name\": \"kdsjfljsldjf\",\n", | |
" \"transaction_id\": \"qfeef\",\n", | |
" \"amount\": \"qefqf\",\n", | |
" \"transaction_type\": \"feqfe\",\n", | |
" \"consumer_name\": \"fewqfq\",\n", | |
" \"mobile\": \"qfefeq\",\n", | |
" \"terminal_id\": \"qfewqf\",\n", | |
" \"transaction_date_time\": \"fqef\"\n", | |
"}" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 58, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"dynamodbDemoVilla-LogTable-1U6Z3WLB40Q82<qfeef>" | |
] | |
}, | |
"execution_count": 58, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"instance = Database.fromDict(digioInputGetNotifyTransaction)\n", | |
"instance" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 59, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"{'ConsumedCapacity': {'CapacityUnits': 1.0,\n", | |
" 'TableName': 'dynamodbDemoVilla-LogTable-1U6Z3WLB40Q82'}}" | |
] | |
}, | |
"execution_count": 59, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"instance.save()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 60, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"CPU times: user 3.08 ms, sys: 0 ns, total: 3.08 ms\n", | |
"Wall time: 5.93 ms\n" | |
] | |
} | |
], | |
"source": [ | |
"%%time\n", | |
"result = list(Database.query(\"qfeef\"))[0]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 47, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"{'merchant_id': '32434',\n", | |
" 'merchant_name': 'kdsjfljsldjf',\n", | |
" 'transaction_id': 'qfeef',\n", | |
" 'amount': 'qefqf',\n", | |
" 'transaction_type': 'feqfe',\n", | |
" 'consumer_name': 'fewqfq',\n", | |
" 'mobile': 'qfefeq',\n", | |
" 'terminal_id': 'qfewqf',\n", | |
" 'transaction_date_time': 'fqef'}" | |
] | |
}, | |
"execution_count": 47, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"result.data" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 48, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'qfeef'" | |
] | |
}, | |
"execution_count": 48, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"result.transaction_id" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 49, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"{'ConsumedCapacity': {'CapacityUnits': 1.0,\n", | |
" 'TableName': 'dynamodbDemoVilla-LogTable-1U6Z3WLB40Q82'}}" | |
] | |
}, | |
"execution_count": 49, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"result.delete()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 50, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"\u001b[0;31mInit signature:\u001b[0m\n", | |
"\u001b[0mDatabase\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\u001b[0m\n", | |
"\u001b[0;34m\u001b[0m \u001b[0mhash_key\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mUnion\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mAny\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mNoneType\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", | |
"\u001b[0;34m\u001b[0m \u001b[0mrange_key\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mUnion\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mAny\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mNoneType\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", | |
"\u001b[0;34m\u001b[0m \u001b[0m_user_instantiated\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mbool\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mTrue\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", | |
"\u001b[0;34m\u001b[0m \u001b[0;34m**\u001b[0m\u001b[0mattributes\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mAny\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\n", | |
"\u001b[0;34m\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m->\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", | |
"\u001b[0;31mDocstring:\u001b[0m \n", | |
"Defines a `PynamoDB` Model\n", | |
"\n", | |
"This model is backed by a table in DynamoDB.\n", | |
"You can create the table by with the ``create_table`` method.\n", | |
"\u001b[0;31mInit docstring:\u001b[0m\n", | |
":param hash_key: Required. The hash key for this object.\n", | |
":param range_key: Only required if the table has a range key attribute.\n", | |
":param attrs: A dictionary of attributes to set on this object.\n", | |
"\u001b[0;31mFile:\u001b[0m ~/anaconda3/envs/python38/lib/python3.8/site-packages/pynamodb/models.py\n", | |
"\u001b[0;31mType:\u001b[0m MetaModel\n", | |
"\u001b[0;31mSubclasses:\u001b[0m \n" | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"Database?" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 51, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"['DoesNotExist',\n", | |
" 'Meta',\n", | |
" '__annotations__',\n", | |
" '__class__',\n", | |
" '__delattr__',\n", | |
" '__dict__',\n", | |
" '__dir__',\n", | |
" '__doc__',\n", | |
" '__eq__',\n", | |
" '__format__',\n", | |
" '__ge__',\n", | |
" '__getattribute__',\n", | |
" '__gt__',\n", | |
" '__hash__',\n", | |
" '__init__',\n", | |
" '__init_subclass__',\n", | |
" '__le__',\n", | |
" '__lt__',\n", | |
" '__module__',\n", | |
" '__ne__',\n", | |
" '__new__',\n", | |
" '__reduce__',\n", | |
" '__reduce_ex__',\n", | |
" '__repr__',\n", | |
" '__setattr__',\n", | |
" '__sizeof__',\n", | |
" '__str__',\n", | |
" '__subclasshook__',\n", | |
" '__weakref__',\n", | |
" '_attributes',\n", | |
" '_batch_get_page',\n", | |
" '_connection',\n", | |
" '_deserialize',\n", | |
" '_dynamo_to_python_attr',\n", | |
" '_dynamo_to_python_attrs',\n", | |
" '_from_data',\n", | |
" '_get_attributes',\n", | |
" '_get_connection',\n", | |
" '_get_indexes',\n", | |
" '_get_json',\n", | |
" '_get_keys',\n", | |
" '_get_save_args',\n", | |
" '_get_schema',\n", | |
" '_handle_version_attribute',\n", | |
" '_hash_key_attribute',\n", | |
" '_hash_keyname',\n", | |
" '_index_classes',\n", | |
" '_indexes',\n", | |
" '_range_key_attribute',\n", | |
" '_range_keyname',\n", | |
" '_serialize',\n", | |
" '_serialize_keys',\n", | |
" '_serialize_value',\n", | |
" '_set_attributes',\n", | |
" '_set_defaults',\n", | |
" '_ttl_attribute',\n", | |
" '_version_attribute_name',\n", | |
" 'batch_get',\n", | |
" 'batch_write',\n", | |
" 'count',\n", | |
" 'create_table',\n", | |
" 'data',\n", | |
" 'delete',\n", | |
" 'delete_table',\n", | |
" 'describe_table',\n", | |
" 'dump',\n", | |
" 'dumps',\n", | |
" 'exists',\n", | |
" 'fromDict',\n", | |
" 'from_raw_data',\n", | |
" 'get',\n", | |
" 'get_attributes',\n", | |
" 'get_operation_kwargs_from_class',\n", | |
" 'get_operation_kwargs_from_instance',\n", | |
" 'load',\n", | |
" 'loads',\n", | |
" 'query',\n", | |
" 'refresh',\n", | |
" 'save',\n", | |
" 'scan',\n", | |
" 'transaction_id',\n", | |
" 'update',\n", | |
" 'update_local_version_attribute',\n", | |
" 'update_ttl']" | |
] | |
}, | |
"execution_count": 51, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"dir(Database)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 54, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"CPU times: user 2.97 ms, sys: 0 ns, total: 2.97 ms\n", | |
"Wall time: 9.43 ms\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"[dynamodbDemoVilla-LogTable-1U6Z3WLB40Q82<qfeejhkhkf>]" | |
] | |
}, | |
"execution_count": 54, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"%%time\n", | |
"list(Database.scan(limit=10))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "python38", | |
"language": "python", | |
"name": "python38" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.8.6" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment