&:hbase的连接信息类似jdbc地址,可通过配置文件,也可写在程序里。
通过api的使用,更深切的体会了nosql数据库的叫法的意义。
操作全部通过api完成,比jdbc写sql麻烦一些。
package com.demo.hbase;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.*;
import org.apache.hadoop.hbase.filter.CompareFilter;
import org.apache.hadoop.hbase.filter.RegexStringComparator;
import org.apache.hadoop.hbase.filter.ValueFilter;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.util.CollectionUtils;
import org.apache.hadoop.hbase.util.Strings;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.log4j.PropertyConfigurator;
import java.io.IOException;
import java.util.Properties;
import java.util.List;
import java.util.ArrayList;
public class HBaseDemo {
public static Configuration config;
static {
config=HBaseConfiguration.create();
config.addResource(new Path("F:\\hbase-1.3.3\\conf\\hbase-site.xml"));
}
/**
* 创建表
* @param tableName
* 表名
* @param colFmlys
* 列族
* @throws Exception
*/
public static void createTable(String tableName,String[] colFmlys)
throws IOException
{
HBaseAdmin hbaseAdmin=new HBaseAdmin(config);
if(hbaseAdmin.tableExists(tableName)){
System.out.print("表已经存在");
return;
}
HTableDescriptor desc=new HTableDescriptor(tableName);
for(String cf :colFmlys){
desc.addFamily(new HColumnDescriptor(cf));
}
hbaseAdmin.createTable(desc);
System.out.println("建表成功");
}
/**
* 插入数据
* @param tabname 表名
* @throws Exception
*/
public static void insert(String tabname,String rowkey,String colfmly,String col,String val){
System.out.println("******* start insert *******");
HTablePool pool=new HTablePool(config,1000);
Put put=new Put(rowkey.getBytes());
put.add(colfmly.getBytes(),col.getBytes(),val.getBytes());
try{
pool.getTable(tabname).put(put);
}catch(IOException e){
e.printStackTrace();
}
System.out.println("******* end insert *******");
}
/**
* 根据rowkey删除记录
* @param tabname
* @param rowkey
* @throws Exception
*/
public static void delrow(String tabname,String rowkey){
try{
HTable table=new HTable(config,tabname);
List list =new ArrayList();
Delete del=new Delete(rowkey.getBytes());
list.add(del);
table.delete(list);
}catch (IOException e){
e.printStackTrace();
}
}
/**
* 查询全表
* @param tabname 表名
* @throws Exception
*/
public static void queryAll(String tabname){
HTablePool pool =new HTablePool(config,1000);
try {
ResultScanner rs =pool.getTable(tabname).getScanner(new Scan());
for(Result r:rs){
System.out.println("rowkey:"+new String(r.getRow()));
for(KeyValue kv:r.raw()){
System.out.println("列族:"+new String(kv.getFamily())+"====列:"+new String(kv.getQualifier())+"====值:"+new String(kv.getValue()));
}
}
} catch (IOException e){
e.printStackTrace();
}
}
/**
* 用rowkey过滤
* @param tabname
* @throws Exception
*/
public static void queryRange(String tabname,String startRk,String endRk){
HTablePool pool =new HTablePool(config,1000);
Scan scan=new Scan(startRk.getBytes(),endRk.getBytes());
try {
ResultScanner rs =pool.getTable(tabname).getScanner(scan);
for(Result r:rs){
System.out.println("rowkey:"+new String(r.getRow()));
for(KeyValue kv:r.raw()){
System.out.println("列族:"+new String(kv.getFamily())+"====列:"+new String(kv.getQualifier())+"====值:"+new String(kv.getValue()));
}
}
} catch (IOException e){
e.printStackTrace();
}
}
/**
* 用filter过滤
* @param tabname
* @throws Exception
*/
public static void queryFilter(String tabname,String col,String regexp){
HTablePool pool =new HTablePool(config,1000);
Scan scan=new Scan();
scan.addColumn(new String("info").getBytes(),col.getBytes());
Filter f=new ValueFilter(CompareFilter.CompareOp.EQUAL,new RegexStringComparator(".*"+regexp+"*."));
scan.setFilter(f);
try {
ResultScanner rs =pool.getTable(tabname).getScanner(scan);
for(Result r:rs){
System.out.println("rowkey:"+new String(r.getRow()));
for(KeyValue kv:r.raw()){
System.out.println("列族:"+new String(kv.getFamily())+"====列:"+new String(kv.getQualifier())+"====值:"+new String(kv.getValue()));
}
}
} catch (IOException e){
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
try {
// Configuration conf = HBaseConfiguration.create();
//hbase连接信息
//1 直接写到代码里
// conf.set("hbase.zookeeper.quorum", "127.0.0.1");
//2 引用hbase-site.xml
// conf.addResource(new Path("F:\\hbase-1.3.3\\conf\\hbase-site.xml"));
// conf.addResource(new Path("hbase-site.xml"));
// Properties prop=new Properties();
// PropertyConfigurator.configure("hbase.properties");
//验证配置值
// String quorum=conf.get("hbase.zookeeper.quorum");
// System.out.println(quorum);
// HTable table = new HTable(conf, "table");
// Put put = new Put(Bytes.toBytes("row1607"));
// put.add(Bytes.toBytes("column_famaly1"), Bytes.toBytes("col1"), Bytes.toBytes("val1"));
// table.put(put);
String[] colfmly={"info","twit"};
createTable("tabapi",colfmly);
insert("tabapi","rowkey2","info","name","tim");
insert("tabapi","rowkey2","info","age","25");
queryAll("tabapi");
System.out.println("******* queryRange start*******");
queryRange("tabapi","rowkey1","rowkey7");
System.out.println("******* queryRange end*******");
System.out.println("******* queryFilter start*******");
queryFilter("tabapi","name","tim");
System.out.println("******* queryFilter end*******");
System.out.println("compelete");
} catch (Exception e){
e.printStackTrace();
}
}
}
参考:
1)https://www.cnblogs.com/kxdblog/p/4034248.html