用法示例:
HBaseAdmin admin = new HBaseAdmin(config);
admin.disableTable("tablename");
用法示例:
HBaseConfiguration hconfig = new HBaseConfiguration();
hconfig.set("hbase.zookeeper.property.clientPort","2181");
用法示例:
HTableDescriptor htd = new HTableDescriptor(table);
htd.addFamily(new HcolumnDescriptor("family");
在上述例子中,通过一个HTableDescriptor 实例,为HTableDescriptor 添加了一个列簇:family
用法示例:
HTableDescriptor htd = new HTableDescriptor(tablename);
HColumnDescriptor col = new HColumnDescriptor("content");
htd.addFamily(col);
此例添加了一个content列簇
用法示例:
HTable table = new HTable(conf,Bytes.toBytes(tablename));
ResultScanner scanner = table.getScanner(family);
用法示例:
HTable table = new HTable(conf,Bytes.toBytes(tablename));
Put put = new Put(brow); // 为指定行创建一个Put操作
put.add(family,qualifier,value);
table.Put(put);
用法示例:
HTable table = new HTabel(conf,Bytes.toBytes(tablename));
Get get = new Get(Bytes.toBytes(row));
<dependencies>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.4.6</version>
</dependency>
</dependencies>
package com.liangzai.hbase;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import java.io.IOException;
public class Demo01TestAPI {
public static void main(String[] args) throws IOException {
// 1、创建配置文件,设置HBase的连接地址(ZK的地址)
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum","master:2181,node1:2181,node2:2181");
// 2、建立连接
Connection conn = ConnectionFactory.createConnection(conf);
/**
* 3、执行操作:
* 对表的结构进行操作 则getAdmin
* 对表的数据进行操作 则getTable
*/
Admin admin = conn.getAdmin();
conn.getTable(TableName.valueOf("test5"));
}
}
Connection conn;
@Before
// 建立连接
public void init() throws IOException {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "192.168.3.100:2181,192.168.3.101:2181,192.168.3.102:2181");
conn = ConnectionFactory.createConnection(conf);
}
@Test
// cerate table
public void createTable() throws IOException {
Admin admin = conn.getAdmin();
HTableDescriptor testAPI = new HTableDescriptor(TableName.valueOf("testAPI"));
// 创建列簇
HColumnDescriptor cf1 = new HColumnDescriptor("cf1");
// 对列簇进行配置
cf1.setMaxVersions(3);
// 给testAPI表添加一个列簇
testAPI.addFamily(cf1);
// 创建testAPI表
admin.createTable(testAPI);
}
@Test
// 查看所有表
public void ListTbales() throws IOException {
Admin admin = conn.getAdmin();
TableName[] tableNames = admin.listTableNames();
for (TableName tableName : tableNames) {
System.out.println(tableName.getNameAsString());
}
}
@Test
// desc查看表结构
public void getTableDescriptor() throws IOException {
Admin admin = conn.getAdmin();
HTableDescriptor testAPI = admin.getTableDescriptor(TableName.valueOf("testAPI"));
HColumnDescriptor[] cfs = testAPI.getColumnFamilies();
for (HColumnDescriptor cf : cfs) {
System.out.println(cf.getNameAsString());
System.out.println(cf.getMaxVersions());
System.out.println(cf.getTimeToLive());
}
}
@Test
// alter
// 对testAPI 将cf1的版本设置为5,并且新加一个列簇cf2
public void AlterTbale() throws IOException {
Admin admin = conn.getAdmin();
TableName testAPI = TableName.valueOf("testAPI");
HTableDescriptor testAPIDesc = admin.getTableDescriptor(testAPI);
HColumnDescriptor[] cfs = testAPIDesc.getColumnFamilies();
for (HColumnDescriptor cf : cfs) {
if ("cf1".equals(cf.getNameAsString())) {
cf.setMaxVersions(5);
}
}
testAPIDesc.addFamily(new HColumnDescriptor("cf2"));
admin.modifyTable(testAPI, testAPIDesc);
}
@Test
// drop
public void DropTable() throws IOException {
Admin admin = conn.getAdmin();
TableName tableName = TableName.valueOf("test2");
if (admin.tableExists(tableName)) {
// 表在删除之前需要先disable
admin.disableTable(tableName);
admin.deleteTable(tableName);
} else {
System.out.println("表不存在!");
}
}
@Test
// put
public void PutData() throws IOException {
Table testAPI = conn.getTable(TableName.valueOf("testAPI"));
Put put = new Put("001".getBytes());
put.addColumn("cf1".getBytes(), "name".getBytes(), "张三".getBytes());
put.addColumn("cf1".getBytes(), "age".getBytes(), "18".getBytes());
put.addColumn("cf1".getBytes(), "clazz".getBytes(), "文科一班".getBytes());
put.addColumn("cf1".getBytes(), "clazz".getBytes(), 1, "文科二班".getBytes());
testAPI.put(put);
}
@Test
// get
public void GetData() throws IOException {
Table testAPI = conn.getTable(TableName.valueOf("testAPI"));
Get get = new Get("001".getBytes());
get.setMaxVersions(10);
Result rs = testAPI.get(get);
// 获取rowkey
byte[] row = rs.getRow();
byte[] name = rs.getValue("cf1".getBytes(), "name".getBytes());
byte[] age = rs.getValue("cf1".getBytes(), "age".getBytes());
byte[] clazz = rs.getValue("cf1".getBytes(), "clazz".getBytes());
System.out.println(Bytes.toString(row) + "," + Bytes.toString(name) + "," + Bytes.toString(age) + "," + Bytes.toString(clazz));
}
@Test
// 提取数据的另一种方式
public void ListCells() throws IOException {
Table testAPI = conn.getTable(TableName.valueOf("testAPI"));
Get get = new Get("001".getBytes());
get.setMaxVersions(10);
Result rs = testAPI.get(get);
// 获取所有的Cell
List<Cell> cells = rs.listCells();
for (Cell cell : cells) {
String value = Bytes.toString(CellUtil.cloneValue(cell));
System.out.println(value);
}
}
@Test
/**
* 创建stu表,增加一个info列簇,将students.txt的1000条数据全部插入
*/ public void PutStu() throws IOException {
TableName stu = TableName.valueOf("stu");
// 创建表
Admin admin = conn.getAdmin();
if (!admin.tableExists(stu)) {
admin.createTable(new HTableDescriptor(stu).addFamily(new HColumnDescriptor("info")));
}
Table stuTable = conn.getTable(stu);
ArrayList<Put> puts = new ArrayList<>();
// 读取文件
BufferedReader br = new BufferedReader(new FileReader("data/students.txt"));
int cnt = 0;
String line;
while ((line = br.readLine()) != null) {
String[] split = line.split(",");
String id = split[0];
String name = split[1];
String age = split[2];
String gender = split[3];
String clazz = split[4];
Put put = new Put(id.getBytes());
put.addColumn("info".getBytes(),"name".getBytes(),name.getBytes());
put.addColumn("info".getBytes(),"age".getBytes(),age.getBytes());
put.addColumn("info".getBytes(),"gender".getBytes(),gender.getBytes());
put.addColumn("info".getBytes(),"clazz".getBytes(),clazz.getBytes());
// 批量插入
puts.add(put);
cnt += 1;
if (cnt == 100) {
stuTable.put(puts);
puts.clear(); // 清空
cnt = 0;
}
// 逐条插入,效率低
// stuTable.put(put);
}
// 判断Put的List是否为空
if (!puts.isEmpty()) {
stuTable.put(puts);
}
br.close();
}
@Test
// delete
public void DeleteData() throws IOException {
Table stuTable = conn.getTable(TableName.valueOf("stu"));
Delete del = new Delete("1500100001".getBytes());
stuTable.delete(del);
}
package com.liangzai.hbase;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class Demo02API {
Connection conn;
@Before
// 建立连接
public void init() throws IOException {
Configuration conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "192.168.3.100:2181,192.168.3.101:2181,192.168.3.102:2181");
conn = ConnectionFactory.createConnection(conf);
}
@Test
// cerate table
public void createTable() throws IOException {
Admin admin = conn.getAdmin();
HTableDescriptor testAPI = new HTableDescriptor(TableName.valueOf("testAPI"));
// 创建列簇
HColumnDescriptor cf1 = new HColumnDescriptor("cf1");
// 对列簇进行配置
cf1.setMaxVersions(3);
// 给testAPI表添加一个列簇
testAPI.addFamily(cf1);
// 创建testAPI表
admin.createTable(testAPI);
}
@Test
// 查看所有表
public void ListTbales() throws IOException {
Admin admin = conn.getAdmin();
TableName[] tableNames = admin.listTableNames();
for (TableName tableName : tableNames) {
System.out.println(tableName.getNameAsString());
}
}
@Test
// desc查看表结构
public void getTableDescriptor() throws IOException {
Admin admin = conn.getAdmin();
HTableDescriptor testAPI = admin.getTableDescriptor(TableName.valueOf("testAPI"));
HColumnDescriptor[] cfs = testAPI.getColumnFamilies();
for (HColumnDescriptor cf : cfs) {
System.out.println(cf.getNameAsString());
System.out.println(cf.getMaxVersions());
System.out.println(cf.getTimeToLive());
}
}
@Test
// alter
// 对testAPI 将cf1的版本设置为5,并且新加一个列簇cf2
public void AlterTbale() throws IOException {
Admin admin = conn.getAdmin();
TableName testAPI = TableName.valueOf("testAPI");
HTableDescriptor testAPIDesc = admin.getTableDescriptor(testAPI);
HColumnDescriptor[] cfs = testAPIDesc.getColumnFamilies();
for (HColumnDescriptor cf : cfs) {
if ("cf1".equals(cf.getNameAsString())) {
cf.setMaxVersions(5);
}
}
testAPIDesc.addFamily(new HColumnDescriptor("cf2"));
admin.modifyTable(testAPI, testAPIDesc);
}
@Test
// drop
public void DropTable() throws IOException {
Admin admin = conn.getAdmin();
TableName tableName = TableName.valueOf("test2");
if (admin.tableExists(tableName)) {
// 表在删除之前需要先disable
admin.disableTable(tableName);
admin.deleteTable(tableName);
} else {
System.out.println("表不存在!");
}
}
@Test
// put
public void PutData() throws IOException {
Table testAPI = conn.getTable(TableName.valueOf("testAPI"));
Put put = new Put("001".getBytes());
put.addColumn("cf1".getBytes(), "name".getBytes(), "张三".getBytes());
put.addColumn("cf1".getBytes(), "age".getBytes(), "18".getBytes());
put.addColumn("cf1".getBytes(), "clazz".getBytes(), "文科一班".getBytes());
put.addColumn("cf1".getBytes(), "clazz".getBytes(), 1, "文科二班".getBytes());
testAPI.put(put);
}
@Test
// get
public void GetData() throws IOException {
Table testAPI = conn.getTable(TableName.valueOf("testAPI"));
Get get = new Get("001".getBytes());
get.setMaxVersions(10);
Result rs = testAPI.get(get);
// 获取rowkey
byte[] row = rs.getRow();
byte[] name = rs.getValue("cf1".getBytes(), "name".getBytes());
byte[] age = rs.getValue("cf1".getBytes(), "age".getBytes());
byte[] clazz = rs.getValue("cf1".getBytes(), "clazz".getBytes());
System.out.println(Bytes.toString(row) + "," + Bytes.toString(name) + "," + Bytes.toString(age) + "," + Bytes.toString(clazz));
}
@Test
// 提取数据的另一种方式
public void ListCells() throws IOException {
Table testAPI = conn.getTable(TableName.valueOf("testAPI"));
Get get = new Get("001".getBytes());
get.setMaxVersions(10);
Result rs = testAPI.get(get);
// 获取所有的Cell
List<Cell> cells = rs.listCells();
for (Cell cell : cells) {
String value = Bytes.toString(CellUtil.cloneValue(cell));
System.out.println(value);
}
}
@Test
/**
* 创建stu表,增加一个info列簇,将students.txt的1000条数据全部插入
*/ public void PutStu() throws IOException {
TableName stu = TableName.valueOf("stu");
// 创建表
Admin admin = conn.getAdmin();
if (!admin.tableExists(stu)) {
admin.createTable(new HTableDescriptor(stu).addFamily(new HColumnDescriptor("info")));
}
Table stuTable = conn.getTable(stu);
ArrayList<Put> puts = new ArrayList<>();
// 读取文件
BufferedReader br = new BufferedReader(new FileReader("data/students.txt"));
int cnt = 0;
String line;
while ((line = br.readLine()) != null) {
String[] split = line.split(",");
String id = split[0];
String name = split[1];
String age = split[2];
String gender = split[3];
String clazz = split[4];
Put put = new Put(id.getBytes());
put.addColumn("info".getBytes(),"name".getBytes(),name.getBytes());
put.addColumn("info".getBytes(),"age".getBytes(),age.getBytes());
put.addColumn("info".getBytes(),"gender".getBytes(),gender.getBytes());
put.addColumn("info".getBytes(),"clazz".getBytes(),calzz.getBytes());
// 批量插入
puts.add(put);
cnt += 1;
if (cnt == 100) {
stuTable.put(puts);
puts.clear(); // 清空
cnt = 0;
}
// 逐条插入,效率低
// stuTable.put(put);
}
// 判断Put的List是否为空
if (!puts.isEmpty()) {
stuTable.put(puts);
}
br.close();
}
@Test
// delete
public void DeleteData() throws IOException {
Table stuTable = conn.getTable(TableName.valueOf("stu"));
Delete del = new Delete("1500100001".getBytes());
stuTable.delete(del);
}
@After
public void close() throws IOException {
conn.close();
}
}
电信案例
@Test
// 将数据写入HBase
public void putALL() throws IOException {
Table dx_tb = conn.getTable(dianxin);
ArrayList<Put> puts = new ArrayList<>();
int cnt = 0;
int batchSize = 1000;
BufferedReader br = new BufferedReader(new FileReader("data/DIANXIN.csv"));
String line;
while ((line = br.readLine()) != null) {
String[] split = line.split(",");
String mdn = split[0];
String start_time = split[1];
String lg = split[4];
String lat = split[5];
Put put = new Put(mdn.getBytes());
put.addColumn("cf1".getBytes(),"lg".getBytes(),Long.parseLong(start_time), lg.getBytes());
put.addColumn("cf1".getBytes(),"lat".getBytes(),Long.parseLong(start_time),lat.getBytes());
puts.add(put);
cnt += 1;
if (cnt == batchSize) {
dx_tb.put(puts);
puts.clear();
cnt = 0;
}
}
if (!puts.isEmpty()) {
dx_tb.put(puts);
}
br.close();
}
@Test
// 根据mdn获取用户最新的3个位置
public void getPositionByMdn() throws IOException {
Table dx_tb = conn.getTable(dianxin);
String mdn = "48E938D94E866A716C86642C5E9AF179DA658A07";
Get get = new Get(mdn.getBytes());
get.setMaxVersions(3);
Result rs = dx_tb.get(get);
ArrayList<String> lgArr = new ArrayList<>();
ArrayList<String> latArr = new ArrayList<>();
for (Cell cell : rs.listCells()) {
String value = Bytes.toString(CellUtil.cloneValue(cell));
if ("lg".equals(Bytes.toString(CellUtil.cloneQualifier(cell)))) {
lgArr.add(value);
} else if ("lat".equals(Bytes.toString(CellUtil.cloneQualifier(cell)))) {
latArr.add(value);
}
}
for (int i = 0; i < 3; i++) {
System.out.println(lgArr.get(i) + "," + latArr.get(i));
}
}
到底啦!关注靓仔学习更多的大数据知识!😊
因篇幅问题不能全部显示,请点此查看更多更全内容