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 {
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
);
admin
=(HBaseAdmin
) connection
.getAdmin();
if(admin
.tableExists(tableName
)) {
System
.out
.println(tableName
+"已存在");
}else {
HTableDescriptor descriptor
=new HTableDescriptor(TableName
.valueOf(tableName
));
for(String cf
:columnFamily
) {
descriptor
.addFamily(new HColumnDescriptor(cf
));
}
admin
.createTable(descriptor
);
admin
.close();
connection
.close();
}
}
}
1.2测试
描述:创建sample表,列族info1,info2
public static void main(String
[] args
) throws IOException
{
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();
Table table
=connection
.getTable(TableName
.valueOf(tableName
));
Put put
=new Put(Bytes
.toBytes(rowkey
));
put
.addColumn(Bytes
.toBytes(columnFamliy
), Bytes
.toBytes(column
),
Bytes
.toBytes(value
));
table
.put(put
);
table
.close();
admin
.close();
connection
.close();
}
}
2.2测试
public static void main(String
[] args
) throws IOException
{
HbaseSample hs
=new HbaseSample();
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
);
Table table
=connection
.getTable(TableName
.valueOf(tableName
));
Get get
=new Get(Bytes
.toBytes(rowkey
));
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测试
public static void main(String
[] args
) throws IOException
{
HbaseSample hs
=new HbaseSample();
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
);
Table table
=connection
.getTable(TableName
.valueOf(tableName
));
Delete delete
=new Delete(Bytes
.toBytes(rowkey
));
table
.delete(delete
);
table
.close();
connection
.close();
}
4.2测试
public static void main(String
[] args
) throws IOException
{
HbaseSample hs
=new HbaseSample();
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 {
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
);
admin
=(HBaseAdmin
) connection
.getAdmin();
if(admin
.tableExists(tableName
)) {
System
.out
.println(tableName
+"已存在");
}else {
HTableDescriptor descriptor
=new HTableDescriptor(TableName
.valueOf(tableName
));
for(String cf
:columnFamily
) {
descriptor
.addFamily(new HColumnDescriptor(cf
));
}
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
);
Table table
=connection
.getTable(TableName
.valueOf(tableName
));
Put put
=new Put(Bytes
.toBytes(rowkey
));
put
.addColumn(Bytes
.toBytes(columnFamliy
), Bytes
.toBytes(column
),
Bytes
.toBytes(value
));
table
.put(put
);
table
.close();
connection
.close();
}
public static void getData(String tableName
,String rowkey
) throws IOException
{
connection
=ConnectionFactory
.createConnection(conf
);
Table table
=connection
.getTable(TableName
.valueOf(tableName
));
Get get
=new Get(Bytes
.toBytes(rowkey
));
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
);
Table table
=connection
.getTable(TableName
.valueOf(tableName
));
Delete delete
=new Delete(Bytes
.toBytes(rowkey
));
table
.delete(delete
);
table
.close();
connection
.close();
}
public static void main(String
[] args
) throws IOException
{
HbaseSample hs
=new HbaseSample();
hs
.deleteData("sample", "001");
}
}