Unity/Database

02. Azure CosmosDB - Insert

๐Ÿ”ท Insert Data

๐Ÿ”ถ Document

https://docs.microsoft.com/en-us/dotnet/api/microsoft.azure.cosmos.table.tableoperation?view=azure-dotnet

 

TableOperation Class (Microsoft.Azure.Cosmos.Table) - Azure for .NET Developers

Represents a single table operation.

docs.microsoft.com

 

 

 

https://docs.microsoft.com/ko-kr/rest/api/storageservices/Understanding-the-Table-Service-Data-Model

 

Table service ๋ฐ์ดํ„ฐ ๋ชจ๋ธ ์ดํ•ด (REST API)-Azure Storage

ํ…Œ์ด๋ธ” ์„œ๋น„์Šค๋Š” ํ…Œ์ด๋ธ” ํ˜•์‹์œผ๋กœ ๊ตฌ์กฐ์  ์ €์žฅ์†Œ๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

docs.microsoft.com

- PartitionKey, RowKey๋Š” ํ•„์ˆ˜ ํ‚ค์ž„

- ์œ ๋‹ˆํฌํ•ด์•ผํ•จ

 

 

 

๐Ÿ”ถ Unity

- ํ…Œ์ด๋ธ”์ด ์•„๋ฌด๊ฒƒ๋„ ์—†๋Š” ์ƒํƒœ

 

 

 

using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using Microsoft.WindowsAzure.Storage.Table;
using MRTK.Tutorials.AzureCloudServices.Scripts.Domain;
using UnityEngine;
using UnityEngine.Events;

namespace MRTK.Tutorials.AzureCloudServices.Scripts.Managers
{
    public class DataManager : MonoBehaviour
    {
        public bool IsReady { get; private set; }

        [Header("Base Settings")]
        [SerializeField]
        private string connectionString = default;
        [Header("Table Settings")]
        [SerializeField]
        private string projectsTableName = "projects";
        [SerializeField]
        private bool tryCreateTableOnStart = true;
        [Header("Events")]
        [SerializeField]
        private UnityEvent onDataManagerReady = default;
        [SerializeField]
        private UnityEvent onDataManagerInitFailed = default;

        private CloudStorageAccount storageAccount;
        private CloudTableClient cloudTableClient;
        private CloudTable projectsTable;

        private async void Awake()
        {
            storageAccount = CloudStorageAccount.Parse(connectionString);
            cloudTableClient = storageAccount.CreateCloudTableClient();
            projectsTable = cloudTableClient.GetTableReference(projectsTableName);
            if (tryCreateTableOnStart)
            {
                try
                {
                    if (await projectsTable.CreateIfNotExistsAsync())
                    {
                        Debug.Log($"Created table {projectsTableName}.");
                    }
                }
                catch (StorageException ex)
                {
                    Debug.LogError("Failed to connect with Azure Storage.\nIf you are running with the default storage emulator configuration, please make sure you have started the storage emulator.");
                    Debug.LogException(ex);
                    onDataManagerInitFailed?.Invoke();
                }
            }

            IsReady = true;
            onDataManagerReady?.Invoke();

        }
}

- DataManager.cs์—์„œ ํ•„์š”ํ•œ ๊ฒƒ๋งŒ ๋‚จ๊ธฐ๊ณ , ๋‚˜๋จธ์ง€๋Š” ์‚ญ์ œํ•จ.

- connectionString : Azure ์—ฐ๊ฒฐ ๋ฌธ์ž์—ด

- projectsTableName : ์ƒ์„ฑ/๋ถˆ๋Ÿฌ์˜ฌ ํ…Œ์ด๋ธ” ์ด๋ฆ„

- tryCreateTableOnStart : True์ผ๋•Œ -> projectsTableName๋ผ๋Š” ํ…Œ์ด๋ธ”์ด ์—†์œผ๋ฉด ํ•ด๋‹น ์ด๋ฆ„์œผ๋กœ ํ…Œ์ด๋ธ” ์ƒ์„ฑ

 

 

 

using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using Microsoft.WindowsAzure.Storage.Table;
using MRTK.Tutorials.AzureCloudServices.Scripts.Domain;
using UnityEngine;
using UnityEngine.Events;

namespace MRTK.Tutorials.AzureCloudServices.Scripts.Managers
{
    public class DataManager : MonoBehaviour
    {
        public bool IsReady { get; private set; }

        [Header("Base Settings")]
        [SerializeField]
        private string connectionString = default;
        [Header("Table Settings")]
        [SerializeField]
        private string projectsTableName = "students";
        [SerializeField]
        private bool tryCreateTableOnStart = true;
        [Header("Events")]
        [SerializeField]
        private UnityEvent onDataManagerReady = default;
        [SerializeField]
        private UnityEvent onDataManagerInitFailed = default;

        private CloudStorageAccount storageAccount;
        private CloudTableClient cloudTableClient;
        private CloudTable projectsTable;

        private async void Awake()
        {
            storageAccount = CloudStorageAccount.Parse(connectionString);
            cloudTableClient = storageAccount.CreateCloudTableClient();
            projectsTable = cloudTableClient.GetTableReference(projectsTableName);
            if (tryCreateTableOnStart)
            {
                try
                {
                    if (await projectsTable.CreateIfNotExistsAsync())
                    {
                        Debug.Log($"Created table {projectsTableName}.");
                    }
                }
                catch (StorageException ex)
                {
                    Debug.LogError("Failed to connect with Azure Storage.\nIf you are running with the default storage emulator configuration, please make sure you have started the storage emulator.");
                    Debug.LogException(ex);
                    onDataManagerInitFailed?.Invoke();
                }
            }

            InsertData();

            IsReady = true;
            onDataManagerReady?.Invoke();

        }

