package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
controller:
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @Author: jja
* @Description:
* @Date: 2020/2/22 12:36
*/
@RestController
public class HbaseController {
@Autowired
HbaseService hbaseService;
@GetMapping("/hello")
public String print() {
return "ok";
}
/**
* 获取所有的命名空间
* @return 命名空间的list
*/
@GetMapping("/getAllNameSpaces")
public List<String> getAllNameSpaces() {
List<String> allNamespacesList = new ArrayList<>();
try {
allNamespacesList = this.hbaseService.getAllNamespaces();
} catch (Exception e) {
e.printStackTrace();
}
return allNamespacesList;
}
/**
* 获取某个命名空间下的表名
* @param nameSpace 命名空间
* @return 命名空间的list
*/
@PostMapping("/nameSpace")
public List<String> getAllTableNamesOfNameSpace(String nameSpace) {
List<String> allTableNamesOfNameSpaceList = new ArrayList<>();
try {
allTableNamesOfNameSpaceList = this.hbaseService.getAllTableNamesOfNameSpace(nameSpace);
} catch (Exception e) {
e.printStackTrace();
}
return allTableNamesOfNameSpaceList;
}
/**
* 根据tablename查询数据
* @param tableName 表名
* @param pageMun 每页多少条
* @param pageSize 第几页
* @return 数据
* @throws IOException
*/
@PostMapping("/getPageData")
public Map<String, Map<String, String>> getPageData(String tableName, int pageMun, int pageSize) throws IOException {
Map<String, Map<String, String>> stringMapMap = new HashMap<>();
try {
stringMapMap = this.hbaseService.getPageData(tableName, pageMun,pageSize);
} catch (IOException e) {
e.printStackTrace();
}
return stringMapMap;
}
}
service:
package com.example.demo;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.PageFilter;
import org.apache.hadoop.hbase.util.Bytes;
import org.springframework.stereotype.Service;
import sun.nio.cs.ext.IBM037;
import java.io.IOException;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* @Author: jja
* @Description:
* @Date: 2020/2/22 12:48
*/
@Service("hbaseServive")
public class HbaseService {
private static Connection connection = null;
private static Admin admin = null;
private static Configuration configuration;
/**
* 静态代码块,用于初始化Hbase连接
*/
static {
try {
//获取配置信息
configuration = HBaseConfiguration.create();
configuration.set("hbase.zookeeper.quorum", "192.168.1.100");
configuration.set("hbase.zookeeper.property.clientPort", "2181");
//创建连接
connection = ConnectionFactory.createConnection(configuration);
//创建Admin
admin = connection.getAdmin();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 获取HBase连接
*
* @return
*/
public Connection getConnection() {
return connection;
}
/**
* 获取Admin实例
*
* @return
*/
public Admin getAdmin() {
return admin;
}
public Table getTable(String tableName) throws IOException {
Table table = connection.getTable(TableName.valueOf(tableName));
return table;
}
/**
* 判断表名称是否存在
*
* @param tableName 表名称
* @return 是否存在
* @throws IOException
*/
public boolean isTableExist(String tableName) throws IOException {
return admin.tableExists(TableName.valueOf(tableName));
}
/**
* 创建表
*
* @param tableName 表名称
* @param cfs 可变 列簇
* @throws IOException
*/
public void createTable(String tableName, String... cfs) throws IOException {
//判断是否存在列簇信息
if (cfs.length <= 0) {
System.out.println("请设置列簇信息");
return;
}
if (isTableExist(tableName)) {
System.out.println(tableName + "表已经存在!");
return;
}
//创建表描述器
HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf(tableName));
//循环添加列簇
for (String cf : cfs) {
HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(cf);
hTableDescriptor.addFamily(hColumnDescriptor);
}
admin.createTable(hTableDescriptor);
}
/**
* 删除表
*
* @param tableName 表名称
* @throws IOException
*/
public void dropTable(String tableName) throws IOException {
//判断表是否存在
if (!isTableExist(tableName)) {
System.out.println(tableName + "表不存在");
return;
}
//使表下线
admin.disableTable(TableName.valueOf(tableName));
//删除表
admin.deleteTable(TableName.valueOf(tableName));
}
/**
* 创建命名空间
*
* @param ns 命名空间
*/
public void createNameSpace(String ns) {
//创建命名空间描述器
NamespaceDescriptor namespaceDescriptor = NamespaceDescriptor.create(ns).build();
try {
admin.createNamespace(namespaceDescriptor);
// 列出所有存在的命名空间
// NamespaceDescriptor[] namespaceDescriptors = admin.listNamespaceDescriptors();
// for (NamespaceDescriptor descriptor : namespaceDescriptors) {
// String name = descriptor.getName();
// System.out.println(name+ "------------------");
// }
} catch (NamespaceExistException e) {
System.out.println("命名空间" + ns + "已经存在");
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 列出所有的命名空间
*
* @return 命名空间列表
* @throws Exception
*/
List<String> getAllNamespaces() throws Exception {
NamespaceDescriptor[] namespaceDescriptors = admin.listNamespaceDescriptors();
List<String> namespaceList = new ArrayList<>();
for (NamespaceDescriptor descriptor : namespaceDescriptors) {
namespaceList.add(descriptor.getName());
}
// for (String namespace : namespaceList) {
// System.out.println("namespace: " + namespace);
// }
return namespaceList;
}
/**
* 获取所有的表名称 支持正则
*
* @return list 的表名称
* @throws Exception
*/
private List<String> getAllTables() throws Exception {
NamespaceDescriptor[] namespaceDescriptors = admin.listNamespaceDescriptors();
// 可以进行正则匹配
HTableDescriptor[] hTableDescriptors = admin.listTables();
List<String> allTableNames = new ArrayList<>();
for (HTableDescriptor hTableDescriptor : hTableDescriptors) {
// 获取名称
allTableNames.add(hTableDescriptor.getNameAsString());
}
return allTableNames;
}
/****
* 使用scan查询所有数据
* @param tableName
*/
Map<String, Map<String, String>> getPageData(String tableName, int pageNum, int pageSize) throws IOException {
final long l1 = System.currentTimeMillis();
int i = 0;
HTable table = new HTable(configuration, Bytes.toBytes(tableName));
Map<String, Map<String, String>> map = new HashMap<>();
HashMap<String, String> familyName = new HashMap<>();
try {
// 展示所有的列簇 可省略
HTableDescriptor hTableDescriptor = table.getTableDescriptor();
for (int ii = 0; ii < hTableDescriptor.getColumnFamilies().length; ii++) {
familyName.put("familyName_" + ii, hTableDescriptor.getColumnFamilies()[ii].getNameAsString());
}
//Scan所有数据
Scan scan = new Scan();
Integer firstPage = pageNum * pageSize;
Integer endPage = pageNum * (pageSize + 1);
scan.setMaxVersions(100);
ResultScanner rss = table.getScanner(scan);
loop:
while (rss.next() != null) {
for (KeyValue kv : rss.next().raw()) {
if (i >= firstPage && i < endPage) {
HashMap<String, String> valueMap = new HashMap<>();
valueMap.put("rowKey", new String(kv.getRow()));
valueMap.put("family", new String(kv.getFamily()));
valueMap.put("qualifier", new String(kv.getQualifier()));
valueMap.put("timestamp", String.valueOf(kv.getTimestamp()));
valueMap.put("value", new String(kv.getValue()));
map.put(kv.getRow() + "_" + String.valueOf(kv.getTimestamp()), valueMap);
}
i++;
if (i >= endPage) {
break loop;
}
}
}
rss.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
map.put("familyName", familyName);
i = 0;
System.out.println("time" + (System.currentTimeMillis() - l1));
return map;
}
/**
* 获取某个nameSpace下的所有tableName
*
* @param nameSpace 命名空间
* @return 表名
* @throws Exception
*/
List<String> getAllTableNamesOfNameSpace(String nameSpace) throws Exception {
HTableDescriptor[] hTableDescriptors = admin.listTableDescriptorsByNamespace(nameSpace);
List<String> tableNames = new ArrayList<>();
for (HTableDescriptor hTableDescriptor : hTableDescriptors) {
tableNames.add(hTableDescriptor.getNameAsString());
}
return tableNames;
}
/**
* 查询数据
*
* @param tableName 表名称
* @param rowKey 行键
* @param columnFamily 列簇名称
* @param column 列名
* @throws IOException
*/
public void getData(String tableName, String rowKey, String columnFamily, String column) throws IOException {
//获取表名称
Table table = connection.getTable(TableName.valueOf(tableName));
//创建get对象
// 将每个rowKey传递进去进行 查询
final byte[] row = Bytes.toBytes(rowKey);
Get get = new Get(row);
get.addColumn(Bytes.toBytes(columnFamily), Bytes.toBytes(column));
get.setMaxVersions(100);
Result result = table.get(get);
String value;
String rowkey;
String qualifierName;
String familyName;
long timestamp;
final List<Cell> cells = result.listCells();
for (Cell cell : cells) {
value = Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
rowkey = Bytes.toString(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength());
qualifierName = Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength());
familyName = Bytes.toString(cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength());
timestamp = cell.getTimestamp();
// rowKey
System.out.println("rowkey: " + rowkey);
// 列簇
System.out.println("familyName: " + familyName);
// 列名
System.out.println("qualifierName: " + qualifierName);
// 值
System.out.println("value: " + value);
// 时间戳 版本
System.out.println("timestamp: " + timestamp);
}
}
public List<String> queryTableTestBatch(List<String> rowkeyList) throws IOException {
List<Get> getList = new ArrayList();
String tableName = "student1";
List<String> list = new ArrayList<>();
Table table = connection.getTable(TableName.valueOf(tableName));// 获取表
for (String rowkey : rowkeyList) {//把rowkey加到get里,再把get装到list中
final byte[] row = Bytes.toBytes(rowkey);
final Get get = new Get(row);
get.setMaxVersions(100);
getList.add(get);
}
final Result[] results = table.get(getList);//重点在这,直接查getList<Get>
for (Result result : results) {//对返回的结果集进行操作
for (Cell cell : result.rawCells()) {
String value = Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength());
String rowkey = Bytes.toString(cell.getRowArray(), cell.getRowOffset(), cell.getRowLength());
String qualifierName = Bytes.toString(cell.getQualifierArray(), cell.getQualifierOffset(), cell.getQualifierLength());
String familyName = Bytes.toString(cell.getFamilyArray(), cell.getFamilyOffset(), cell.getFamilyLength());
long timestamp = cell.getTimestamp();
System.out.println("rowkey: " + rowkey);
System.out.println("familyName: " + familyName);
System.out.println("qualifierName: " + qualifierName);
System.out.println("value: " + value);
System.out.println("timestamp: " + timestamp);
list.add(value);
}
}
return list;
}
/**
* 获取一个表名的所有原始信息
*
* @param tableName 表名称
* @throws Exception
*/
public void getNoDealData(String tableName) throws Exception {
Table table = connection.getTable(TableName.valueOf(tableName));
Scan scan = new Scan();
scan.setMaxVersions(10);
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
System.out.println("scan: " + result);
}
}
}