hbase-java-client

package hbase.test;

import java.io.IOException;
import org.apache.avro.generic.GenericData;
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.MasterNotRunningException;
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;

public class HbaseTest {

            static Configuration configuration = null;

            static {
                        configuration = HBaseConfiguration.create();
            }

            //創建表並指定列簇
            public static void createTable(String tableName, String cols[]) throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
                        HBaseAdmin baseAdmin = new HBaseAdmin(configuration);
                        if (baseAdmin.tableExists(tableName)) {
                                    System.out.println("表【" + tableName + "】存在");
                        } else {
                                    HTableDescriptor tableDescriptor = new HTableDescriptor(tableName);
                                    for (String s : cols) {
                                                HColumnDescriptor columnDescriptor = new HColumnDescriptor(s);
                                                tableDescriptor.addFamily(columnDescriptor);
                                    }
                                    baseAdmin.createTable(tableDescriptor);
                                    baseAdmin.close();
                        }
            }

            //刪除表
            public static void deleteTable(String tableName) throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
                        HBaseAdmin admin = new HBaseAdmin(configuration);
                        if (admin.tableExists(tableName)) {
                                    admin.disableTable(tableName);
                                    admin.deleteTable(tableName);
                        }
            }

            //添加數據
            /**
             * @param tableName 表名
             * @param rowkey 行號
             * @param colFamily 列簇
             * @param column 列
             * @param value 列值
             */
            public static void insertData(String tableName, String rowkey, String colFamily, String column, String value) throws IOException {
                        HTable table = new HTable(configuration, tableName);
                        Put put = new Put(Bytes.toBytes(rowkey));
                        put.add(Bytes.toBytes(colFamily), Bytes.toBytes(column), Bytes.toBytes(value));
                        table.put(put);
                        table.close();
            }

            //查詢一條數據
            public static void getOneByeRowkey(String tableName, String rowkey) throws IOException {
                        HTable table = new HTable(configuration, tableName);
                        Get get = new Get(Bytes.toBytes(rowkey));
                        Result result = table.get(get);
                        for (KeyValue k : result.raw()) {
                                    System.out.println("行號:" + Bytes.toStringBinary(k.getRow()));
                                    System.out.println("時間戳:" + k.getTimestamp());
                                    System.out.println("列簇:" + Bytes.toStringBinary(k.getFamily()));
                                    System.out.println("列:" + Bytes.toStringBinary(k.getQualifier()));
                                    System.out.println("值:" + Bytes.toString(k.getValue()));
                        }
                        table.close();
            }

            //查詢指定數據
            public static void scanData(String tableName) throws IOException {
                        HTable table = new HTable(configuration, tableName);
                        Scan scan = new Scan();
                        ResultScanner rs = table.getScanner(scan);
                        for (Result result : rs) {
                                    for (KeyValue k : result.raw()) {
                                                System.out.println("行號:" + Bytes.toStringBinary(k.getRow()));
                                                System.out.println("時間戳:" + k.getTimestamp());
                                                System.out.println("列簇:" + Bytes.toStringBinary(k.getFamily()));
                                                System.out.println("列:" + Bytes.toStringBinary(k.getQualifier()));
                                                System.out.println("值:" + Bytes.toString(k.getValue()));
                                    }
                        }
                        table.close();
            }

            //刪除一條數據
            @SuppressWarnings("empty-statement")
            public static void deleteByRow(String tableName, String rowkey) throws IOException {
                        HTable table = new HTable(configuration, tableName);
                        //刪除一條數據
                        Delete delete = new Delete(Bytes.toBytes(rowkey));
                        table.delete(delete);
                        //刪除多條數據
                        String[] rowkeys = {};
                        java.util.List<Delete> list = new java.util.ArrayList<Delete>();
                        for (String rk : rowkeys) {
                                    Delete d = new Delete(Bytes.toBytes(rk));
                                    list.add(d);
                        }
                        table.delete(list);
                        //
                        table.close();
            }

            public static void main(String[] args) throws ZooKeeperConnectionException, IOException {
                        String tableName = "mytest";
                        String cols[] = {"a", "b", "c"};
                        // createTable(tableName, cols);
                        // insertData(tableName, "1", "a", "abc", "11111");
                        // getOneByeRowkey(tableName, "1");
                        //scanData(tableName);
                        // deleteByRow(tableName, "1");
                        // scanData(tableName);
            }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章