package com.bean.hbase;
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.KeyValue;
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.HConnection;
import org.apache.hadoop.hbase.client.HConnectionManager;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.HTableInterface;
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;
public class Hbase {
private Configuration hconf;
private HBaseAdmin hadmin;
public Hbase() throws Exception {
hconf = HBaseConfiguration.create();
hconf.set("hbase.zookeeper.quorum", "192.168.5.200");
hconf.set("hbase.zookeeper.property.clientPort", "2181");
hadmin = new HBaseAdmin(hconf);
}
/**
* 创建表
*
* @param tableName
* @param col
* @throws Exception
*/
private void createTable(String tableName, List col) throws Exception {
// TODO Auto-generated method stub
if (hadmin.tableExists(tableName)) {
throw new Exception(tableName + " exists");
}
HTableDescriptor htd = new HTableDescriptor(tableName);
for (String string : col) {
HColumnDescriptor hcd = new HColumnDescriptor(string);
htd.addFamily(hcd);
}
hadmin.createTable(htd);
}
/**
* 插入数据
*
* @param args
* @throws Exception
*/
private void putData(String tableName, List value) throws Exception {
// TODO Auto-generated method stub
HTable htable = new HTable(hconf, tableName);
htable.put(value);
htable.setAutoFlush(false);
htable.flushCommits();
}
/**
* 删除表
* @param tableName
* @throws Exception
*/
private void deleteTable(String tableName) throws Exception {
// TODO Auto-generated method stub
hadmin.disableTable(tableName);
hadmin.deleteTable(tableName);
}
/**
* 删除数据
* @throws Exception
*/
private void deleteValue(String tableName,String columnName) throws Exception {
// TODO Auto-generated method stub
HTable htable = new HTable(hconf, tableName);
Delete delete = new Delete(columnName.getBytes());
delete.deleteColumn("address".getBytes(), "city".getBytes());
htable.delete(delete);
}
/**
* @throws Exception
*
*/
private Result getData(String tableName,String rowKey) throws Exception {
// TODO Auto-generated method stub
HTable htable = new HTable(hconf, tableName);
Get get = new Get(Bytes.toBytes(rowKey));
return htable.get(get);
}
/**
*
*/
private void format(Result result) {
// TODO Auto-generated method stub
String rowkey = Bytes.toString(result.getRow());
KeyValue[] kvs = result.raw();
for (KeyValue keyValue : kvs) {
//api给出一句话
//Do not use unless you have to.
String key = Bytes.toString(keyValue.getRow());
String family = Bytes.toString(keyValue.getFamily());
String qualifier = Bytes.toString(keyValue.getQualifier());
String value = Bytes.toString(keyValue.getValue());
System.out.println("key : "+key+"列族"+family+" : " +qualifier +" value "+value);
}
}
private void hbaseScan(String tableName) throws Exception {
// TODO Auto-generated method stub
Scan scan = new Scan();
//api给出
//Set the number of rows for caching that will be passed to scanners.
scan.setCaching(1000);
HTable htable = new HTable(hconf, tableName);
ResultScanner rs = htable.getScanner(scan);
for (Result result : rs) {
String row = Bytes.toString(result.getRow());
System.out.println(row);
String value = Bytes.toString(result.getValue("address".getBytes(),"city".getBytes() ));
System.out.println(value);
}
}
public static void main(String[] args) throws Exception {
Hbase h = new Hbase();
// 创建表
// List col = new ArrayList();
// col.add("st_id");
// col.add("info");
// col.add("address");
// col.add("scores");
// h.createTable("student", col);
// 插入数据
//String tableName = "student";
// List puts = new ArrayList();
// Put put1 = new Put("xueba1".getBytes());
// put1.add(Bytes.toBytes("address"), Bytes.toBytes("city"),
// Bytes.toBytes("shanghai"));
// put1.add(Bytes.toBytes("info"), Bytes.toBytes("age"),
// Bytes.toBytes("20"));
// put1.add(Bytes.toBytes("scores"), Bytes.toBytes("chiness"),
// Bytes.toBytes("80"));
// put1.add(Bytes.toBytes("scores"), Bytes.toBytes("math"),
// Bytes.toBytes("85"));
// put1.add(Bytes.toBytes("scores"), Bytes.toBytes("english"),
// Bytes.toBytes("90"));
// Put put2 = new Put("xuezha".getBytes());
// put2.add(Bytes.toBytes("address"), Bytes.toBytes("city"),
// Bytes.toBytes("jinan"));
// put2.add(Bytes.toBytes("info"), Bytes.toBytes("age"),
// Bytes.toBytes("19"));
// put2.add(Bytes.toBytes("scores"), Bytes.toBytes("chiness"),
// Bytes.toBytes("90"));
// put2.add(Bytes.toBytes("scores"), Bytes.toBytes("math"),
// Bytes.toBytes("95"));
// put2.add(Bytes.toBytes("scores"), Bytes.toBytes("english"),
// Bytes.toBytes("100"));
// puts.add(put1);
// puts.add(put2);
// h.putData(tableName, puts);
//h.deleteTable(tableName);
//h.deleteValue("student", "xueba1");
//Result result = h.getData("student", "xueba1");
//h.format(result);
h.hbaseScan("student");
}
}