HBase的API
MAVEN:
<dependencies>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>0.99.2</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-server</artifactId>
<version>0.96.1-hadoop2</version>
</dependency>
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-examples</artifactId>
<version>0.98.13-hadoop2</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-client</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
</dependency>
</dependencies>
/**
* HBase的分布式数据库
*/
public class HBaseApi {
Configuration conf;
HBaseAdmin admin;
/**
* 获取连接
*/
@Before
public void connection() throws Exception{
//设置配置
conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum","hadoop01:2181,hadoop02:2181,hadoop03:2181");
admin = new HBaseAdmin(conf);
}
/**
* 创建表
* @throws IOException
*/
@Test
public void createTable() throws Exception{
HTableDescriptor table = new HTableDescriptor("user");
HColumnDescriptor colfam1 = new HColumnDescriptor("colfam1");
HColumnDescriptor colfam2 = new HColumnDescriptor("colfam2");
table.addFamily(colfam1);
table.addFamily(colfam2);
admin.createTable(table);
admin.close();
}
/**
* 单条数据插入
* @throws Exception
*/
@Test
public void insertOne() throws Exception{
Put put = new Put(Bytes.toBytes("line1"));
put.add(Bytes.toBytes("colfam1"),Bytes.toBytes("name"),Bytes.toBytes("何睿"));
//HTable与Hbase中的表对接
HTable table = new HTable(conf,"user");
table.put(put);
table.close();
}
/**
* 插入100万条 批量插入
* @throws Exception
*/
@Test
public void insert100W() throws Exception{
HTable table = new HTable(conf,"user");
List<Put> puts = new ArrayList<Put>();
int j = 0;
for (int i = 0; i < 1000000; i++) {
Put put = new Put(Bytes.toBytes("row"+i));
put.add(Bytes.toBytes("colfam2"),Bytes.toBytes("text"),Bytes.toBytes("value"+i));
puts.add(put);
if(i%10000==0){
table.put(puts);
puts= new ArrayList<Put>();
System.out.println(j);
j++;
}
}
System.out.println(puts.size());
table.put(puts);
table.close();
}
/**
* 获取单条记录
*/
@Test
public void getOne() throws Exception{
HTable table = new HTable(conf,"user");
Get get = new Get(Bytes.toBytes("row3"));
Result result = table.get(get);
byte[] value = result.getValue(Bytes.toBytes("colfam2"), Bytes.toBytes("text"));
System.out.println(new String(value));
}
/**
* 获取多条记录
*/
@Test
public void getMore() throws Exception{
HTable table = new HTable(conf,"user");
List<Get> gets = new ArrayList<Get>();
for (int i = 0; i < 3; i++) {
Get get = new Get(Bytes.toBytes("row"+i));
gets.add(get);
}
Result[] results = table.get(gets);
for (Result result : results) {
byte[] value = result.getValue(Bytes.toBytes("colfam2"), Bytes.toBytes("text"));
System.out.println(new String(value));
}
}
/**
* 扫描获取数据111113
*/
@Test
public void scan() throws Exception{
HTable table = new HTable(conf,"user");
Scan scan = new Scan();
scan.setStartRow(Bytes.toBytes("row0"));
scan.setStopRow(Bytes.toBytes("row4"));
ResultScanner scanner = table.getScanner(scan);
int i =1;
for (Result result : scanner) {
i++;
byte[] value = result.getValue(Bytes.toBytes("colfam2"), Bytes.toBytes("text"));
System.out.println(new String(value));
System.out.println(i);
}
}
/**
* 删除数据
*/
@Test
public void delete() throws Exception{
HTable table = new HTable(conf,"user");
Delete delete = new Delete(Bytes.toBytes("row1"));
delete.addFamily(Bytes.toBytes("colfam1"));
// delete.addColumn(Bytes.toBytes("colfam1"),Bytes.toBytes("age"));
table.delete(delete);
table.close();
}
/**
* 批量删除
*/
@Test
public void deleteMore() throws Exception{
HTable table = new HTable(conf,"user");
List<Delete> deletes = new ArrayList<Delete>();
for (int i = 5; i < 10; i++) {
Delete delete = new Delete(Bytes.toBytes("row"+i));
deletes.add(delete);
}
table.delete(deletes);
table.close();
}
/**
* 删除表
*/
@Test
public void dropTbale() throws Exception{
admin.disableTable("user");
admin.deleteTable("user");
}
}
//批处理 batch 和 batchCallback
@Test
public void batch() throws Exception{
Table user = conn.getTable(TableName.valueOf("user"));
List<Row> batch= new ArrayList<Row>();
Get get1 = new Get(Bytes.toBytes("224382618261914241"));
Put put1 = new Put(Bytes.toBytes("998"));
put1.add(Bytes.toBytes("info"),Bytes.toBytes("age"),Bytes.toBytes("22"));
Put put2 = new Put(Bytes.toBytes("999"));
put2.add(Bytes.toBytes("info"),Bytes.toBytes("age"),Bytes.toBytes("25"));
batch.add(get1);
batch.add(put1);
batch.add(put2);
Object[] results = new Object[batch.size()];
user.batch(batch,results);
for (int i = 0; i < results.length; i++) {
System.out.println("Result[" + i + "]: type = " +
results[i].getClass().getSimpleName() + "; " + results[i]);
}
}
user.batchCallback(batch, results, new Batch.Callback<Result>() {
public void update(byte[] bytes, byte[] row, Result result) {
System.out.println("Received callback for row[" + Bytes.toString(row) + "] -> " + result);
}
});
新版本API
package hbase;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.HTable;
import org.junit.Before;
import org.junit.Test;
import java.util.regex.Pattern;
public class NewApi {
Configuration conf ;
Connection conn;
/**
* 获取连接
* @throws Exception
*/
@Before
public void connection() throws Exception{
conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum","192.168.90.42:2181,192.168.90.43:2181,192.168.90.44:2181");
conn = ConnectionFactory.createConnection(conf);
System.out.println(conn);
}
/**
* 创建命名空间
* @throws Exception
*/
@Test
public void createNameSpace() throws Exception{
Admin admin = conn.getAdmin();
NamespaceDescriptor namespace = NamespaceDescriptor.create("NAMESPACE").build();
admin.createNamespace(namespace);
admin.close();
}
/**
* 创建表
*/
@Test
public void createTable() throws Exception{
Admin admin = conn.getAdmin();
TableName tableName = TableName.valueOf("NAMESPACE","TABLE1");
HTableDescriptor table = new HTableDescriptor(tableName);
HColumnDescriptor colfam1 = new HColumnDescriptor("colfam1");
HColumnDescriptor colfam2 = new HColumnDescriptor("colfam2");
table.addFamily(colfam1);
table.addFamily(colfam2);
admin.createTable(table);
admin.close();
}
@Test
public void list() throws Exception{
Admin admin = conn.getAdmin();
NamespaceDescriptor[] namespaceDescriptors = admin.listNamespaceDescriptors();
for (NamespaceDescriptor namespaceDescriptor : namespaceDescriptors) {
System.out.println(namespaceDescriptor.getName());
}
System.out.println("------------------------");
TableName[] tableNames = admin.listTableNamesByNamespace("school");
for (TableName tableName : tableNames) {
System.out.println(tableName.getNameAsString());
}
System.out.println("------------------------");
Pattern pattern = Pattern.compile("school:.*");
HTableDescriptor[] hTableDescriptors = admin.listTables(pattern);
for (HTableDescriptor hTableDescriptor : hTableDescriptors) {
System.out.println(hTableDescriptor);
}
}
}