跳轉至

如何使用資料存儲

資料存儲 使用 NoSQL 雲端資料庫來同步和存儲用於客戶端的數據,例如遊戲的組件和設置信息。使用此服務是基於客戶端的遊戲或需要客戶端之間實時更新的好解決方案。

資料庫 具有以下功能:

  • 數據分別存儲在每個遊戲的存儲中。
  • 所有數據均已加密並安全傳送。
  • SQL數據庫不同,NoSQL雲數據庫沒有表和行,數據以由集合組成的文檔形式存儲。
  • -對的形式存儲和記錄數據。
  • 優化存儲由多個文檔組成的集合。
  • 有關設計-的注意事項,請參見Hive開發者的操作 > 遊戲 數據存儲指南。

 


添加資料

您可以一次以一對鍵值或多對映射格式將數據添加到數據存儲中。請求後,將返回truefalse

Warning

鍵值需要設置在一維 JSON 字串中,該字串包含一個簡單的字串或反斜線(\)。

以下是添加鍵值對數據的示例代碼。

API 參考: DataStore .set

using hive;    
    string key = "your data key";    
    string value = "your data value";    
    DataStore.set(key, value, (ResultAPI result) => {    
         if (result.isSuccess()) {    
             // call successful    
         } else {    
             // Call failed. See error code below    
         }    
});
#include "HiveDataStore.h"

FString Key = TEXT("你的數據鍵");
FString Value = TEXT("你的數據值");

FHiveDataStore::Set(Key, Value, FHiveDataStoreOnSetDelegate::CreateLambda([this](const FHiveResultAPI& Result) {
        if (Result.IsSuccess()) {
                // API 呼叫成功 
        } else {
                // 呼叫失敗。請參閱下面的錯誤代碼
        }
}));

API 參考: DataStore ::set

#include <HIVE_SDK_Plugin/HIVE_CPP.h>    
    using namespace std;    
    using namespace hive;    

    string key = "your data key";    
    string value = "your data value";    

    DataStore::set(key, value, [=](ResultAPI const & result) {    
         if (result.isSuccess()) {    
             // call successful    
         } else {    
             // Call failed. See error code below    
         }    
});

API 參考: DataStore.set

import com.hive.DataStore    
    import com.hive.ResultAPI    
    val key = "your data key"    
    val value = "your data value"    
    DataStore.set(key, value, object : DataStore.DataStoreSetListener {    
         override fun onDataStoreSet(result: ResultAPI) {    
             if (result.isSuccess) {    
                 // call successful    
             } else {    
                 // Call failed. See error code below    
             }    
         }    
})

API 參考: DataStore.INSTANCE .set

import com.hive.DataStore;    
    import com.hive.ResultAPI;    
    String key = "your data key";    
    String value = "your data value";    
    DataStore.INSTANCE.set(key, value, result -> {    
         if (result.isSuccess()) {    
             // call successful    
         } else {    
             // Call failed. See error code below    
         }    
});

API 參考: DataStoreInterface.set

import HIVEService    
    let key = "your data key"    
    let value = "your data value"    
    DataStoreInterface.set(key, value: value) { result in    
        if result.isSuccess() {    
        // call successful    
        }    
        else {    
        // Call failed. See error code below    
        }    
}

API 參考: HIVEDataStore set

#import <HIVEService/HIVEService-Swift.h>    
    NSString *key = @"your data key";    
    NSString *value = @"your data value";    

    [HIVEDataStore set: key value: value handler: ^(HIVEResultAPI *result) {    
         if ([result isSuccess]) {    
             // call successful    
         } else {    
             // Call failed. See error code below    
         }    
}];

以下是添加格式化 Map 数据的示例代码。

API 參考: DataStore .set

using hive;    
    Dictionary<string, string> map = new Dictionary<string, string> {    
         {"key1", "value1"},    
         {"key2", "value2"},    
         {"key3", "value3"}    
    };    

    DataStore.set(map, (ResultAPI result) => {    
         if (result.isSuccess()) {    
             // call successfull    
         } else {    
             // Call failed. See error code below    
         }    
});
#include "HiveDataStore.h"

