首先要启动hbase的rest服务
1、src/config/hbase_config.js
module.exports= {
url:'http://10.7.128.18:8080',
namespace_name :'test',
table_name:'data',
column_cluster:['info']
}
2、src/hbase/run_rest.js
const axios = require('axios')
const mysql_config = require('../config/mysql_config')
const {connect, read} = require('../mysql/mysql');
const hbase_config = require('../config/hbase_config')
axios.defaults.baseURL = hbase_config.url
/**
* @description 创建命名空间
* @param config:src/config/hbase_config
* @return {Promise<void>}
*/
let create_namespace = async (config) => {
let path = `namespaces/${config.namespace_name}`
await axios.post(path).then(r => {
}).catch(err => {
console.log(err)
})
}
/**
* @description 判断命名空间是否存在
* @return {Promise<boolean>}
*/
let exist_namespaces = async (config) => {
let flag = false
let path = `namespaces/${config.namespace_name}`
await axios.get(path).then(r => {
if (r.status === 200) {
flag = true
}
}).catch(err => {
console.log(err)
})
return flag
}
/**
* @description 创建表
* @return {Promise<void>}
*/
let create_table = async (config) => {
let path = `${config.namespace_name}:${config.table_name}/schema`;
let body = {
"name": `${config.namespace_name}:${config.table_name}`,
"ColumnSchema": [],
}
for (let i in config.column_cluster) {
if (config.column_cluster.hasOwnProperty(i)) {
let temp = {
"name": config.column_cluster[i],
"VERSIONS": 1
}
body['ColumnSchema'].push(temp)
}
}
await axios.put(path, body).then().catch(err => {
console.log(err)
})
}
/**
* @description 判断表是否存在
* @return {Promise<boolean>}
*/
let exist_table = async (config) => {
let flag = false;
let path = `${config.namespace_name}:${config.table_name}/schema`
await axios.get(path).then(r => {
if (r.status === 200) {
flag = true
}
}).catch(err => {
console.log(err)
})
return flag
}
/**
* @description 数据类型进行base64加密
* @param str
* @return {string}
*/
let transfer = (str) => {
if (str === null) {
str = 'null'
}
if (str === undefined) {
str = 'undefined'
}
if (typeof str === "boolean" || typeof str === "number") {
str = String(str)
}
return Buffer.from(str).toString('base64')
}
/**
* @description 存数据
* @param n 作为row_key
* @param config src/config/hbase_config
* @param insert_data 需要存储的数据,即从mysql中取出的单条记录
* @return {Promise<void>}
*/
let put_data = async (config, n, insert_data) => {
let c = String(n).length
let key;
switch (c) {
case 1:
key = `0000${String(n)}`
break;
case 2:
key = `000${String(n)}`
break;
case 3:
key = `00${String(n)}`
break;
case 4:
key = `0${String(n)}`
break;
case 5:
key = `${String(n)}`
break;
}
let path = `${config.namespace_name}:${config.table_name}/${key}`;
let cell = []
for (let i in insert_data) {
let temp = {
"column": null,
"$": null
}
if (insert_data.hasOwnProperty(i)) {
temp['column'] = transfer(`${hbase_config.column_cluster}:${i}`)
temp['$'] = transfer(insert_data[i])
cell.push(temp)
}
}
let data = {
"Row": [
{
"key": null,
"Cell": cell
}
]
}
data['Row'][0]['key'] = transfer(key)
await axios.put(path, data).then(r => {
}).catch(err => {
console.log(err)
})
}
/**
* @description 返回指定命名空间,指定表中的数据条数
* @param config
* @return {Promise<*>}
*/
let len = async (config) => {
let path = `${config.namespace_name}:${config.table_name}/*`
let le;
await axios.get(path).then(r => {
le = r.data['Row'].length
})
return le
};
!(async () => {
let f = await exist_namespaces(hbase_config)
if (!f) {
await create_namespace(hbase_config)
}
let ft = await exist_table(hbase_config)
if (!ft) {
await create_table(hbase_config)
}
let c = await connect(mysql_config.options)
let arr = await read(c, mysql_config.sql)
let l = await len(hbase_config)
for (let i in arr) {
if (arr.hasOwnProperty(i)) {
await put_data(hbase_config, l + parseInt(i) + 1, arr[i])
}
}
await c.end()
})()