HBase增刪改查客戶端
阿新 • • 發佈:2018-12-06
需要講解的內容基本都寫在程式碼裡了,直接貼程式碼
import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; 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.util.Bytes; import org.apache.log4j.Logger; import com.css.service.DtsService; /** * 資料傳輸服務客戶端 * @version DTS 1.0 * @time 2018-11-14 * @author 一朝風月 */ public class DtsClient implements DtsService { // Logger物件 private Logger log = Logger.getLogger(DtsClient.class); // 連線配置 private static final Configuration config = HBaseConfiguration.create(); // 設定日期格式 private static final SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); /** * 預設構造器--無參構造器 */ public DtsClient() { super(); } /** * 初始化連線配置 * @param quorum 叢集成員列表 * @param zkPort ZooKeeper對外提供服務埠 * @param master 叢集主機名 */ public DtsClient(String quorum, String zkPort, String master){ config.set("hbase.zookeeper.quorum",quorum); config.set("hbase.zookeeper.property.clientPort", zkPort); config.set("hbase.master", master); } /** * 建立表 * @param tableName 表名稱 * @param families 列族 */ @Override public boolean createTable(String tableName, String[] families) { boolean result = false; try{ HBaseAdmin admin = new HBaseAdmin(config); HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf(tableName)); for(int i = 0; i < families.length; i++){ tableDescriptor.addFamily(new HColumnDescriptor(families[i])); } log.info(df.format(new Date()) + "--" + "開始建立" + tableName); admin.createTable(tableDescriptor); log.info(df.format(new Date()) + "--" + tableName+"表建立完成!"); result = true; admin.close(); } catch (MasterNotRunningException e1){ log.error(Thread.currentThread().getName() + "master主機沒有執行", e1); } catch (ZooKeeperConnectionException e2){ log.error(Thread.currentThread().getName() + "zookeeper沒有連線", e2); } catch (IOException e3){ log.error(Thread.currentThread().getName() + "IOExcetpion讀寫異常", e3); } return result; } /** * 刪除表 * @param tableName 表名稱 */ @Override public boolean deleteTable(String tableName) { boolean result = false; try { HBaseAdmin admin = new HBaseAdmin(config); log.info(df.format(new Date()) + "--" + tableName + "被禁用"); admin.disableTable(tableName); log.info(df.format(new Date()) + "--" + tableName + "被刪除"); admin.deleteTable(tableName); result = true; admin.close(); } catch (IOException e) { log.error(Thread.currentThread().getName() + "--" +df.format(new Date()) + "--" + "IO讀寫失敗", e); } return result; } @Override public boolean modifyTable() { // TODO Auto-generated method stub return false; } /** * 查詢某一屬性的值 * @param tableName 表名稱 * @param rowNo 行號 * @param family 列名 * @param property 屬性名 */ @Override public String select(String tableName, String rowNo, String family, String property) { String resultValue = ""; // 批量查詢效率值得商榷 try{ HTable hTable = new HTable(config, tableName); Get g = new Get(Bytes.toBytes(rowNo)); Result result = hTable.get(g); byte [] value = result.getValue(Bytes.toBytes(family),Bytes.toBytes(property)); resultValue = Bytes.toString(value); hTable.close(); } catch (IOException e){ log.error("IO讀寫異常"); log.error(Thread.currentThread().getName(), e); } return resultValue; } /** * 插入一條記錄 * @param tableName 表名稱 * @param rowNo 行號 * @param family 列名 * @param property 屬性名 * @param value 屬性值 */ @Override public boolean insert(String tableName, String rowNo, String family, String property, String value) { boolean result = false; try{ HTable hTable = new HTable(config, tableName); Put p = new Put(Bytes.toBytes(rowNo)); p.add(Bytes.toBytes(family), Bytes.toBytes(property),Bytes.toBytes(value)); hTable.put(p); log.info(df.format(new Date()) + "--" +"插入資料完成!"); result = true; hTable.close(); } catch (IOException e) { log.error("IO讀寫異常"); log.error(Thread.currentThread().getName(), e); } return result; } /** * 更新一條記錄 * @param tableName 表名稱 * @param rowNo 行號 * @param family 列名 * @param property 屬性名 * @param value 屬性值 */ @Override public boolean update(String tableName, String rowNo, String family, String property, String value) { boolean result = false; try{ HTable hTable = new HTable(config, tableName); Put p = new Put(Bytes.toBytes(rowNo)); p.add(Bytes.toBytes(family), Bytes.toBytes(property),Bytes.toBytes(value)); hTable.put(p); log.info(df.format(new Date()) + "--" +"更新資料完成!"); result = true; hTable.close(); } catch (IOException e) { log.error("IO讀寫異常"); log.error(Thread.currentThread().getName(), e); } return result; } /** * 刪除某條記錄 * @param tableName 表名稱 * @param rowNo 行號 * @param family 列名 * @param property 屬性 */ @Override public boolean delete(String tableName, String rowNo, String family, String property) { boolean result = false; try{ HTable table = new HTable(config, tableName); Delete delete = new Delete(Bytes.toBytes(rowNo)); delete.deleteColumn(Bytes.toBytes(family), Bytes.toBytes(property)); table.delete(delete); log.info("刪除某條記錄"); table.close(); } catch (IOException e) { log.error("IO讀寫異常"); log.error(Thread.currentThread().getName(), e); } return result; } /** * 測試方法 * @param args */ public static void main(String[] args) { String quorum = "master,slave1,slave2"; String zkPort = "2181"; String master = "192.168.225.100:16000"; DtsClient dc = new DtsClient(quorum, zkPort, master); String[] families = {"teacher", "student"}; dc.createTable("school", families); dc.insert("school", "row1", "teacher", "name", "zhangsanfeng"); dc.insert("school", "row1", "teacher", "age", "30"); dc.insert("school", "row1", "student", "name", "songyuanqiao"); dc.insert("school", "row1", "student", "age", "18"); String name = dc.select("school", "row1", "student", "name"); String age = dc.select("school", "row1", "student", "age"); System.out.println(name + ": " + age); dc.update("school", "row1", "student", "name", "moshenggu"); name = dc.select("school", "row1", "student", "name"); age = dc.select("school", "row1", "student", "age"); System.out.println(name + ": " + age); dc.delete("school", "row1", "student", "name"); dc.delete("school", "row1", "student", "age"); dc.deleteTable("school"); } }