在Dynamo DB AWS中搜索列表数据类型 [英] Search in list data type in dynamo db aws

查看:106
本文介绍了在Dynamo DB AWS中搜索列表数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用dynamo db作为我们网站之一的数据库解决方案。我们按照下面给定的json将数据存储在dynamo db中。

We are using dynamo db as our database solution for one of our sites. we are storing data in dynamo db as per below given json.

我们有可以属于一种/多种类型的视频,因此我们选择了列表数据类型并已存储数据并使其成为GSI(全球二级索引)

We have video which can belong to one/many genres, so we have chosen list data type and have stored data into it and have made the genre as GSI (global secondary index)

我正面临几个问题。

1)当我将流派定义为索引时,aws仅提供三种数据类型(字符串,二进制,数字),不允许我们存储列表类型的数据。

1) When I define genre as index, aws provides only three data types (string, binary, number) not allowing us to store a list type data. It gives an unexpected data type error.

2)如果我未将其定义为索引,则不允许获取数据。 DynamoDB要求输入哈希键,在我的情况下这是不可能的,因为我正在获取不应该依赖哈希键(主键)的列表。

2) If I do not define it as index I am not allowed to fetch the data. DynamoDB asks for hash key, which is not possible in my case as I am fetching a listing that should not depend on a hash key(primary key).

{
  "description": "********",
  "genre": [
    "Kids",
    "Documentary"
  ],
  "language": "******",
  "status": "0",
  "thumb_url": "******",
  "title": "******",
  "uploaded_by": "****** ",
  "url": "******",
  "video_id": 1330051052
}

获取数据的代码

$DynamoDbClient = AWS::get('DynamoDb');
        $result = $DynamoDbClient->query(array(
            'TableName' => 'videos',
            'IndexName' => 'genre-index',
            'AttributesToGet' => array('video_id', 'language', 'description'),
            'KeyConditions' => array(
                // Key attribute
                // This is non-key attribute
                'genre' => array(
                    'ComparisonOperator' => 'EQ',
                    'AttributeValueList' => array(
                        array("S" => "Kids"),
                    )
                ),
            ),
        ));

在上面的代码中,我正在寻找儿童类型的视频。但是如果我不将流派声明为索引,它将返回空白并给出错误消息。同一视频可以属于多种流派。

In the above code I am looking for videos in Kids genre. but it returns blank and gives error if I don't declare genre as index. Same video can belong to multiple genre.

因此,无论如何,我可以在列表中进行搜索还是我使用的API不正确?总是很感激帮助。

So is there anyway that I can search inside a list OR am I not using API in a right way? Help is always appreciated.

推荐答案

NoSQL的一个问题是它不能适合所有地方,但是我也遇到类似的情况我的客户,这是我的解决方案:

There is on thing about NoSQL is that it wont fit every where, but I had similar situation with my client, here is my solution:

videoMaster (videoId(hash), desc, link ..etc)
tagDetail (tagId(hash), videoId(Range))

现在您可以通过传递tagId(kids ,study..etc),您将获得所有带有特定标签的视频

Now you can query by passing tagId (kids, study..etc) you will get all the videos of particular tags

您在tagDetail中的数据如下所示:

Your data in tagDetail will look something like:

kids -> video1
kids -> video2
Education -> video1
Education -> video3

上述解决方案的问题:如果您有数十亿个视频一个特定的标记,因为哈希没有正确分配,您的性能将受到影响。

Problem with the above solution: If you have billions of videos in one particular tag then your performance will be affected as the Hash is not distributed properly.

小提示:您可以为表实现缓存机制进行读取,因此您不必每次都查询数据库。

Small Tip: You can implement caching mechanism for your table reads so that you don't have to query your database every time.

这篇关于在Dynamo DB AWS中搜索列表数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