TMap<FString, FString> Data;
Data.Emplace(TEXT("key1"), TEXT("value1"));
Data.Emplace(TEXT("key2"), TEXT("value2"));
Data.Emplace(TEXT("key3"), TEXT("value3"));

FHiveDataStore::Set(Data, FHiveDataStoreOnSetDelegate::CreateLambda([this](const FHiveResultAPI& Result) {
        if (Result.IsSuccess()) {
                // call successfull
        } else {
                // Call failed. See error code below
        }
}));

API 參考: DataStore ::set

#include <HIVE_SDK_Plugin/HIVE_CPP.h>    
    using namespace std;    
    using namespace hive;    

    map<string, string> map = {    
         {"key1", "value1"},    
         {"key2", "value2"},    
         {"key3", "value3"}    
    };    

    DataStore::set(map, [=](ResultAPI const & result) {    
         if (result.isSuccess()) {    
             // call successful    
         } else {    
             // Call failed. See error code below    
         }    
});

API 參考: DataStore.set

import com.hive.DataStore    
    import com.hive.ResultAPI    
    val map = mapOf(    
         "key1" to "value1",    
         "key2" to "value2",    
         "key3" to "value3",    
    )    
    DataStore.set(map, object : DataStore.DataStoreSetListener {    
         override fun onDataStoreSet(result: ResultAPI) {    
             if (result.isSuccess) {    
                 // call successful    
             } else {    
                 // Call failed. See error code below    
             }    
         }    
})

API 參考: DataStore.INSTANCE .set

import com.hive.DataStore;    
    import com.hive.ResultAPI;    
    Map<String, String> map = new HashMap<>();    
    map.put("key1", "value1");    
    map.put("key2", "value2");    
    map.put("key3", "value3");    
    DataStore.INSTANCE.set(map, result -> {    
         if (result.isSuccess()) {    
             // call successful    
         } else {    
             // Call failed. See error code below    
         }    
});

API 參考: DataStoreInterface.set

import HIVEService    
    let map: [String: String] = [    
    "key1" : "value1",    
    "key2" : "value2",    
    "key3" : "value3"    
    ]    
    DataStoreInterface.set(map) { result in    
        if result.isSuccess() {    
        // call successful    
        } else {    
        // Call failed. See error code below    
        }    
}

API 參考: HIVEDataStore set

#import <HIVEService/HIVEService-Swift.h>    
    NSDictionary<NSString *, NSString *> *map = @{    
         @"key1" : @"value1",    
         @"key2" : @"value2",    
         @"key3" : @"value3"    
    };    

    [HIVEDataStore set: map handler: ^(HIVEResultAPI *result) {    
         if ([result isSuccess]) {    
             // call successful    
         } else {    
             // Call failed. See error code below    
         }    
}];

獲取數據

有兩種方法可以獲取您的數據,一次一個或一次全部,您還可以獲取與您請求的鍵相對應的所有用戶數據。獲取數據的三種方法如下:

  • 獲取我的一個數據
    • 獲取我的所有數據
    • 使用鍵獲取所有數據

查看以下每種方式的範例代碼。

以下是获取我的数据的示例代码。

API 参考: DataStore .get

using hive;    
    string key = "your data key";    

    DataStore.get(key, (ResultAPI result, string data) => {    
         if (result.isSuccess()) {    
             // call successfull    
         } else {    
             // Call failed. See error code below    
         }    
});
#include "HiveDataStore.h"

FString Key = TEXT("your data key");
FHiveDataStore::Get(Key, FHiveDataStoreOnGetDelegate::CreateLambda([this](const FHiveResultAPI& Result, const FString& Data) {
        if (Result.IsSuccess()) {
                // API 呼叫成功
        } else {
                // 呼叫失敗。請參見下面的錯誤代碼
        }
}));

API 參考: DataStore ::get

#include <HIVE_SDK_Plugin/HIVE_CPP.h>    
    using namespace std;    
    using namespace hive;    

    string key = "your data key";    

    DataStore::get(key, [=](ResultAPI const & result, string data) {    
         if (result.isSuccess()) {    
             // call successful    
         } else {    
             // Call failed. See error code below    
         }    
});

API 參考: DataStore.get

import com.hive.DataStore    
    import com.hive.ResultAPI    
    val key = "your data key"    
    DataStore.get(key, object : DataStore.DataStoreGetListener {    
         override fun onDataStoreGet(result: ResultAPI, data: String?) {    
             if (result.isSuccess) {    
                 // call successful    
             } else {    
                 // Call failed. See error code below    
             }    
         }    
})

API 參考: DataStore.INSTANCE .get

import com.hive.DataStore;    
    import com.hive.ResultAPI;    
    String key = "your data key";    
    DataStore.INSTANCE.get(key, (result, data) -> {    
         if (result.isSuccess()) {    
             // call successful    
         } else {    
             // Call failed. See error code below    
         }    
});

API 參考: DataStoreInterface.get

import HIVEService    
    let key = "your data key"    
    DataStoreInterface.get(key) { result, data in    
        if result.isSuccess() {    
        // call successful    
        } else {    
        // Call failed. See error code below    
        }    
}

API 參考: HIVEDataStore get

#import <HIVEService/HIVEService-Swift.h>    
    NSString* key = @"your data key";    

    [HIVEDataStore get: key handler: ^(HIVEResultAPI *result, NSString *data) {    
         if ([result isSuccess]) {    
             // call successful    
         } else {    
             // Call failed. See error code below    
         }    
}];

以下是獲取我所有數據的示例代碼。

API 參考: DataStore .getMyData

using hive;    
    DataStore.getMyData((ResultAPI result, Dictionary<string, string> myData) => {    
         if (result.isSuccess()) {    
             // call successfull    
             // myData: the added key-value map    
         } else {    
             // Call failed. See error code below    
         }    
});
#include "HiveDataStore.h"

FHiveDataStore::GetMyData(FHiveDataStoreOnMyDataDelegate::CreateLambda([this](const FHiveResultAPI& Result, const DataStoreData& MyData) {

        if (Result.IsSuccess()) {
                // API  call successfull
                // myData: the added key-value map  
        } else {
                // Call failed. See error code below
        }
}));

API 參考: DataStore .getMyData

#include <HIVE_SDK_Plugin/HIVE_CPP.h>    
    using namespace std;    
    using namespace hive;    
    DataStore.getMyData([=](ResultAPI const & result, map<string, string> const & myData) {    
         if (result.isSuccess()) {    
             // call successful    
             // myData: the added key-value map    
         } else {    
             // Call failed. See error code below    
         }    
});

API 參考: DataStore.getMyData

import com.hive.DataStore    
    import com.hive.ResultAPI    
    DataStore.getMyData(object : DataStore.DataStoreMyDataListener {    
         override fun onDataStoreMyData(result: ResultAPI, myData: Map<String, String>) {    
             if (result.isSuccess) {    
                 // call successful    
                 // myData: the added key-value map    
             } else {    
                 // Call failed. See error code below    
             }    
         }    
})

API 參考: DataStore.INSTANCE .getMyData

import com.hive.DataStore    
    import com.hive.ResultAPI    
    DataStore.INSTANCE.getMyData((result, myData) -> {    
         if (result.isSuccess()) {    
             // call successful    
             // myData: the added key-value map    
         } else {    
             // Call failed. See error code below    
         }    
});

API 參考: DataStoreInterface .getMyData

import HIVEService    
    DataStoreInterface.getMyData() { result, myData in    
        if result.isSuccess() {    
        // call successful    
                // myData: the added key-value map    
        } else {    
        // Call failed. See error code below    
        }    
}

API 參考: HIVEDataStore getMyData

#import <HIVEService/HIVEService-Swift.h>    
    [HIVEDataStore getMyData: ^(HIVEResultAPI *result, NSDictionary<NSString *, NSString *> *myData) {    
         if ([result isSuccess]) {    
             // 调用成功    
             // myData: 添加的键值映射    
         } else {    
             // 调用失败。请参见下面的错误代码    
         }    
}];

以下是使用键获取所有数据的示例代码。

API 参考: DataStore .getUsersData

using hive;    
    string key = "your data key";    
    DataStore.getUsersData(key, (ResultAPI result, string key, Dictionary<long, string> usersData) => {    
         if (result.isSuccess()) {    
             // call successful    
             // key: Entered key    
             // usersData: Map in PlayerId-value format. Be careful about the long key type.    
         } else {    
             // Call failed. See error code below    
         }    
});
#include "HiveDataStore.h"