        public void InsertData()
        {
            TableOperation operation;
            TableResult result;

            Student student;

            student = new Student
            {
                PartitionKey = "00",
                RowKey = "00",
                Name = "OJUI",
                Email = "ojui@example.com",
                Address = "Developer Village"
            };

            operation = TableOperation.InsertOrMerge(student);
            result = projectsTable.ExecuteAsync(operation).Result;
        }

    }

    public class Student : TableEntity
    {
        public Student(string PartitionKey, string RowKey, string Name, string Email = null, string Address = null)
        {
            this.PartitionKey = PartitionKey;
            this.RowKey = RowKey;
            this.Name = Name;
            this.Email = Email;
            this.Address = Address;
        }

        public Student() { }

        public string Name { get; set; }

        public string Email { get; set; }

        public string Address { get; set; }

    }

}

- project ์ด๋ฆ„ students๋กœ ๋ณ€๊ฒฝํ•จ

- Student Class : ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ

 

 

 

- playํ•˜๋ฉด ์œ„์™€ ๊ฐ™์€ log๊ฐ€ ์ฐํž˜

 

 

 

- students๋ผ๋Š” ์ด๋ฆ„์˜ ํ…Œ์ด๋ธ”์ด ์ƒ์„ฑ๋จ

- ๋ฐ์ดํ„ฐ๋„ ์‚ฝ์ž…๋œ ๊ฒƒ์„ ํ™•์ธ

 

 

 

using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using Microsoft.WindowsAzure.Storage.Table;
using MRTK.Tutorials.AzureCloudServices.Scripts.Domain;
using UnityEngine;
using UnityEngine.Events;

namespace MRTK.Tutorials.AzureCloudServices.Scripts.Managers
{
    public class DataManager : MonoBehaviour
    {
        public bool IsReady { get; private set; }

        [Header("Base Settings")]
        [SerializeField]
        private string connectionString = default;
        [Header("Table Settings")]
        [SerializeField]
        private string projectsTableName = "students";
        [SerializeField]
        private bool tryCreateTableOnStart = true;
        [Header("Events")]
        [SerializeField]
        private UnityEvent onDataManagerReady = default;
        [SerializeField]
        private UnityEvent onDataManagerInitFailed = default;

        private CloudStorageAccount storageAccount;
        private CloudTableClient cloudTableClient;
        private CloudTable projectsTable;

        private async void Awake()
        {
            storageAccount = CloudStorageAccount.Parse(connectionString);
            cloudTableClient = storageAccount.CreateCloudTableClient();
            projectsTable = cloudTableClient.GetTableReference(projectsTableName);
            if (tryCreateTableOnStart)
            {
                try
                {
                    if (await projectsTable.CreateIfNotExistsAsync())
                    {
                        Debug.Log($"Created table {projectsTableName}.");
                    }
                }
                catch (StorageException ex)
                {
                    Debug.LogError("Failed to connect with Azure Storage.\nIf you are running with the default storage emulator configuration, please make sure you have started the storage emulator.");
                    Debug.LogException(ex);
                    onDataManagerInitFailed?.Invoke();
                }
            }

            InsertData();

            IsReady = true;
            onDataManagerReady?.Invoke();

        }

        public void InsertData()
        {
            TableOperation operation;
            TableResult result;

            Student student;

            student = new Student
            {
                PartitionKey = "01",
                RowKey = "01",
                Name = "Minu",
                Email = "Minu01@example.com",
                Address = "Military Village"
            };

            operation = TableOperation.InsertOrMerge(student);
            result = projectsTable.ExecuteAsync(operation).Result;
        }
    }

    public class Student : TableEntity
    {
        public Student(string PartitionKey, string RowKey, string Name, string Email = null, string Address = null)
        {
            this.PartitionKey = PartitionKey;
            this.RowKey = RowKey;
            this.Name = Name;
            this.Email = Email;
            this.Address = Address;
        }

        public Student() { }

        public string Name { get; set; }

        public string Email { get; set; }

        public string Address { get; set; }

    }

}

- ๋„ฃ๊ณ ์žํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ”๊ฟˆ

 

 

 

- play ํ•ด๋ณด๋ฉด ์ด๋ฒˆ์—๋„ ๋ฐ์ดํ„ฐ๊ฐ€ ์‚ฝ์ž…๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Œ.

 

 

 

๐Ÿ”ถ Potal

- ํฌํ„ธ์—์„œ ์ง์ ‘ ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ์„ ์ˆ˜ ์žˆ์Œ

 

 

 

- Add Entity

 

 

 

- Value ๊ธฐ์ž…

- Add Entity

 

 

 

- ๋ฐ์ดํ„ฐ๊ฐ€ ์ถ”๊ฐ€๋œ ๊ฒƒ ํ™•์ธ

'Unity > Database' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

04. Azure CosmosDB - Delete  (0) 2021.09.06
03. Azure CosmosDB - Read  (0) 2021.08.29
01. Azure CosmosDB Settings  (0) 2021.08.21
00. Azure ์ฒดํ—˜๊ณ„์ • ํ™œ์„ฑํ™”  (0) 2021.08.20
06. Firebase Realtime Database - ์‹ฌํ™”  (1) 2021.08.15