标签:admin Bytes public API IOException DDL table HBase throws
所需pom文件
<dependency>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
<version>1.3.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
初始化配置
private static Configuration conf = null;
private static Connection conn = null;
private static Admin admin = null;
/**
* 初始化配置
*
* @throws IOException
*/
@Before
public void init() throws IOException {
conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "hdp1:2181,hdp2:2181,hdp3:2181");
conn = ConnectionFactory.createConnection(conf);
admin = conn.getAdmin();
}
判断是否有指定表
/**
* 判断表是否存在
*
* @throws IOException
*/
@Test
public void exist_table() throws IOException {
boolean b = admin.tableExists(TableName.valueOf("emp_table"));
System.out.println(b);
}
创建表
/**
* 创建表
*
* @throws IOException
*/
@Test
public void create_table() throws IOException {
HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf("emp_table"));
hTableDescriptor.addFamily(new HColumnDescriptor("info"));
hTableDescriptor.addFamily(new HColumnDescriptor("exp"));
admin.createTable(hTableDescriptor);
}
删除表
/**
* 删除表
*
* @throws IOException
*/
@Test
public void delete_table() throws IOException {
TableName tn = TableName.valueOf("student");
//下线表
admin.disableTable(tn);
//删除表
admin.deleteTable(tn);
}
插入数据
/**
* 插入数据
*
* @throws IOException
*/
@Test
public void put_data() throws IOException {
Table table = conn.getTable(TableName.valueOf("emp_table"));
Put put = new Put(Bytes.toBytes("10002"));
put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("lisi"));
table.put(put);
table.close();
}
获取数据
/**
* 获取数据
*
* @throws IOException
*/
@Test
public void get_data() throws IOException {
Table tab = conn.getTable(TableName.valueOf("emp_table"));
Get get = new Get(Bytes.toBytes("10001"));
get.addFamily(Bytes.toBytes("info"));
get.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"));
Result result = tab.get(get);
for (Cell cell : result.rawCells()) {
String family = Bytes.toString(CellUtil.cloneFamily(cell));
String qualifier = Bytes.toString(CellUtil.cloneQualifier(cell));
String value = Bytes.toString(CellUtil.cloneValue(cell));
System.out.println(family + "," + qualifier + "," + value);
}
tab.close();
}
创建命名空间
/**
* 创建命名空间
*/
@Test
public void create_namespace() {
try {
NamespaceDescriptor nsd = NamespaceDescriptor.create("senior6").build();
admin.createNamespace(nsd);
} catch (NamespaceExistException e) {
System.out.println("已存在");
} catch (IOException e) {
e.printStackTrace();
}
}
释放资源
/**
* 释放资源
*
* @throws IOException
*/
@After
public void disabled() throws IOException {
if (admin != null)
admin.close();
if (conn != null)
conn.close();
}
完整代码如下
package com.guantengyun.day0719;
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.IOException;
/**
* HBase API
* DDL操作
*/
public class HBaseClient {
private static Configuration conf = null;
private static Connection conn = null;
private static Admin admin = null;
/**
* 初始化配置
*
* @throws IOException
*/
@Before
public void init() throws IOException {
conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "hdp1:2181,hdp2:2181,hdp3:2181");
conn = ConnectionFactory.createConnection(conf);
admin = conn.getAdmin();
}
/**
* 判断表是否存在
*
* @throws IOException
*/
@Test
public void exist_table() throws IOException {
boolean b = admin.tableExists(TableName.valueOf("emp_table"));
System.out.println(b);
}
/**
* 创建表
*
* @throws IOException
*/
@Test
public void create_table() throws IOException {
HTableDescriptor hTableDescriptor = new HTableDescriptor(TableName.valueOf("emp_table"));
hTableDescriptor.addFamily(new HColumnDescriptor("info"));
hTableDescriptor.addFamily(new HColumnDescriptor("exp"));
admin.createTable(hTableDescriptor);
}
/**
* 删除表
*
* @throws IOException
*/
@Test
public void delete_table() throws IOException {
TableName tn = TableName.valueOf("student");
//下线表
admin.disableTable(tn);
//删除表
admin.deleteTable(tn);
}
/**
* 插入数据
*
* @throws IOException
*/
@Test
public void put_data() throws IOException {
Table table = conn.getTable(TableName.valueOf("emp_table"));
Put put = new Put(Bytes.toBytes("10002"));
put.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("lisi"));
table.put(put);
table.close();
}
/**
* 获取数据
*
* @throws IOException
*/
@Test
public void get_data() throws IOException {
Table tab = conn.getTable(TableName.valueOf("emp_table"));
Get get = new Get(Bytes.toBytes("10001"));
get.addFamily(Bytes.toBytes("info"));
get.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"));
Result result = tab.get(get);
for (Cell cell : result.rawCells()) {
String family = Bytes.toString(CellUtil.cloneFamily(cell));
String qualifier = Bytes.toString(CellUtil.cloneQualifier(cell));
String value = Bytes.toString(CellUtil.cloneValue(cell));
System.out.println(family + "," + qualifier + "," + value);
}
tab.close();
}
/**
* 创建命名空间
*/
@Test
public void create_namespace() {
try {
NamespaceDescriptor nsd = NamespaceDescriptor.create("senior6").build();
admin.createNamespace(nsd);
} catch (NamespaceExistException e) {
System.out.println("已存在");
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 释放资源
*
* @throws IOException
*/
@After
public void disabled() throws IOException {
if (admin != null)
admin.close();
if (conn != null)
conn.close();
}
}
标签:admin,Bytes,public,API,IOException,DDL,table,HBase,throws 来源: https://blog.csdn.net/weixin_46524944/article/details/118916770
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。