简体   繁体   English

在dynamo db aws中搜索列表数据类型

[英]Search in list data type in dynamo db aws

We are using dynamo db as our database solution for one of our sites. 我们正在使用dynamo db作为我们其中一个站点的数据库解决方案。 we are storing data in dynamo db as per below given json. 我们将数据存储在dynamo db中,如下面给出的json。

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) 我们有可以属于一个/多个类型的视频,因此我们选择了列表数据类型并将数据存储到其中并使该类型成为GSI(全球二级索引)

I am facing several issues. 我面临几个问题。

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. 1)当我将类型定义为索引时,aws仅提供三种数据类型(字符串,二进制,数字),不允许我们存储列表类型数据。 It gives an unexpected data type error. 它会产生意外的数据类型错误。

2) If I do not define it as index I am not allowed to fetch the data. 2)如果我没有将其定义为索引,则不允许获取数据。 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). DynamoDB请求散列密钥,这在我的情况下是不可能的,因为我正在获取不应该依赖于散列键(主键)的列表。

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

Code to fetch data 用于获取数据的代码

$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. 同一视频可以属于多种类型。

So is there anyway that I can search inside a list OR am I not using API in a right way? 那么无论如何我可以在列表中搜索或者我是不是以正确的方式使用API​​? Help is always appreciated. 总是感谢帮助。

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: 关于NoSQL的事情是,它不适合每个地方,但我与我的客户有类似的情况,这是我的解决方案:

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

Now you can query by passing tagId (kids, study..etc) you will get all the videos of particular tags 现在您可以通过传递tagId(kids,study..etc)进行查询,您将获得特定标签的所有视频

Your data in tagDetail will look something like: 您在tagDetail中的数据将类似于:

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. 小提示:您可以为表读取实现缓存机制,这样您就不必每次都查询数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM