예제 #1
0
        /// <summary>Snippet for MutateRows</summary>
        public async Task MutateRows1()
        {
            // Snippet: MutateRows(string, IEnumerable<MutateRowsRequest.Types.Entry>, CallSettings)
            // Create client
            BigtableClient bigtableClient = BigtableClient.Create();
            // Initialize request argument(s)
            string tableName = "projects/[PROJECT]/instances/[INSTANCE]/tables/[TABLE]";
            IEnumerable <MutateRowsRequest.Types.Entry> entries = new MutateRowsRequest.Types.Entry[]
            {
                new MutateRowsRequest.Types.Entry(),
            };

            // Make the request, returning a streaming response
            BigtableClient.MutateRowsStream response = bigtableClient.MutateRows(tableName, entries);

            // Read streaming responses from server until complete
            // Note that C# 8 code can use await foreach
            AsyncResponseStream <MutateRowsResponse> responseStream = response.GetResponseStream();

            while (await responseStream.MoveNextAsync())
            {
                MutateRowsResponse responseItem = responseStream.Current;
                // Do something with streamed response
            }
            // The response stream has completed
            // End snippet
        }
예제 #2
0
        /// <summary>Snippet for MutateRows</summary>
        public async Task MutateRows2ResourceNames()
        {
            // Snippet: MutateRows(TableName, IEnumerable<MutateRowsRequest.Types.Entry>, string, CallSettings)
            // Create client
            BigtableClient bigtableClient = BigtableClient.Create();
            // Initialize request argument(s)
            TableName tableName = TableName.FromProjectInstanceTable("[PROJECT]", "[INSTANCE]", "[TABLE]");
            IEnumerable <MutateRowsRequest.Types.Entry> entries = new MutateRowsRequest.Types.Entry[]
            {
                new MutateRowsRequest.Types.Entry(),
            };
            string appProfileId = "";

            // Make the request, returning a streaming response
            BigtableClient.MutateRowsStream response = bigtableClient.MutateRows(tableName, entries, appProfileId);

            // Read streaming responses from server until complete
            // Note that C# 8 code can use await foreach
            AsyncResponseStream <MutateRowsResponse> responseStream = response.GetResponseStream();

            while (await responseStream.MoveNextAsync())
            {
                MutateRowsResponse responseItem = responseStream.Current;
                // Do something with streamed response
            }
            // The response stream has completed
            // End snippet
        }
예제 #3
0
        public async Task MutateRows()
        {
            // Snippet: MutateRows(MutateRowsRequest,CallSettings)
            // Create client
            BigtableClient bigtableClient = BigtableClient.Create();
            // Initialize request argument
            MutateRowsRequest request = new MutateRowsRequest
            {
                TableName = new TableName("[PROJECT]", "[INSTANCE]", "[TABLE]").ToString(),
                Entries   = { },
            };

            // Make the request, returning a streaming response
            BigtableClient.MutateRowsStream streamingResponse = bigtableClient.MutateRows(request);

            // Read streaming responses from server until complete
            IAsyncEnumerator <MutateRowsResponse> responseStream = streamingResponse.ResponseStream;

            while (await responseStream.MoveNext())
            {
                MutateRowsResponse response = responseStream.Current;
                // Do something with streamed response
            }
            // The response stream has completed
            // End snippet
        }
예제 #4
0
        /// <summary>Snippet for MutateRows</summary>
        /// <remarks>
        /// This snippet has been automatically generated for illustrative purposes only.
        /// It may require modifications to work in your environment.
        /// </remarks>
        public async Task MutateRowsRequestObject()
        {
            // Create client
            BigtableClient bigtableClient = BigtableClient.Create();
            // Initialize request argument(s)
            MutateRowsRequest request = new MutateRowsRequest
            {
                TableNameAsTableName = TableName.FromProjectInstanceTable("[PROJECT]", "[INSTANCE]", "[TABLE]"),
                Entries =
                {
                    new MutateRowsRequest.Types.Entry(),
                },
                AppProfileId = "",
            };

            // Make the request, returning a streaming response
            BigtableClient.MutateRowsStream response = bigtableClient.MutateRows(request);

            // Read streaming responses from server until complete
            // Note that C# 8 code can use await foreach
            AsyncResponseStream <MutateRowsResponse> responseStream = response.GetResponseStream();

            while (await responseStream.MoveNextAsync())
            {
                MutateRowsResponse responseItem = responseStream.Current;
                // Do something with streamed response
            }
            // The response stream has completed
        }
