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

查看:15
本文介绍了在 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天全站免登陆