tableHelper.cs
4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using Google.Cloud.Bigtable.Admin.V2;
using System;
using System.Collections.Generic;
using System.Text;
namespace Google.Cloud.Bigtable.ScanTest
{
class tableHelper
{
private AppSettings _appSettings { get; set; }
private BigtableTableAdminClient _adminClient;
private V2.TableName _table;
public tableHelper()
{
this._appSettings = Program.AppSetting;
_adminClient = BigtableTableAdminClient.Create();
_appSettings.TableName = "perf123456";
dropTable();
}
public V2.TableName getTable()
{
Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss} - Checking existance of {_appSettings.TableName}");
bool _isTableExists = checkTable();
if (!_isTableExists)
{
while (!_isTableExists)
{
Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss} - Creating table {_appSettings.TableName}.");
createTable();
_isTableExists = checkTable();
};
}
else
{
_table = new Google.Cloud.Bigtable.V2.TableName(_appSettings.ProjectId, _appSettings.InstanceId, _appSettings.TableName);
}
return _table;
}
private bool checkTable()
{
DateTime startTime = DateTime.Now;
try
{
Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss} - Checking table {_appSettings.TableName}.");
var response = _adminClient.GetTable(new Admin.V2.TableName(_appSettings.ProjectId, _appSettings.InstanceId, _appSettings.TableName), null);
Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss} - Table {_appSettings.TableName} exits.");
}
catch (Exception ex)
{
if (ex.Message.ToLower().Contains("table not found"))
{
Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss} - Table {_appSettings.TableName} not found.");
return false;
}
else
{
Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss} - Exception while checking table {_appSettings.TableName}. Error message: {ex.Message}.");
checkTable();
}
}
return true;
}
private void createTable()
{
try
{
var response = _adminClient.CreateTable(new InstanceName(_appSettings.ProjectId, _appSettings.InstanceId),
_appSettings.TableName,
new Table
{
Granularity = Table.Types.TimestampGranularity.Millis,
ColumnFamilies =
{
{ _appSettings.ColumnFamily, new ColumnFamily { GcRule = new GcRule { MaxNumVersions = 3 } } }
}
});
Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss} - Table created {_appSettings.TableName} successfully.");
_table = new Google.Cloud.Bigtable.V2.TableName(_appSettings.ProjectId, _appSettings.InstanceId, _appSettings.TableName);
new loadDataHelper(_table).loadTable();
}
catch (Exception ex)
{
Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss} - Exception while creating table {_appSettings.TableName}. Error message: {ex.Message}.");
}
}
private void dropTable()
{
DateTime startTime = DateTime.Now;
try
{
_adminClient.DeleteTable(
new Admin.V2.TableName(_appSettings.ProjectId, _appSettings.InstanceId, _appSettings.TableName),
null);
Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss} - Table {_appSettings.TableName} deleted successfully.");
}
catch (Exception ex)
{
Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss} - Exception while deleting table {_appSettings.TableName}, error message: {ex.Message}");
}
}
}
}