예제 #5
0
        public BigtableClientFixture()
        {
            BigtableClient bigtableClient = BigtableClient.Create();

            _bigtableTableAdminClient = BigtableTableAdminClient.Create();
            Table table = new Table
            {
                Granularity = Table.Types.TimestampGranularity.Millis
            };

            table.ColumnFamilies.Add("stats_summary", new ColumnFamily());
            table.ColumnFamilies.Add("cell_plan", new ColumnFamily());
            CreateTableRequest createTableRequest = new CreateTableRequest
            {
                ParentAsInstanceName = new InstanceName(projectId, instanceId),
                Table   = table,
                TableId = tableId,
            };

            _bigtableTableAdminClient.CreateTable(createTableRequest);

            TableName       tableName          = new TableName(projectId, instanceId, tableId);
            BigtableVersion timestamp          = new BigtableVersion(new DateTime(2020, 1, 10, 14, 0, 0, DateTimeKind.Utc));
            BigtableVersion timestamp_minus_hr = new BigtableVersion(new DateTime(2020, 1, 10, 13, 0, 0, DateTimeKind.Utc));

            MutateRowsRequest.Types.Entry[] entries =
            {
                Mutations.CreateEntry(new BigtableByteString("phone#4c410523#20190501"),
                                      Mutations.SetCell("cell_plan",                                                "data_plan_01gb",  "false",           timestamp),
                                      Mutations.SetCell("cell_plan",                                                "data_plan_01gb",  "true",            timestamp_minus_hr),
                                      Mutations.SetCell("cell_plan",                                                "data_plan_05gb",  "true",            timestamp),
                                      Mutations.SetCell("stats_summary",                                            "connected_cell",  "1",               timestamp),
                                      Mutations.SetCell("stats_summary",                                            "connected_wifi",  "1",               timestamp),
                                      Mutations.SetCell("stats_summary",                                            "os_build",        "PQ2A.190405.003", timestamp)),
                Mutations.CreateEntry(new BigtableByteString("phone#4c410523#20190502"),
                                      Mutations.SetCell("cell_plan",                                                "data_plan_05gb",  "true",            timestamp),
                                      Mutations.SetCell("stats_summary",                                            "connected_cell",  "1",               timestamp),
                                      Mutations.SetCell("stats_summary",                                            "connected_wifi",  "1",               timestamp),
                                      Mutations.SetCell("stats_summary",                                            "os_build",        "PQ2A.190405.004", timestamp)),
                Mutations.CreateEntry(new BigtableByteString("phone#4c410523#20190505"),
                                      Mutations.SetCell("cell_plan",                                                "data_plan_05gb",  "true",            timestamp),
                                      Mutations.SetCell("stats_summary",                                            "connected_cell",  "0",               timestamp),
                                      Mutations.SetCell("stats_summary",                                            "connected_wifi",  "1",               timestamp),
                                      Mutations.SetCell("stats_summary",                                            "os_build",        "PQ2A.190406.000", timestamp)),
                Mutations.CreateEntry(new BigtableByteString("phone#5c10102#20190501"),
                                      Mutations.SetCell("cell_plan",                                                "data_plan_10gb",  "true",            timestamp),
                                      Mutations.SetCell("stats_summary",                                            "connected_cell",  "1",               timestamp),
                                      Mutations.SetCell("stats_summary",                                            "connected_wifi",  "1",               timestamp),
                                      Mutations.SetCell("stats_summary",                                            "os_build",        "PQ2A.190401.002", timestamp)),
                Mutations.CreateEntry(new BigtableByteString("phone#5c10102#20190502"),
                                      Mutations.SetCell("cell_plan",                                                "data_plan_10gb",  "true",            timestamp),
                                      Mutations.SetCell("stats_summary",                                            "connected_cell",  "1",               timestamp),
                                      Mutations.SetCell("stats_summary",                                            "connected_wifi",  "0",               timestamp),
                                      Mutations.SetCell("stats_summary",                                            "os_build",        "PQ2A.190406.000", timestamp))
            };

            bigtableClient.MutateRows(tableName, entries);
        }
