forked from welefen/thrift-hbase-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
187 lines (164 loc) · 5.58 KB
/
index.js
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
178
179
180
181
182
183
184
185
186
187
const thrift = require('thrift')
const Debounce = require('think-debounce')
const helper = require('think-helper')
const HBase = require('./gen-nodejs/Hbase.js')
const HBaseTypes = require('./gen-nodejs/Hbase_types.js')
/**
* formatRows
*/
function formatRows(data, includeFamilies) {
const rows = []
for (let i = 0; i < data.length; i++) {
const r = {
rowkey: data[i].row.toString('utf8'),
columns: {}
}
let key
let parts
for (key in data[i].columns) {
if (includeFamilies) {
r.columns[key] = data[i].columns[key].value.toString('utf8')
} else {
parts = key.split(':')
r.columns[parts[1]] = data[i].columns[key].value.toString('utf8')
}
}
rows.push(r)
}
return rows
}
/**
* prepareColumn
* create a columnValue object
* for the given column
*/
function prepareColumn(key, value) {
let column
const v = typeof value !== 'string' ?
JSON.stringify(value) : value
// default family to 'd' for data
const name = key.split(':')
column = name[1] ? name[0] : 'd'
column += ':' + (name[1] ? name[1] : name[0])
return new HBaseTypes.Mutation({
column: column,
value: v
})
}
/**
* prepareColumns
* create an array of columnValue
* objects for the given data
*/
function prepareColumns(data) {
const columns = []
let column
let value
for (column in data) {
value = data[column]
// ignore empty rows
if (!value && value !== 0) {
continue
}
columns.push(prepareColumn(column, value))
}
return columns
}
module.exports = class HbaseClient {
constructor(options) {
this.options = Object.assign({
host: 'localhost',
port: 9090,
timeout: 3000,
connect_timeout: 30000,
logger: console,
transport: thrift.TBufferedTransport,
protocol: thrift.TBinaryProtocol,
compat: "0.98",
}, options);
this.logger = this.options.logger;
this.connection = null;
this.debounce = new Debounce();
}
getConnection() {
if (this.connection) {
return Promise.resolve(this.connection);
}
return this.debounce.debounce('getConnection', () => {
return new Promise((resolve, reject) => {
const connection = thrift.createConnection(this.options.host, this.options.port, {
transport: this.options.transport,
protocol: this.options.protocol,
timeout: this.options.timeout,
connect_timeout: this.options.connect_timeout
})
connection.once('connect', () => {
connection.connection.setKeepAlive(true)
connection.client = thrift.createClient(HBase, connection)
this.connection = connection;
resolve(connection);
})
connection.on('error', err => {
const error = new Error('ThriftHbaseClient connection error');
this.logger.error(error);
this.logger.error(err);
reject(err);
});
connection.on('close', () => {
this.logger.log('ThriftHbaseClient connection closed');
this.close();
})
})
})
}
getRow(options = {}) {
const table = options.table
return this.getConnection().then(connection => {
return new Promise((resolve, reject) => {
// listening error event
// callback not invoked when connection error
const connectionError = err => reject(err);
connection.once('error', connectionError);
const handleResponse = (err, rows) => {
connection.removeListener('error', connectionError);
if (err) {
reject(err);
} else {
resolve(rows ? formatRows(rows, options.includeFamilies)[0] : undefined)
}
}
if (options.columns) {
connection.client.getRowWithColumns(table, options.rowkey, options.columns, null, handleResponse);
} else {
connection.client.getRow(table, options.rowkey, null, handleResponse)
}
})
})
}
putRow(options = {}) {
const table = options.table
const columns = prepareColumns(options.columns)
if (!options.rowkey) {
return Promise.reject(new Error('missing required parameter: rowkey'))
}
return this.getConnection().then(connection => {
return new Promise((resolve, reject) => {
// listening error event
// callback not invoked when connection error
const connectionError = err => reject(err);
connection.once('error', connectionError);
connection.client.mutateRow(table, options.rowkey, columns, null, (err, data) => {
connection.removeListener('error', connectionError);
if (err) reject(err);
resolve(data);
})
})
})
}
close() {
if (this.connection) {
this.connection.connection.destroy();
this.connection = null;
}
}
}