5
5
1 . [ Foreword] ( #foreword )
6
6
2 . [ Example] ( #example ) \
7
7
2.1. [ Error handling] ( #error-handling )
8
- 3 . [ HiveSession] ( #hivesession )
9
- 4 . [ HiveOperation] ( #hiveoperation ) \
10
- 4.1. [ HiveUtils] ( #hiveutils )
8
+ 3 . [ DBSQLSession] ( #dbsqlsession )
9
+ 4 . [ DBSQLOperation] ( #dbsqloperation )
11
10
5 . [ Status] ( #status )
12
11
6 . [ Finalize] ( #finalize )
13
12
@@ -23,7 +22,6 @@ If you find any mistakes, misleading or some confusion feel free to create an is
23
22
const { DBSQLClient } = require (' @databricks/sql' );
24
23
25
24
const client = new DBSQLClient ();
26
- const utils = DBSQLClient .utils ;
27
25
28
26
client
29
27
.connect ({
@@ -37,20 +35,17 @@ client
37
35
const createTableOperation = await session .executeStatement (
38
36
' CREATE TABLE IF NOT EXISTS pokes (foo INT, bar STRING)' ,
39
37
);
40
- await utils . waitUntilReady (createTableOperation, false , () => {} );
38
+ await createTableOperation . fetchAll ( );
41
39
await createTableOperation .close ();
42
40
43
41
const loadDataOperation = await session .executeStatement (' INSERT INTO pokes VALUES(123, "Hello, world!"' );
44
- await utils . waitUntilReady (loadDataOperation, false , () => {} );
42
+ await loadDataOperation . fetchAll ( );
45
43
await loadDataOperation .close ();
46
44
47
45
const selectDataOperation = await session .executeStatement (' SELECT * FROM pokes' , { runAsync: true });
48
- await utils .waitUntilReady (selectDataOperation, false , () => {});
49
- await utils .fetchAll (selectDataOperation);
46
+ const result = await selectDataOperation .fetchAll (selectDataOperation);
50
47
await selectDataOperation .close ();
51
48
52
- const result = utils .getResult (selectDataOperation).getValue ();
53
-
54
49
console .log (JSON .stringify (result, null , ' \t ' ));
55
50
56
51
await session .close ();
@@ -71,9 +66,9 @@ client.on('error', (error) => {
71
66
});
72
67
```
73
68
74
- ## HiveSession
69
+ ## DBSQLSession
75
70
76
- After you connect to the server you should open session to start working with Hive server.
71
+ After you connect to the server you should open session to start working with server.
77
72
78
73
``` javascript
79
74
...
@@ -84,9 +79,9 @@ To open session you must provide [OpenSessionRequest](/lib/hive/Commands/OpenSes
84
79
85
80
Into "configuration" you may set any of the configurations that required for the session of your Hive instance.
86
81
87
- After the session is opened you will have the [ HiveSession ] ( /lib/HiveSession .ts ) instance.
82
+ After the session is opened you will have the [ DBSQLSession ] ( /lib/DBSQLSession .ts ) instance.
88
83
89
- Class [ HiveSession ] ( /lib/HiveSession .ts ) is a facade for API that works with [ SessionHandle] ( /lib/hive/Types/index.ts#L77 ) .
84
+ Class [ DBSQLSession ] ( /lib/DBSQLSession .ts ) is a facade for API that works with [ SessionHandle] ( /lib/hive/Types/index.ts#L77 ) .
90
85
91
86
The method you will use the most is ` executeStatement `
92
87
@@ -100,81 +95,44 @@ const operation = await session.executeStatement(
100
95
101
96
- "statement" is DDL/DML statement (CREATE TABLE, INSERT, UPDATE, SELECT, LOAD, etc.)
102
97
103
- - [ options] ( /lib/contracts/IHiveSession .ts#L14 )
98
+ - [ options] ( /lib/contracts/IDBSQLSession .ts#L14 )
104
99
105
100
- runAsync allows executing operation asynchronously.
106
101
107
102
- confOverlay overrides session configuration properties.
108
103
109
104
- timeout is the maximum time to execute an operation. It has Buffer type because timestamp in Hive has capacity 64. So for such value, you should use [ node-int64] ( https://www.npmjs.com/package/node-int64 ) npm module.
110
105
111
- To know other methods see [ IHiveSession] ( /lib/contracts/IHiveSession.ts ) and [ examples/session.js] ( /examples/session.js ) .
112
-
113
- ## HiveOperation
106
+ To know other methods see [ IDBSQLSession] ( /lib/contracts/IDBSQLSession.ts ) and [ examples/session.js] ( /examples/session.js ) .
114
107
115
- In most cases, HiveSession methods return [ HiveOperation ] ( /lib/HiveOperation.ts ) , which helps you to retrieve requested data.
108
+ ## DBSQLOperation
116
109
117
- After you fetch the result, the operation will have [ TableSchema ] ( /lib/hive/Types/index .ts#L143 ) and data (Array< [ RowSet ] ( /lib/hive/Types/index.ts#L218 ) >) .
110
+ In most cases, DBSQLSession methods return [ DBSQLOperation ] ( /lib/DBSQLOperation .ts ) , which helps you to retrieve requested data .
118
111
119
- ### HiveUtils
112
+ After you fetch the result, the operation will have [ TableSchema ] ( /lib/hive/Types/index.ts#L143 ) and data.
120
113
121
- Operation is executed asynchronously, so before retrieving the result, you have to wait until it has finished state.
114
+ Operation is executed asynchronously, but ` fetchChunk ` /` fetchAll ` will wait until it has finished. You can
115
+ get current status of operation any time using a dedicated method:
122
116
123
117
``` javascript
124
118
...
125
119
const response = await operation .status ();
126
120
const isReady = response .operationState === TCLIService_types .TOperationState .FINISHED_STATE ;
127
121
```
128
122
129
- Also, the result is fetched by portions, the size of a portion you can set by method [ setMaxRows() ] ( /lib/HiveOperation.ts#L115 ) .
123
+ Also, the result is fetched by portions, the size of a portion you can pass as option to ` fetchChunk ` / ` fetchAll ` .
130
124
131
125
``` javascript
132
126
...
133
- operation .setMaxRows (500 );
134
- const status = await operation .fetch ();
127
+ const results = await operation .fetchChunk ({ maxRows: 500 });
135
128
```
136
129
137
- After you fetch all data and you have schema and set of data, you can transfrom data in readable format .
130
+ Schema becomes available after you start fetching data.
138
131
139
132
``` javascript
140
133
...
134
+ await operation .fetchChunk ();
141
135
const schema = operation .getSchema ();
142
- const data = operation .getData ();
143
- ```
144
-
145
- To simplify this process, you may use [ HiveUtils] ( /lib/utils/HiveUtils.ts ) .
146
-
147
- ``` typescript
148
- /**
149
- * Executes until operation has status finished or has one of the invalid states.
150
- *
151
- * @param operation operation to perform
152
- * @param progress flag for operation status command. If it sets true, response will include progressUpdateResponse with progress information
153
- * @param callback if callback specified it will be called each time the operation status response received and it will be passed as first parameter
154
- */
155
- waitUntilReady (
156
- operation : IOperation ,
157
- progress ?: boolean ,
158
- callback ?: Function
159
- ): Promise < IOperation >
160
-
161
- /**
162
- * Fetches data until operation hasMoreRows.
163
- *
164
- * @param operation
165
- */
166
- fetchAll (operation : IOperation ): Promise < IOperation >
167
-
168
- /**
169
- * Transforms operation result
170
- *
171
- * @param operation operation to perform
172
- * @param resultHandler you may specify your own handler. If not specified the result is transformed to JSON
173
- */
174
- getResult (
175
- operation : IOperation ,
176
- resultHandler ?: IOperationResult
177
- ): IOperationResult
178
136
```
179
137
180
138
_ NOTICE_
@@ -187,19 +145,13 @@ For more details see [IOperation](/lib/contracts/IOperation.ts).
187
145
### Example
188
146
189
147
``` javascript
190
- const { DBSQLClient } = require (' @databricks/sql' );
191
- const utils = DBSQLClient .utils ;
192
148
...
193
- await utils .waitUntilReady (
194
- operation,
195
- true ,
196
- (stateResponse ) => {
197
- console .log (stateResponse .taskStatus );
198
- }
199
- );
200
- await utils .fetchAll (operation);
201
-
202
- const result = utils .getResult (operation).getValue ();
149
+ const result = await operation .fetchAll ({
150
+ progress: true ,
151
+ callback : (stateResponse ) => {
152
+ console .log (stateResponse .taskStatus );
153
+ },
154
+ });
203
155
```
204
156
205
157
## Status
0 commit comments