FString Key = TEXT("your data key");
FHiveDataStore::GetUsersData(Key, FHiveDataStoreOnUsersDataDelegate::CreateLambda([this](const FHiveResultAPI& Result, const FString& Key, const DataStoreUsersData& UsersData) {
        if (Result.IsSuccess()) {
                // call successful    
      // key: Entered key    
      // usersData: Map in PlayerId-value format. Be careful about the long key type.的
        } else {
                // Call failed. See error code below
        }
}));

API 參考: DataStore ::getUsersData

#include <HIVE_SDK_Plugin/HIVE_CPP.h>    
    using namespace std;    
    using namespace hive;    

    string key = "your data key";    

    DataStore::getUsersData(key, [=](ResultAPI const & result, string key, map const & usersData) {    
         if (result.isSuccess()) {    
             // call successful    
             // key: Entered key    
             // usersData: Map in PlayerId-value format. Be careful that the key is of long long type.    
         } else {    
             // Call failed. See error code below    
         }    
});

API 參考: DataStore.getUsersData

import com.hive.DataStore    
    import com.hive.ResultAPI    
    val key = "your data key"    
    DataStore.getUsersData(key, object : DataStore.DataStoreUsersDataListener {    
         override fun onDataStoreUsersData(result: ResultAPI, key: String?, usersData: Map<Long, String>) {    
             if (result.isSuccess) {    
                 // call successful    
                 // key: Entered key    
                 // usersData: Map in PlayerId-value format. Be careful about the long type.    
             } else {    
                 // Call failed. See error code below    
             }    
         }    
})

API 參考: DataStore.INSTANCE .getUsersData

import com.hive.DataStore;    
    import com.hive.ResultAPI;    
    String key = "your data key";    
    DataStore.INSTANCE.getUsersData(key, (result, key1, usersData) -> {    
         if (result.isSuccess()) {    
             // call successful    
             // key: Entered key    
             // usersData: Map in PlayerId-value format. Be careful about the long type.    
         } else {    
             // Call failed. See error code below    
         }    
});

API 參考: DataStoreInterface.getUsersData

import HIVEService    
    let key = "your data key"    
    DataStoreInterface.getUsersData(key) { result, key, usersData in    
        if result.isSuccess() {    
        // call successful    
        // key: Entered key    
        // usersData: Map in PlayerId-value format. Note that the key is of type Int64.    
        } else {    
        // Call failed. See error code below    
        }    
}

API 參考: HIVEDataStore getUsersData

#import <HIVEService/HIVEService-Swift.h>    
    NSString* key = @"你的数据密钥";    
    [HIVEDataStore getUsersData: key handler: ^(HIVEResultAPI *result, NSString *key, NSDictionary<NSNumber *, NSString *> *usersData) {    
         if ([result isSuccess]) {    
             // 成功    
             // key: 输入的密钥    
             // usersData: 以 PlayerId-值格式的映射。注意,密钥的类型为 NSNumber *。    
         } else {    
             // 调用失败。请参阅下面的错误代码    
         }    
}];

錯誤代碼

錯誤代碼 訊息 描述
RESPONSE_FAIL DataStoreNotExistKey 伺服器上不存在該鍵
RESPONSE_FAIL DataStoreNotExistColumn 伺服器上不存在列族(表)
RESPONSE_FAIL DataStoreGameIsBeingInspected 遊戲的資料存儲正在維護中
DEVELOPER_ERROR DataStoreNotExistPublicKey 公開金鑰不存在。需要檢查Hive 控制台上的設置。
NEED_INITIALIZE DataStoreNotInitialized SDK設置(AuthV4.setup)尚未完成
INVALID_SESSION DataStoreNeedSignIn 尚未登入。需要登入。
DEVELOPER_ERROR DataStoreDisabled 資料存儲設置為不使用。需要檢查Hive 控制台上的設置。
RESPONSE_FAIL DataStoreResponseError 成功連接到伺服器但返回錯誤。需要檢查錯誤訊息。
INVALID_PARAM DataStoreInvalidParam 由於無效參數,呼叫set() API失敗