1.pom.xml引入相关jar包
<dependency>
<groupId>org.apache.hbasegroupId>
<artifactId>hbase-clientartifactId>
<version>2.4.2version>
dependency>
<dependency>
<groupId>org.apache.hbasegroupId>
<artifactId>hbase-commonartifactId>
<version>2.4.2version>
dependency>
<dependency>
<groupId>org.apache.hbasegroupId>
<artifactId>hbase-serverartifactId>
<version>2.4.2version>
dependency>
<dependency>
<groupId>org.apache.hbasegroupId>
<artifactId>hbase-protocolartifactId>
<version>2.4.2version>
<type>pomtype>
<exclusions>
<exclusion>
<groupId>org.glassfishgroupId>
<artifactId>javax.elartifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.apache.hbasegroupId>
<artifactId>hbase-mapreduceartifactId>
<version>2.4.2version>
dependency>
<dependency>
<groupId>org.apache.hbasegroupId>
<artifactId>hbase-zookeeperartifactId>
<version>2.4.2version>
dependency>
2.nameSpace的创建、查询、删除
package cn.tedu.hbase;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.NamespaceDescriptor;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
public class NameSpaceDemo {
private Connection connection;
private Admin admin;
@Before
public void connect() throws IOException {
//获取HBase的配置
Configuration conf = HBaseConfiguration.create();
//配置Zookeeper的连接地址
conf.set("hbase.zookeeper.quorum","hadoop01:2181,hadoop01:2181,hadoop01:2181");
//发起连接
connection = ConnectionFactory.createConnection(conf);
//获取管理权
admin = connection.getAdmin();
}
//创建空间
@Test
public void createNameSpace() throws IOException {
//构建空间描述器
NamespaceDescriptor descriptor =NamespaceDescriptor.create("demo0").build();
//创建空间
admin.createNamespace(descriptor);
}
//获取空间下所有的空间
@Test
public void listNamespace() throws IOException {
//获取所有的名称空间
String[] namespaces = admin.listNamespaces();
for (String namespace :namespaces){
System.out.println(namespace);
}
}
//删除空间
@Test
public void deleteNamespace() throws IOException {
admin.deleteNamespace("demo");
}
@After
public void close() throws IOException {
//关闭管理权
admin.close();
//关闭连接
connection.close();
}
}
3.table的增删改查
package cn.tedu.hbase;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.CompareOperator;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.RegexStringComparator;
import org.apache.hadoop.hbase.filter.ValueFilter;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.NavigableMap;
public class TableDemo {
private Connection connection;
private Admin admin;
private Table users;
@Before
public void connect() throws IOException {
//获取HBase的配置
Configuration conf = HBaseConfiguration.create();
//配置Zookeeper的连接地址
conf.set("hbase.zookeeper.quorum","hadoop01:2181,hadoop01:2181,hadoop01:2181");
//发起连接
connection = ConnectionFactory.createConnection(conf);
//获取管理权
admin = connection.getAdmin();
//指定要 *** 作的表
users = connection.getTable(TableName.valueOf("users"));
}
//创建表
@Test
public void createTable() throws IOException {
//构建列簇描述器
ColumnFamilyDescriptor cf1 =ColumnFamilyDescriptorBuilder.newBuilder("basic".getBytes(StandardCharsets.UTF_8)).build();
ColumnFamilyDescriptor cf2 =ColumnFamilyDescriptorBuilder.newBuilder("info".getBytes(StandardCharsets.UTF_8)).build();
//构建表描述器
TableDescriptor table = TableDescriptorBuilder.newBuilder(TableName.valueOf("users")).setColumnFamily(cf1).setColumnFamily(cf2).build();
//创建表
admin.createTable(table);
}
//添加数据
@Test
public void appendData() throws IOException {
//构建Append对象
Append append = new Append("u1".getBytes(StandardCharsets.UTF_8));
//指定列族
byte[] basic = "basic".getBytes(StandardCharsets.UTF_8);
byte[] info = "info".getBytes(StandardCharsets.UTF_8);
//指定列和数据
append.addColumn(basic,"name".getBytes(StandardCharsets.UTF_8),"David".getBytes(StandardCharsets.UTF_8));
append.addColumn(basic,"age".getBytes(StandardCharsets.UTF_8),"25".getBytes(StandardCharsets.UTF_8));
append.addColumn(basic,"gender".getBytes(StandardCharsets.UTF_8),"male".getBytes(StandardCharsets.UTF_8));
append.addColumn(basic,"address".getBytes(StandardCharsets.UTF_8),"beijing".getBytes(StandardCharsets.UTF_8));
//添加数据
users.append(append);
}
//添加/修改数据
@Test
public void putData() throws IOException {
//构建put对象
Put put = new Put("u1".getBytes(StandardCharsets.UTF_8));
//指定列和数据
put.addColumn("basic".getBytes(StandardCharsets.UTF_8),"password".getBytes(StandardCharsets.UTF_8),"123456".getBytes(StandardCharsets.UTF_8));
//添加数据
users.put(put);
}
//添加百万数据(花费了9889毫秒)
@Test
public void putMillionData() throws IOException {
//指定列族
byte[] basic = "basic".getBytes(StandardCharsets.UTF_8);
//指定列
byte[] password = "password".getBytes(StandardCharsets.UTF_8);
//构建集合实现批量 *** 作
List<Put> puts = new ArrayList<>();
long begin = System.currentTimeMillis();
//添加百万条数据
for (int i = 1000000; i < 2000000; i++) {
//构建Put对象
Put put = new Put(("u" + i).getBytes(StandardCharsets.UTF_8));
//添加数据
put.addColumn(basic,password,getPassword());
//将put对象放到puts集合中
puts.add(put);
//每1000条向HBase中添加一次
if (puts.size() >= 1000){
users.put(puts);
//清空集合
puts.clear();
}
}
//添加数据
users.put(puts);
//记录结束时间
long end = System.currentTimeMillis();
System.out.println(end - begin);
}
//生成六位大写字母的随机密码
private byte[] getPassword(){
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 6; i++) {
char c = (char) (Math.random()*26+65);
sb.append(c);
}
return sb.toString().getBytes(StandardCharsets.UTF_8);
}
//读取一行数据 - get 'users' , 'u1'
@Test
public void getLine() throws IOException {
//封装Get对象
Get get = new Get("u1".getBytes(StandardCharsets.UTF_8));
//查询数据,获取结果
Result result = users.get(get);
//获取一行数据
NavigableMap<byte[], NavigableMap<byte[], NavigableMap<Long,byte[]>>> families = result.getMap();
for (Map.Entry<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> family : families.entrySet()) {
//键表示的是列族名字
System.out.println("column Family:" + new String(family.getKey()));
//值表示的是实际数据
NavigableMap<byte[], NavigableMap<Long, byte[]>> columns = family.getValue();
for (Map.Entry<byte[], NavigableMap<Long, byte[]>> column : columns.entrySet()) {
//键表示的是列名
System.out.println("\tColumn:" + new String(column.getKey()));
//值表示的是实际数据
NavigableMap<Long, byte[]> values = column.getValue();
for (Map.Entry<Long, byte[]> value : values.entrySet()) {
//键表示的是时间戳
System.out.println("\t\tTimestamp:" + value.getKey());
//值表示的是实际的值
System.out.println("\t\tValue:" + new String(value.getValue()));
}
}
}
}
//获取指定行间列祖的数据 - get 'users' , 'u1' , 'basic'
@Test
public void getFamily() throws IOException {
//封装Get对象
Get get = new Get("u1".getBytes(StandardCharsets.UTF_8));
//指定列祖
byte[] basic = "basic".getBytes(StandardCharsets.UTF_8);
get.addFamily(basic);
//查询数据,获取结果
Result result = users.get(get);
//获取数据
NavigableMap<byte[], byte[]> columns = result.getFamilyMap(basic);
//遍历
for (Map.Entry<byte[], byte[]> column : columns.entrySet()) {
//键是列族中包含的列名,值是实际数据
System.out.println(new String(column.getKey()) + "=" + new String(column.getValue()));
}
}
//获取指定行间列祖的数据 - get 'users' , 'u1' , 'basic:password'
@Test
public void getColumnData() throws IOException {
//封装Get对象
Get get = new Get("u1".getBytes(StandardCharsets.UTF_8));
//指定列祖
byte[] basic = "basic".getBytes(StandardCharsets.UTF_8);
//指定列
byte[] password = "password".getBytes(StandardCharsets.UTF_8);
get.addColumn(basic, password);
//查询数据,获取结果
Result result = users.get(get);
//获取实际数据
byte[] value = result.getValue(basic, password);
System.out.println(new String(value));
}
//遍历数据 - scan 'users'
@Test
public void scan() throws IOException {
//封装Scan对象
Scan scan = new Scan();
//获取结果集
ResultScanner rs = users.getScanner(scan);
//指定列祖
byte[] basic = "basic".getBytes(StandardCharsets.UTF_8);
//指定列
byte[] password = "password".getBytes(StandardCharsets.UTF_8);
for (Result r : rs) {
byte[] value = r.getValue(basic, password);
System.out.println(new String(value));
}
}
//遍历过程中过滤数据
@Test
public void filter() throws IOException {
//封装Scan对象
Scan scan = new Scan();
//封装Filter对象
Filter filter = new ValueFilter(CompareOperator.EQUAL, new RegexStringComparator(".*AAA.*"));
//设置过滤器
scan.setFilter(filter);
//获取结果集
ResultScanner rs = users.getScanner(scan);
//指定列祖
byte[] basic = "basic".getBytes(StandardCharsets.UTF_8);
//指定列
byte[] password = "password".getBytes(StandardCharsets.UTF_8);
for (Result r : rs) {
byte[] value = r.getValue(basic, password);
System.out.println(new String(value));
}
}
//删除数据 - 删除指定行键的指定列
@Test
public void deleteData() throws IOException {
//封装Delete对象
Delete del = new Delete("u1".getBytes(StandardCharsets.UTF_8));
//指定列
del.addColumn("basic".getBytes(StandardCharsets.UTF_8),"age".getBytes(StandardCharsets.UTF_8));
//删除数据
users.delete(del);
}
//删除数据 - 删除指定行键列族的数据
@Test
public void deleteFamily() throws IOException {
//封装Delete对象
Delete del = new Delete("u1".getBytes(StandardCharsets.UTF_8));
//指定列
del.addFamily("info".getBytes(StandardCharsets.UTF_8));
//删除数据
users.delete(del);
}
//删除数据 - 删除指定行数据
@Test
public void deleteLine() throws IOException {
//封装Delete对象
Delete del = new Delete("u1".getBytes(StandardCharsets.UTF_8));
//删除数据
users.delete(del);
}
//删除表
@Test
public void deleteTable() throws IOException {
//禁用表
admin.disableTable(TableName.valueOf("users"));
//删除表
admin.deleteTable(TableName.valueOf("users"));
}
@After
public void close() throws IOException {
//关闭管理权
admin.close();
//关闭连接
connection.close();
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)