概述
1. HBASE连接及创建表
1.1代码
- 描述:
- 建立连接
- 实例化HBaseAdmin
- 创建TableDescriptor
- 创建列族
- 使用HBaseAdmin类的createTable()方法创建表
package com.hbase;
import java.io.IOException;
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.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.HBaseAdmin;
public class HbaseSample {
public static Configuration conf;
public static HBaseAdmin admin;
public static Connection connection;
static {
//使用HBaseConfiguration的单例方法实例化
conf=HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "hadoop101");
conf.set("hbase.zookeeper.property.clientPort", "2181");
}
public static void createTable(String tableName,String ...columnFamily) throws IOException {
connection=ConnectionFactory.createConnection(conf);
//实例化HBaseAdmin
admin=(HBaseAdmin) connection.getAdmin();
if(admin.tableExists(tableName)) {
System.out.println(tableName+"已存在");
}else {
//创建TableDescriptor
HTableDescriptor descriptor=new HTableDescriptor(TableName.valueOf(tableName));
//创建列族
for(String cf:columnFamily) {
descriptor.addFamily(new HColumnDescriptor(cf));
}
//使用HBaseAdmin类的createTable()方法,创建表
admin.createTable(descriptor);
admin.close();
connection.close();
}
}
}
1.2测试
描述:创建sample表,列族info1,info2
public static void main(String[] args) throws IOException {
//1、创建sample表,列族info1,info2
HbaseSample hs=new HbaseSample();
hs.createTable("sample", "info1","info2");
}
1.3 测试结果
描述:通过hbase shell 使用 describe 命令查看
hbase(main):029:0> describe "sample"
Table sample is ENABLED
sample
COLUMN FAMILIES DESCRIPTION
{NAME => 'info1', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', K
EEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', C
OMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '6
5536', REPLICATION_SCOPE => '0'}
{NAME => 'info2', BLOOMFILTER => 'ROW', VERSIONS => '1', IN_MEMORY => 'false', K
EEP_DELETED_CELLS => 'FALSE', DATA_BLOCK_ENCODING => 'NONE', TTL => 'FOREVER', C
OMPRESSION => 'NONE', MIN_VERSIONS => '0', BLOCKCACHE => 'true', BLOCKSIZE => '6
5536', REPLICATION_SCOPE => '0'}
2 row(s) in 0.0530 seconds
2. 插入数据
public class HbaseSample {
public static void addData(String tableName,String rowkey,String columnFamliy,
String column,String value) throws IOException {
connection=ConnectionFactory.createConnection(conf);
admin=(HBaseAdmin) connection.getAdmin();
//1、获取Table
Table table=connection.getTable(TableName.valueOf(tableName));
//2、创建put对象,插入数据
Put put=new Put(Bytes.toBytes(rowkey));
put.addColumn(Bytes.toBytes(columnFamliy), Bytes.toBytes(column),
Bytes.toBytes(value));
//3 保存数据
table.put(put);
//4 关闭连接
table.close();
admin.close();
connection.close();
}
}
2.2测试
public static void main(String[] args) throws IOException {
HbaseSample hs=new HbaseSample();
//1、创建sample表,列族info1,info2
// hs.createTable("sample", "info1","info2");
//2 插入数据
hs.addData("sample", "001", "info1", "name", "Kate");
hs.addData("sample", "001", "info1", "sex", "Female");
hs.addData("sample", "001", "info2", "name", "Micle");
hs.addData("sample", "001", "info2", "sex", "Male");
}
2.3测试结果
hbase(main):035:0> scan "sample"
ROW COLUMN+CELL
001 column=info1:name, timestamp=1593603805436, value=Kate
001 column=info1:sex, timestamp=1593603805558, value=Female
001 column=info2:name, timestamp=1593603805624, value=Micle
001 column=info2:sex, timestamp=1593603805682, value=Male
1 row(s) in 0.0270 seconds
3. 查询数据
3.1代码
public static void getData(String tableName,String rowkey) throws IOException {
connection=ConnectionFactory.createConnection(conf);
//1 Table对象
Table table=connection.getTable(TableName.valueOf(tableName));
//2 创建get对象
Get get=new Get(Bytes.toBytes(rowkey));
//3 获取数据
Result result=table.get(get);
for(Cell cell:result.rawCells()) {
System.out.println("列族:"+Bytes.toString(CellUtil.cloneFamily(cell)));
System.out.println("列名"+Bytes.toString(CellUtil.cloneQualifier(cell)));
System.out.println("值"+Bytes.toString(CellUtil.cloneValue(cell)));
System.out.println("+++++++++++++++++++++++++++++++++++++===");
}
table.close();
connection.close();
}
3.2测试
//3查询数据
public static void main(String[] args) throws IOException {
HbaseSample hs=new HbaseSample();
//1、创建sample表,列族info1,info2
// hs.createTable("sample", "info1","info2");
//2 插入数据
// hs.addData("sample", "001", "info1", "name", "Kate");
// hs.addData("sample", "001", "info1", "sex", "Female");
// hs.addData("sample", "001", "info2", "name", "Micle");
// hs.addData("sample", "001", "info2", "sex", "Male");
//3查询数据
hs.getData("sample", "001");
}
3.3测试结果
2020-07-01 20:18:55,722 INFO [org.apache.zookeeper.ClientCnxn]
列族:info1
列名name
值Kate
+++++++++++++++++++++++++++++++++++++===
列族:info1
列名sex
值Female
+++++++++++++++++++++++++++++++++++++===
列族:info2
列名name
值Micle
+++++++++++++++++++++++++++++++++++++===
列族:info2
列名sex
值Male
+++++++++++++++++++++++++++++++++++++===
4. 删除数据
4.1代码
public static void deleteData(String tableName,String rowkey) throws IOException{
connection=ConnectionFactory.createConnection(conf);
//1 Table
Table table=connection.getTable(TableName.valueOf(tableName));
//2 Delete
Delete delete=new Delete(Bytes.toBytes(rowkey));
//3 删除数据
table.delete(delete);
//4关闭资源
table.close();
connection.close();
}
4.2测试
public static void main(String[] args) throws IOException {
HbaseSample hs=new HbaseSample();
//1、创建sample表,列族info1,info2
// hs.createTable("sample", "info1","info2");
//2 插入数据
// hs.addData("sample", "002", "info1", "name", "Kate");
// hs.addData("sample", "002", "info1", "sex", "Female");
// hs.addData("sample", "003", "info2", "name", "Micle");
// hs.addData("sample", "003", "info2", "sex", "Male");
//3查询数据
// hs.getData("sample", "001");
//4 删除数据
hs.deleteData("sample", "001");
}
4.3测试结果
hbase(main):038:0> get 'sample','001'
COLUMN CELL
0 row(s) in 0.0310 seconds
hbase(main):039:0> scan 'sample'
ROW COLUMN+CELL
002 column=info1:name, timestamp=1593605901102, value=Kate
002 column=info1:sex, timestamp=1593605901160, value=Female
003 column=info2:name, timestamp=1593605901249, value=Micle
003 column=info2:sex, timestamp=1593605901322, value=Male
2 row(s) in 0.0380 seconds
5全部代码
package com.hbase;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
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.Table;
import org.apache.hadoop.hbase.util.Bytes;
public class HbaseSample {
public static Configuration conf;
public static HBaseAdmin admin;
public static Connection connection;
static {
//使用HBaseConfiguration的单例方法实例化
conf=HBaseConfiguration.create();
conf.set("hbase.zookeeper.quorum", "hadoop101");
conf.set("hbase.zookeeper.property.clientPort", "2181");
}
public static void createTable(String tableName,String ...columnFamily) throws IOException {
connection=ConnectionFactory.createConnection(conf);
//实例化HBaseAdmin
admin=(HBaseAdmin) connection.getAdmin();
if(admin.tableExists(tableName)) {
System.out.println(tableName+"已存在");
}else {
//创建TableDescriptor
HTableDescriptor descriptor=new HTableDescriptor(TableName.valueOf(tableName));
//创建列族
for(String cf:columnFamily) {
descriptor.addFamily(new HColumnDescriptor(cf));
}
//使用HBaseAdmin类的createTable()方法,创建表
admin.createTable(descriptor);
admin.close();
connection.close();
}
}
public static void addData(String tableName,String rowkey,String columnFamliy,
String column,String value) throws IOException {
connection=ConnectionFactory.createConnection(conf);
//1、获取Table
Table table=connection.getTable(TableName.valueOf(tableName));
//2、创建put对象,插入数据
Put put=new Put(Bytes.toBytes(rowkey));
put.addColumn(Bytes.toBytes(columnFamliy), Bytes.toBytes(column),
Bytes.toBytes(value));
//3 保存数据
table.put(put);
//4 关闭连接
table.close();
connection.close();
}
public static void getData(String tableName,String rowkey) throws IOException {
connection=ConnectionFactory.createConnection(conf);
//1 Table对象
Table table=connection.getTable(TableName.valueOf(tableName));
//2 创建get对象
Get get=new Get(Bytes.toBytes(rowkey));
//3 获取数据
Result result=table.get(get);
for(Cell cell:result.rawCells()) {
System.out.println("列族:"+Bytes.toString(CellUtil.cloneFamily(cell)));
System.out.println("列名"+Bytes.toString(CellUtil.cloneQualifier(cell)));
System.out.println("值"+Bytes.toString(CellUtil.cloneValue(cell)));
System.out.println("+++++++++++++++++++++++++++++++++++++===");
}
table.close();
connection.close();
}
public static void deleteData(String tableName,String rowkey) throws IOException{
connection=ConnectionFactory.createConnection(conf);
//1 Table
Table table=connection.getTable(TableName.valueOf(tableName));
//2 Delete
Delete delete=new Delete(Bytes.toBytes(rowkey));
//3 删除数据
table.delete(delete);
//4关闭资源
table.close();
connection.close();
}
public static void main(String[] args) throws IOException {
HbaseSample hs=new HbaseSample();
//1、创建sample表,列族info1,info2
// hs.createTable("sample", "info1","info2");
//2 插入数据
// hs.addData("sample", "002", "info1", "name", "Kate");
// hs.addData("sample", "002", "info1", "sex", "Female");
// hs.addData("sample", "003", "info2", "name", "Micle");
// hs.addData("sample", "003", "info2", "sex", "Male");
//3查询数据
// hs.getData("sample", "001");
//4 删除数据
hs.deleteData("sample", "001");
}
}
最后
以上就是大力小蝴蝶为你收集整理的HBase API实例操作,附代码的全部内容,希望文章能够帮你解决HBase API实例操作,附代码所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复