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

 

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值