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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
#include "import.h"
#include <ydb/public/sdk/cpp/client/ydb_driver/driver.h>
#include <ydb/public/sdk/cpp/client/ydb_operation/operation.h>
#include <ydb/public/sdk/cpp/client/ydb_scheme/scheme.h>
#include <ydb/public/sdk/cpp/client/ydb_table/table.h>
#include <ydb/public/sdk/cpp/client/ydb_proto/accessor.h>
#include <ydb/public/api/protos/ydb_table.pb.h>
#include <ydb/public/api/protos/ydb_formats.pb.h>
#include <ydb/public/lib/ydb_cli/common/recursive_list.h>
#include <ydb/public/lib/ydb_cli/dump/util/util.h>
#include <util/generic/vector.h>
#include <util/stream/file.h>
#include <util/string/builder.h>
#include <util/folder/path.h>
#include <deque>
namespace NYdb {
namespace NConsoleClient {
namespace {
TStatus MakeStatus(EStatus code = EStatus::SUCCESS, const TString& error = {}) {
NYql::TIssues issues;
if (error) {
issues.AddIssue(NYql::TIssue(error));
}
return TStatus(code, std::move(issues));
}
}
TImportFileClient::TImportFileClient(const TDriver& driver)
: OperationClient(std::make_shared<NOperation::TOperationClient>(driver))
, SchemeClient(std::make_shared<NScheme::TSchemeClient>(driver))
, TableClient(std::make_shared<NTable::TTableClient>(driver))
{}
TStatus TImportFileClient::Import(const TString& filePath, const TString& dbPath, const TImportFileSettings& settings) {
if (filePath.empty()) {
return MakeStatus(EStatus::BAD_REQUEST, "No file specified");
}
TFsPath dataFile(filePath);
if (!dataFile.Exists()) {
return MakeStatus(EStatus::BAD_REQUEST,
TStringBuilder() << "File does not exist: " << filePath);
}
if (!dataFile.IsFile()) {
return MakeStatus(EStatus::BAD_REQUEST,
TStringBuilder() << "Not a file: " << filePath);
}
auto result = NDump::DescribePath(*SchemeClient, dbPath).GetStatus();
if (result != EStatus::SUCCESS) {
return MakeStatus(EStatus::SCHEME_ERROR,
TStringBuilder() << "Table does not exist: " << dbPath);
}
if (settings.Format_ != NTable::EDataFormat::CSV) {
return MakeStatus(EStatus::BAD_REQUEST,
TStringBuilder() << "Unsupported format");
}
return UpsertCsv(dataFile, dbPath, settings);
}
namespace {
TStatus WaitForQueue(std::deque<TAsyncStatus>& inFlightRequests, size_t maxQueueSize) {
while (!inFlightRequests.empty() && inFlightRequests.size() > maxQueueSize) {
auto status = inFlightRequests.front().ExtractValueSync();
inFlightRequests.pop_front();
if (!status.IsSuccess()) {
return status;
}
}
return MakeStatus();
}
}
TStatus TImportFileClient::UpsertCsv(const TString& dataFile, const TString& dbPath,
const TImportFileSettings& settings) {
TFileInput input(dataFile, settings.FileBufferSize_);
TString line;
TString buffer;
auto upsertSettings = NTable::TBulkUpsertSettings()
.OperationTimeout(TDuration::Seconds(30))
.ClientTimeout(TDuration::Seconds(35));
auto retrySettings = NTable::TRetryOperationSettings()
.MaxRetries(10);
Ydb::Formats::CsvSettings csvSettings;
bool special = false;
if (settings.Delimiter_ != settings.DefaultDelimiter) {
csvSettings.set_delimiter(settings.Delimiter_);
special = true;
}
if (settings.NullValue_.size()) {
csvSettings.set_null_value(settings.NullValue_);
special = true;
}
// Do not use csvSettings.skip_rows.
for (ui32 i = 0; i < settings.SkipRows_; ++i) {
input.ReadLine(line);
}
TString headerRow;
if (settings.Header_) {
input.ReadLine(headerRow);
headerRow += '\n';
buffer = headerRow;
csvSettings.set_header(true);
}
if (special) {
TString formatSettings;
Y_PROTOBUF_SUPPRESS_NODISCARD csvSettings.SerializeToString(&formatSettings);
upsertSettings.FormatSettings(formatSettings);
}
std::deque<TAsyncStatus> inFlightRequests;
// TODO: better read
// * read serveral lines a time
// * support endlines inside quotes
// ReadLine() should count quotes for it and stop the line then counter is odd.
while (size_t sz = input.ReadLine(line)) {
buffer += line;
buffer += '\n'; // TODO: keep original endline?
if (buffer.Size() >= settings.BytesPerRequest_) {
auto status = WaitForQueue(inFlightRequests, settings.MaxInFlightRequests_);
if (!status.IsSuccess()) {
return status;
}
inFlightRequests.push_back(UpsertCsvBuffer(dbPath, buffer, {}, upsertSettings, retrySettings));
buffer = headerRow;
}
}
if (!buffer.Empty()) {
inFlightRequests.push_back(UpsertCsvBuffer(dbPath, buffer, {}, upsertSettings, retrySettings));
}
return WaitForQueue(inFlightRequests, 0);
}
TAsyncStatus TImportFileClient::UpsertCsvBuffer(const TString& dbPath, const TString& csv, const TString& header,
const NTable::TBulkUpsertSettings& upsertSettings,
const NTable::TRetryOperationSettings& retrySettings) {
auto upsert = [dbPath, csv, header, upsertSettings](NYdb::NTable::TTableClient& tableClient) -> TAsyncStatus {
return tableClient.BulkUpsert(dbPath, NTable::EDataFormat::CSV, csv, header, upsertSettings)
.Apply([](const NYdb::NTable::TAsyncBulkUpsertResult& bulkUpsertResult) {
NYdb::TStatus status = bulkUpsertResult.GetValueSync();
return NThreading::MakeFuture(status);
});
};
return TableClient->RetryOperation(upsert, retrySettings);
}
}
}
|