예제 #6
0
        /// <summary>
        /// Mutate multiple rows in an existing table and column family. Updates multiple cells within each row.
        ///</summary>
        /// <param name="projectId">Your Google Cloud Project ID.</param>
        /// <param name="instanceId">Your Google Cloud Bigtable Instance ID.</param>
        /// <param name="tableId">Your Google Cloud Bigtable table ID.</param>
        public string writeBatch(
            string projectId  = "YOUR-PROJECT-ID",
            string instanceId = "YOUR-INSTANCE-ID",
            string tableId    = "YOUR-TABLE-ID")
        {
            BigtableClient bigtableClient = BigtableClient.Create();

            TableName       tableName     = new TableName(projectId, instanceId, tableId);
            BigtableVersion timestamp     = new BigtableVersion(DateTime.UtcNow);
            String          COLUMN_FAMILY = "stats_summary";

            MutateRowsRequest.Types.Entry mutations1 = Mutations.CreateEntry(new BigtableByteString("tablet#a0b81f74#20190501"),
                                                                             Mutations.SetCell(COLUMN_FAMILY, "connected_cell", 1, timestamp),
                                                                             Mutations.SetCell(COLUMN_FAMILY, "os_build", "12155.0.0-rc1", timestamp)
                                                                             );
            MutateRowsRequest.Types.Entry mutations2 = Mutations.CreateEntry(new BigtableByteString("tablet#a0b81f74#20190502"),
                                                                             Mutations.SetCell(COLUMN_FAMILY, "connected_cell", 1, timestamp),
                                                                             Mutations.SetCell(COLUMN_FAMILY, "os_build", "12145.0.0-rc6", timestamp)
                                                                             );
            MutateRowsRequest.Types.Entry[] entries =
            {
                mutations1,
                mutations2
            };
            MutateRowsResponse mutateRowResponse = bigtableClient.MutateRows(tableName, entries);

            foreach (MutateRowsResponse.Types.Entry entry in mutateRowResponse.Entries)
            {
                if (entry.Status.Code == 0)
                {
                    Console.WriteLine($"Row {entry.Index} written successfully");
                }
                else
                {
                    Console.WriteLine($"\tFailed to write row {entry.Index}");
                    Console.WriteLine(entry.Status.Message);
                    return(entry.Status.Message);
                }
            }
            return("Successfully wrote 2 rows");
        }
