在这里简单介绍一下hbase的接口操作,
首先新建一个java project
项目下建一个lib文件夹,将hbase所依赖的jar包导入(在你安装hbase的lib目录下)
全选后,build path 将jar变为奶瓶。
接下来就可以利用java接口来操作hbase数据库啦。
下面不多说,直接看代码
package cn.master.hbase;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
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.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
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.util.Bytes;
import org.junit.Before;
import org.junit.Test;
public class HBaseDEMO {
private Configuration conf = null;
@Before
public void init(){
conf = HBaseConfiguration.create();
//连zookeeper
conf.set("hbase.zookeeper.quorum", "master04:2181,master05:2181,master06:2181");
}
/**
* 插入数据
* @author tom
* @throws IOException
* @date : 2017年5月20日 下午1:46:51
*/
@Test
public void testPut() throws IOException{
HTable table = new HTable(conf, "peoples");
Put put = new Put(Bytes.toBytes("kr0001"));
put.add(Bytes.toBytes("info"), Bytes.toBytes("name"), Bytes.toBytes("zhangsanfeng"));
put.add(Bytes.toBytes("info"), Bytes.toBytes("age"), Bytes.toBytes("300"));
put.add(Bytes.toBytes("info"), Bytes.toBytes("money"), Bytes.toBytes(3000000));
table.put(put);
table.close();
}
/**
* 这种方式不可取,所有属性放在一个集合中,太多,内存不够用的情况容易发生。
* @author tom
* @throws IOException
* @date : 2017年5月20日 下午1:56:50
*/
@Test
public void testPutAll() throws IOException{
HTable table = new HTable(conf, "peoples");
List<Put> puts = new ArrayList<Put>();
for(Put i:puts){
Put put = new Put(Bytes.toBytes("kr"+i));
put.add(Bytes.toBytes("info"), Bytes.toBytes("money"), Bytes.toBytes(""+i));
puts.add(put);
}
table.put(puts);
table.close();
}
/**
* 每隔10000条数据重新开一个Put的list
* @author tom
* @throws IOException
* @date : 2017年5月20日 下午1:56:50
*/
@Test
public void testPutAll2() throws IOException{
HTable table = new HTable(conf, "peoples");
List<Put> puts = new ArrayList<Put>(10000);
for(int i = 1 ; i <= 1000000 ;i++){
Put put = new Put(Bytes.toBytes("kr"+i));
put.add(Bytes.toBytes("info"), Bytes.toBytes("money"), Bytes.toBytes(""+i));
puts.add(put);
if(i % 10000 == 0){
table.put(puts);
puts = new ArrayList<Put>(10000);
}
}
table.put(puts);
table.close();
}
/**
* 查一个
* @author tom
* @throws IOException
* @date : 2017年5月20日 下午2:14:52
*/
@Test
public void testGet() throws IOException{
HTable table = new HTable(conf, "people");
Get get = new Get(Bytes.toBytes("kr999999"));
Result result = table.get(get);
String r = Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("money")));
System.out.println(r);
table.close();
}
/**
* 查多个 前闭后开
* @author tom
* @throws IOException
* @date : 2017年5月20日 下午2:21:03
*/
@Test
public void testScan() throws IOException{
HTable table = new HTable(conf, "people");
Scan scan = new Scan(Bytes.toBytes("kr299999"), Bytes.toBytes("300000"));
ResultScanner scanner = table.getScanner(scan);
for(Result result:scanner){
String r = Bytes.toString(result.getValue(Bytes.toBytes("info"), Bytes.toBytes("money")));
System.out.println(r);
//注意输出的是299999 3 30 300 3000 30000
}
table.close();
}
/**
* delete single
* @author tom
* @throws IOException
* @date : 2017年5月20日 下午2:35:38
*/
@Test
public void testDel() throws IOException{
HTable table = new HTable(conf, "people");
Delete delete = new Delete(Bytes.toBytes("kr299999"));
table.delete(delete);
table.close();
}
public static void main(String[] args) throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
Configuration conf = HBaseConfiguration.create();
//连zookeeper
conf.set("hbase.zookeeper.quorum", "master04:2181,master05:2181,master06:2181");
HBaseAdmin admin = new HBaseAdmin(conf);
HTableDescriptor htd = new HTableDescriptor(TableName.valueOf("peoples"));
HColumnDescriptor hcd_info = new HColumnDescriptor("info");
hcd_info.setMaxVersions(3);
HColumnDescriptor hcd_data = new HColumnDescriptor("data");
htd.addFamily(hcd_info);
htd.addFamily(hcd_data);
admin.createTable(htd);
admin.close();
}
}