package com.ycit.hbase.test;
import java.io.IOException;
import java.util.ArrayList;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.Before;
import org.junit.Test;
/**
* @author 江鹏飞
* javaApi操作hbase
*/
public class HbaseTest {
static Configuration config = null;
private Connection connection = null;
private Table table = null;
// 配置连接信息
@Before
public void init() throws Exception{
//创建连接
config = HBaseConfiguration.create();
config.set("hbase.zookeeper.quorum", "mini1,mini2,mini3");// zookeeper地址
config.set("hbase.zookeeper.property.clientPort", "2181");// zookeeper端口
connection = ConnectionFactory.createConnection(config);
table = connection.getTable(TableName.valueOf("user"));
}
/**
* hbase 创建一个表
* @throws IOException
* @throws ZooKeeperConnectionException
* @throws MasterNotRunningException
*/
@Test
public void createTable() throws Exception{
HBaseAdmin admin = new HBaseAdmin(config); //创建表管理
//创建表表述信息
TableName tableName = TableName.valueOf("test"); //表名称
HTableDescriptor desc = new HTableDescriptor(tableName);
//创建列族的表述类
HColumnDescriptor family_info1 = new HColumnDescriptor("info1");
//将列族添加到表中
desc.addFamily(family_info1);
HColumnDescriptor family_info2 = new HColumnDescriptor("info2");
desc.addFamily(family_info2);
//创建表
admin.createTable(desc);
System.out.println("表创建成功。。。。。");
}
/**
* 删除表
* @throws IOException
* @throws ZooKeeperConnectionException
* @throws MasterNotRunningException
*/
@Test
@SuppressWarnings("deprecation")
public void deleteTable() throws Exception{
HBaseAdmin admin = new HBaseAdmin(config); //创建表管理
admin.disableTable("test");
admin.deleteTable("test");
admin.close();
System.out.println("success to delete table ....... ");
}
/**
* 单条添加数据到hbase
* @throws IOException
*/
@Test
public void insertData() throws IOException{
Put put = new Put(Bytes.toBytes("jiangpengfei"));
put.add(Bytes.toBytes("info1"), Bytes.toBytes("airen"), Bytes.toBytes("zhanghuan"));
table.put(put);
}
/**
* 批量插入数据到hbase
* @throws IOException
*/
@Test
public void insertDatas() throws IOException{
ArrayList<Put> list = new ArrayList<>();
Put put1 = new Put(Bytes.toBytes("jiangpengfei22222"));
put1.add(Bytes.toBytes("info1"), Bytes.toBytes("age"), Bytes.toBytes("1000"));
Put put2 = new Put(Bytes.toBytes("jiangpengfei2222"));
put2.add(Bytes.toBytes("info1"), Bytes.toBytes("Address"), Bytes.toBytes("nanjing"));
list.add(put1);
list.add(put2);
table.put(list);
}
/**
* 删除数据
* @throws IOException
*/
@Test
public void deleteData() throws IOException{
Delete delete = new Delete(Bytes.toBytes("jiangpengfei2222"));
table.delete(delete);
}
/**
* 查询单条数据
* @throws IOException
*
*/
@Test
public void queryData() throws IOException{
Get get = new Get(Bytes.toBytes("jiangpengfei"));
Result result = table.get(get);
System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("info1"), Bytes.toBytes("age"))));
System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("info1"), Bytes.toBytes("Address"))));
System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("info1"), Bytes.toBytes("airen"))));
}
/**
*
* 全表扫描
* @throws IOException
*/
@Test
public void scanTable() throws IOException{
Scan scan = new Scan();
//扫描某个列族或者某个列
//scan.addFamily(Bytes.toBytes("info"));
//scan.addColumn(Bytes.toBytes("info"), Bytes.toBytes("password"));
//设置扫描的起始rowkey和结束rowkey(根据字典序进行扫描)
/* scan.setStartRow(Bytes.toBytes("jiangpengfei"));
scan.setStopRow(Bytes.toBytes("jiangpengfei1"));*/
ResultScanner scanner = table.getScanner(scan);
for(Result result:scanner){
System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("info1"), Bytes.toBytes("age"))));
System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("info1"), Bytes.toBytes("Address"))));
System.out.println(Bytes.toString(result.getValue(Bytes.toBytes("info1"), Bytes.toBytes("airen"))));
}
}
}
java API操作Hbase
猜你喜欢
转载自blog.csdn.net/qq_40068214/article/details/90143282
今日推荐
周排行