예제 #7
0
        private static void DoHelloWorld()
        {
            try
            {
                // [START bigtable_hw_connect]
                // BigtableTableAdminClient API lets us create, manage and delete tables.
                BigtableTableAdminClient bigtableTableAdminClient = BigtableTableAdminClient.Create();

                // BigtableClient API lets us read and write to a table.
                BigtableClient bigtableClient = BigtableClient.Create();
                // [END bigtable_hw_connect]

                // [START bigtable_hw_create_table]
                // Create a table with a single column family.
                Console.WriteLine($"Create new table: {tableId} with column family: {columnFamily}, instance: {instanceId}");

                // Check whether a table with given TableName already exists.
                if (!TableExist(bigtableTableAdminClient))
                {
                    bigtableTableAdminClient.CreateTable(
                        new InstanceName(projectId, instanceId),
                        tableId,
                        new Table
                    {
                        Granularity    = Table.Types.TimestampGranularity.Millis,
                        ColumnFamilies =
                        {
                            {
                                columnFamily, new ColumnFamily
                                {
                                    GcRule = new GcRule
                                    {
                                        MaxNumVersions = 1
                                    }
                                }
                            }
                        }
                    });
                    // Confirm that table was created successfully.
                    Console.WriteLine(TableExist(bigtableTableAdminClient)
                        ? $"Table {tableId} created successfully\n"
                        : $"There was a problem creating a table {tableId}");
                }
                else
                {
                    Console.WriteLine($"Table: {tableId} already exists");
                }
                // [END bigtable_hw_create_table]

                // [START bigtable_hw_write_rows]
                // Initialize Google.Cloud.Bigtable.V2.TableName object.
                Google.Cloud.Bigtable.Common.V2.TableName tableName = new Google.Cloud.Bigtable.Common.V2.TableName(projectId, instanceId, tableId);

                // Write some rows

                /* Each row has a unique row key.
                 *
                 *     Note: This example uses sequential numeric IDs for simplicity, but
                 *     this can result in poor performance in a production application.
                 *     Since rows are stored in sorted order by key, sequential keys can
                 *     result in poor distribution of operations across nodes.
                 *
                 *     For more information about how to design a Bigtable schema for the
                 *     best performance, see the documentation:
                 *
                 *     https://cloud.google.com/bigtable/docs/schema-design */

                Console.WriteLine($"Write some greetings to the table {tableId}");

                // Insert 1 row using MutateRow()
                s_greetingIndex = 0;
                try
                {
                    bigtableClient.MutateRow(tableName, rowKeyPrefix + s_greetingIndex, MutationBuilder());
                    Console.WriteLine($"\tGreeting:   -- {s_greetings[s_greetingIndex],-18}-- written successfully");
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"\tFailed to write greeting: --{s_greetings[s_greetingIndex]}");
                    Console.WriteLine(ex.Message);
                    throw;
                }

                // Insert multiple rows using MutateRows()
                // Build a MutateRowsRequest (contains table name and a collection of entries).
                MutateRowsRequest request = new MutateRowsRequest
                {
                    TableNameAsTableName = tableName
                };

                s_mapToOriginalGreetingIndex = new List <int>();
                while (++s_greetingIndex < s_greetings.Length)
                {
                    s_mapToOriginalGreetingIndex.Add(s_greetingIndex);
                    // Build an entry for every greeting (consists of rowkey and a collection of mutations).
                    string rowKey = rowKeyPrefix + s_greetingIndex;
                    request.Entries.Add(Mutations.CreateEntry(rowKey, MutationBuilder()));
                }

                // Make the request to write multiple rows.
                MutateRowsResponse response = bigtableClient.MutateRows(request);

                // Check the status code of each entry to ensure that it was written successfully.
                foreach (MutateRowsResponse.Types.Entry entry in response.Entries)
                {
                    s_greetingIndex = s_mapToOriginalGreetingIndex[(int)entry.Index];
                    if (entry.Status.Code == 0)
                    {
                        Console.WriteLine($"\tGreeting:   -- {s_greetings[s_greetingIndex],-18}-- written successfully");
                    }
                    else
                    {
                        Console.WriteLine($"\tFailed to write greeting: --{s_greetings[s_greetingIndex]}");
                        Console.WriteLine(entry.Status.Message);
                    }
                }

                Mutation MutationBuilder() =>
                Mutations.SetCell(columnFamily, columnName, s_greetings[s_greetingIndex], new BigtableVersion(DateTime.UtcNow));

                //[END bigtable_hw_write_rows]

                // [START bigtable_hw_create_filter]
                RowFilter filter = RowFilters.CellsPerRowLimit(1);
                // [END bigtable_hw_create_filter]

                // [START bigtable_hw_get_with_filter]
                // Read from the table.
                Console.WriteLine("Read the first row");

                int rowIndex = 0;

                // Read a specific row. Apply a filter to return latest only cell value accross entire row.
                Row rowRead = bigtableClient.ReadRow(
                    tableName, rowKey: rowKeyPrefix + rowIndex, filter: filter);
                Console.WriteLine(
                    $"\tRow key: {rowRead.Key.ToStringUtf8()} " +
                    $"  -- Value: {rowRead.Families[0].Columns[0].Cells[0].Value.ToStringUtf8(),-16} " +
                    $"  -- Time Stamp: {rowRead.Families[0].Columns[0].Cells[0].TimestampMicros}");
                // [END bigtable_hw_get_with_filter]

                // [START bigtable_hw_scan_with_filter]
                Console.WriteLine("Read all rows using streaming");
                // stream the content of the whole table. Apply a filter to return latest only cell values accross all rows.
                ReadRowsStream responseRead = bigtableClient.ReadRows(tableName, filter: filter);

                Task printRead = PrintReadRowsAsync();
                printRead.Wait();

                async Task PrintReadRowsAsync()
                {
                    await responseRead.ForEachAsync(row =>
                    {
                        Console.WriteLine($"\tRow key: {row.Key.ToStringUtf8()} " +
                                          $"  -- Value: {row.Families[0].Columns[0].Cells[0].Value.ToStringUtf8(),-16} " +
                                          $"  -- Time Stamp: {row.Families[0].Columns[0].Cells[0].TimestampMicros}");
                    });
                }
                // [END bigtable_hw_scan_with_filter]

                // [START bigtable_hw_delete_table]
                // Clean up. Delete the table.
                Console.WriteLine($"Delete table: {tableId}");

                bigtableTableAdminClient.DeleteTable(name: tableName);
                if (!TableExist(bigtableTableAdminClient))
                {
                    Console.WriteLine($"Table: {tableId} deleted successfully");
                }
                // [END bigtable_hw_delete_table]
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Exception while running HelloWorld: {ex.Message}");
            }
        }
