1. 程式人生 > >javaApi呼叫Hbase 單機模式

javaApi呼叫Hbase 單機模式

有興趣可以瞭解下這款國內人氣很旺的JAVA程式碼生成器基於拖拽,不用寫複雜的模板,支援多種資料庫,適配wap,管理後臺各種功能全有 免費開源 地址:https://blog.csdn.net/adyuebanwan/article/details/83006405 或者 http://www.magicalcoder.com

=======================================================================================

今天下載了hbase0.98.11版本  並安裝到了192.168.0.2機器 hadoop zookeeper都不用安裝

hbase-env.sh 配置

 # The java implementation to use.  Java 1.6 required.

export JAVA_HOME=/usr/lib/jvm/j2sdk1.6-oracle/

# Extra Java CLASSPATH elements.  Optional.

export HBASE_CLASSPATH=/home/hadoop/hbase/hbase-0.98.11-hadoop2/

hbase-site.xml

    <property>

        <name>hbase.rootdir</name>

        <value>/home/hadoop/hbase/database</value>

    </property>

然後啟動hbase

./bin/start-hbase.sh

正常

> hbase shell 

> list 

下面使用javaAPI呼叫看看 直接把0.98.11下的lib 直接拷貝到工程下 否則版本不對 也是無法連線的

本地host 配置

192.168.0.2 hbasestudy

package main.base.put;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
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.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 Test {

        static final String rowKey = "row1";
        static HBaseAdmin hBaseAdmin;
        static Configuration conf;

        static {
            conf = HBaseConfiguration.create();
            conf.set("hbase.zookeeper.quorum", "hbasestudy");
            try {
                hBaseAdmin = new HBaseAdmin(conf);
            } catch (IOException e) {
                e.printStackTrace();
            }

        }


        public static void createTable(String tableName, String[] columns) throws Exception {
            dropTable(tableName);
            HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
            for (String columnName : columns) {
                HColumnDescriptor column = new HColumnDescriptor(columnName);
                hTableDescriptor.addFamily(column);
            }
            hBaseAdmin.createTable(hTableDescriptor);
            System.out.println("create table successed");
        }


        public static void dropTable(String tableName) throws Exception {
            if (hBaseAdmin.tableExists(tableName)) {
                hBaseAdmin.disableTable(tableName);
                hBaseAdmin.deleteTable(tableName);
            }
            System.out.println("drop table successed");
        }


        public static HTable getHTable(String tableName) throws Exception {
            return new HTable(conf, tableName);
        }


        public static void insert(String tableName, Map<String, String> map) throws Exception {
            HTable hTable = getHTable(tableName);
            byte[] row1 = Bytes.toBytes(rowKey);
            Put p1 = new Put(row1);
            for (String columnName : map.keySet()) {
                byte[] value = Bytes.toBytes(map.get(columnName));
                String[] str = columnName.split(":");
                byte[] family = Bytes.toBytes(str[0]);
                byte[] qualifier = null;
                if (str.length > 1) {
                    qualifier = Bytes.toBytes(str[1]);
                }
                p1.add(family, qualifier, value);
            }
            hTable.put(p1);
            Get g1 = new Get(row1);
            Result result = hTable.get(g1);
            System.out.println("Get: " + result);
            System.out.println("insert successed");
        }


        public static void delete(String tableName, String rowKey) throws Exception {
            HTable hTable = getHTable(tableName);
            List<Delete> list = new ArrayList<Delete>();
            Delete d1 = new Delete(Bytes.toBytes(rowKey));
            list.add(d1);
            hTable.delete(list);
            Get g1 = new Get(Bytes.toBytes(rowKey));
            Result result = hTable.get(g1);
            System.out.println("Get: " + result);
            System.out.println("delete successed");
        }


        public static void selectOne(String tableName, String rowKey) throws Exception {
            HTable hTable = getHTable(tableName);
            Get g1 = new Get(Bytes.toBytes(rowKey));
            Result result = hTable.get(g1);
            foreach(result);
            System.out.println("selectOne end");
        }


        private static void foreach(Result result) throws Exception {
            for (KeyValue keyValue : result.raw()) {
                StringBuilder sb = new StringBuilder();
                sb.append(Bytes.toString(keyValue.getRow())).append("\t");
                sb.append(Bytes.toString(keyValue.getFamily())).append("\t");
                sb.append(Bytes.toString(keyValue.getQualifier())).append("\t");
                sb.append(keyValue.getTimestamp()).append("\t");
                sb.append(Bytes.toString(keyValue.getValue())).append("\t");
                System.out.println(sb.toString());
            }
        }


        public static void selectAll(String tableName) throws Exception {
            HTable hTable = getHTable(tableName);
            Scan scan = new Scan();
            ResultScanner resultScanner = null;
            try {
                resultScanner = hTable.getScanner(scan);
                for (Result result : resultScanner) {
                    foreach(result);
                }
            }
            catch (Exception e) {
                e.printStackTrace();
            }
            finally {
                if (resultScanner != null) {
                    resultScanner.close();
                }
            }
            System.out.println("selectAll end");
        }


        public static void main(String[] args) throws Exception {
            String tableName = "tableTest";
            String[] columns = new String[] { "column_A", "column_B" };
            createTable(tableName, columns);
            Map<String, String> map = new HashMap<String, String>();
            map.put("column_A", "AAA");
            map.put("column_B:1", "b1");
            map.put("column_B:2", "b2");
            insert(tableName, map);
            selectOne(tableName, rowKey);
            selectAll(tableName);
            delete(tableName, rowKey);
            dropTable(tableName);
        }

}

然後執行 就一直停留在下面 坑啊 什麼情況

log4j:WARN No appenders could be found for logger (org.apache.hadoop.metrics2.lib.MutableMetricsFactory).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

然後 等了好久 崩出來個 unkown host  localhost.2.com

開啟 192.168.0.2  機器的host 

vi /etc/hosts

192.168.0.2 localhost.2.com

192.168.0.2 hbasestudy

我去 然後我調整了下他們的順序  把hbasestudy 放第一行

192.168.0.2 hbasestudy 

192.168.0.2 localhost.2.com

一定要重啟hbase 

然後再呼叫api 問題居然解決了 怎麼這麼奇葩

Connected to the target VM, address: '127.0.0.1:52802', transport: 'socket'
log4j:WARN No appenders could be found for logger (org.apache.hadoop.metrics2.lib.MutableMetricsFactory).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
drop table successed
create table successed
Get: keyvalues={row1/column_A:/1428499379529/Put/vlen=3/mvcc=0, row1/column_B:1/1428499379529/Put/vlen=2/mvcc=0, row1/column_B:2/1428499379529/Put/vlen=2/mvcc=0}
insert successed
row1 column_A 1428499379529 AAA
row1 column_B 1 1428499379529 b1
row1 column_B 2 1428499379529 b2
selectOne end
row1 column_A 1428499379529 AAA
row1 column_B 1 1428499379529 b1
row1 column_B 2 1428499379529 b2
selectAll end
Get: keyvalues=NONE
delete successed
drop table successed