一、通過hash key和range key
import gzip
from pysnap import settings
from pysnap.aws import dynamodb
class DynamodbSearch():
@staticmethod
def search(harsh_key, range_key):
table = settings.aws_service_7478.get_resource('dynamodb').Table('patent_description')
key = {'patent_id': harsh_key, 'lang': range_key}
item = dynamodb.get_item(table, key)
title = item.get('description').__str__()
print(DynamodbSearch.gz2str(title))
@staticmethod
def gz2str(data):
flat = gzip.decompress(data)
return flat.decode('utf-8')
if __name__ == '__main__':
key_one = '81b488fb-e579-452e-bb68-d1bd0586cc6f'
key_two = 'EN'
DynamodbSearch.search(key_one, key_two)
二、僅通過hash key和range key中的hash key
from pysnap import settings
from boto3.dynamodb.conditions import Key
table = settings.aws_service_7478.get_resource('dynamodb').Table('bio_patent_sequence_v1')
item = table.query(KeyConditionExpression=Key('patent_id').eq(pid))
三、通過屬性
from pysnap import settings
from pysnap.aws import dynamodb
table = settings.aws_service_7478.get_resource('dynamodb').Table('patent')
conditions = {'pn': {'AttributeValueList': [pn], 'ComparisonOperator': 'EQ'}}
rs = dynamodb.query_with_index(table, 'pn-index', key_conditions=conditions)
四、sequence_value壓縮與解壓
掃表得到json文件,內容是字符串類型,對sequence_value的值進行一系列轉換得到能入庫的二進制內容
from pysnap import compress
gzip_value = compress.str2gz(compress.gz2str(base64.decodebytes(seq_value.encode('utf-8'))))