예제 #8
0
            internal void DoHelloWorld()
            {
                #region 1. Create table with column family.

                try
                {
                    Console.WriteLine($"Create new table: {_settings.TableName} with column family {_settings.ColumnFamily}, Instance: {_settings.InstanceId}");

                    // Check whether a table with given TableName already exists.
                    if (!DoesTableExist(_bigtableTableAdminClient, _tableNameAdmin))
                    {
                        _bigtableTableAdminClient.CreateTable(
                            new InstanceName(_settings.ProjectId, _settings.InstanceId),
                            _settings.TableName,
                            new Table
                        {
                            Granularity    = Table.Types.TimestampGranularity.Millis,
                            ColumnFamilies =
                            {
                                {
                                    _settings.ColumnFamily, new ColumnFamily
                                    {
                                        GcRule = new GcRule
                                        {
                                            MaxNumVersions = 1
                                        }
                                    }
                                }
                            }
                        });
                        Console.WriteLine(DoesTableExist(_bigtableTableAdminClient, _tableNameAdmin)
                            ? $"Table {_settings.TableName} created succsessfully\n"
                            : $"There was a problem creating a table {_settings.TableName}");
                    }
                    else
                    {
                        Console.WriteLine($"Table: {_settings.TableName} already exist");
                    }

                    #endregion

                    #region 2. Insert multiple rows.

                    /* Each row has a unique row key.
                     *
                     * Note: This example uses sequential numeric IDs for simplicity, but
                     * this can result in poor performance in a production application.
                     * Since rows are stored in sorted order by key, sequential keys can
                     * result in poor distribution of operations across nodes.
                     *
                     * For more information about how to design a Bigtable schema for the
                     * best performance, see the documentation:
                     *
                     * https://cloud.google.com/bigtable/docs/schema-design */

                    Console.WriteLine($"Write some greetings to the table {_settings.TableName}");

                    // Insert 1 row using MutateRow()
                    _greetingIndex = 0;
                    try
                    {
                        _bigtableClient.MutateRow(_tableNameClient, _settings.RowKeyPrefix + _greetingIndex, MutationBuilder(_greetingIndex));
                        Console.WriteLine($"\tGreeting: --{_settings.Greetings[_greetingIndex]}-- written successfully");
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine($"\tFailed to write Greeting: --{_settings.Greetings[_greetingIndex]}");
                        Console.WriteLine(ex.Message);
                        throw;
                    }

                    // Insert multiple rows using MutateRows()
                    // Build a MutateRowsRequest (contains table name and a collection of entries).
                    MutateRowsRequest request = new MutateRowsRequest
                    {
                        TableNameAsTableName = _tableNameClient
                    };

                    _mapToOriginalGreetingIndex = new List <int>();

                    while (++_greetingIndex < _settings.Greetings.Length)
                    {
                        _mapToOriginalGreetingIndex.Add(_greetingIndex);
                        // Build an entry for every greeting (contains of row key and a collection of mutations).
                        request.Entries.Add(Mutations.CreateEntry(_settings.RowKeyPrefix + _greetingIndex,
                                                                  MutationBuilder(_greetingIndex)));
                    }

                    BigtableClient.MutateRowsStream response = _bigtableClient.MutateRows(request);

                    Task write = CheckWrittenAsync(response);
                    write.Wait();

                    #endregion

                    #region Read the first row

                    Console.WriteLine("Read the first row");

                    int rowIndex = 0;

                    Row rowRead = _bigtableClient.ReadRow(_tableNameClient, _settings.RowKeyPrefix + rowIndex);
                    Console.WriteLine(
                        $"\tRow key: {rowRead.Key.ToStringUtf8()}, Value: {rowRead.Families[0].Columns[0].Cells[0].Value.ToStringUtf8()}");

                    #endregion

                    #region Read all rows.

                    BigtableClient.ReadRowsStream responseRead = _bigtableClient.ReadRows(_tableNameClient);

                    Task printRead = PrintReadRowsAsync(responseRead);
                    printRead.Wait();

                    #endregion

                    #region Delete table.

                    Console.WriteLine($"Delete table: {_settings.TableName}");

                    _bigtableTableAdminClient.DeleteTable(_tableNameAdmin);
                    if (DoesTableExist(_bigtableTableAdminClient, _tableNameAdmin) == false)
                    {
                        Console.WriteLine($"Table: {_settings.TableName} deleted succsessfully");
                    }

                    #endregion
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }