Windows IDEA 远程连接操作Linux下的Hbase 关键点

                                                 IDEA下使用JAVA远程操作linux下的Hbase

在Windows下远程连接操作Linux下的Hbase,采坑过程中遇到的问题和关键注意点记录如下

Centos7 下的配置如下:

   hostname:tiger   , IP  =192.168.0.1

   Hbase 版本1.2.*

1 在windows下的hosts文件(C:\Windows\System32\drivers\etc下)一定要添加linux的主机名和地址 

 192.168.0.1  tiger

2  HBase默认的端口是16000和16010,不同版本的具体的值可以在zookeeper命令行使用如下命令查看

get  /hbase

3 linux下的Hbase的 hbase-site.xml 配置如下:

<configuration>
<property>
  <name>hbase.rootdir</name>
  <value>hdfs://10.16.5.188:9000/hbase</value>
  <description>The directory shared byregion servers.</description>
</property>
<property>
  <name>hbase.zookeeper.property.clientPort</name>
  <value>2181</value>
  <description>Property from ZooKeeper'sconfig zoo.cfg. The port at which the clients 
    will connect.
  </description>
</property>
<property>
 <name>zookeeper.session.timeout</name>
 <value>10000</value>
</property>
<property>
  <name>hbase.zookeeper.quorum</name>
  <value>10.16.5.188</value>
</property>

<property>
  <name>hbase.zookeeper.property.dataDir</name>
  <value>/home/hadoop/software/zookeeper/zooKeeperData</value>
</property>

<property>
  <name>hbase.tmp.dir</name>
  <value>/home/hadoop/hbase/tmp</value>
</property>
<property>
  <name>hbase.cluster.distributed</name>
  <value>true</value>
</property>  
</configuration>

具体的配置参数请查阅官网,上面的参数是足够的,上面参数设置完成后对于在Linux本机操作Hbase 是没问题的,

如果想在远程主机通过Java或者scala操作Hbase需要在hbase的配置hbase-site.xml添加下面额外的配置,否则你可能陷入连接超时,重试超时各种查阅资料无法得到正确解决方案的苦恼中。

<property>
 <name>hbase.master.ipc.address</name>
 <value>0.0.0.0</value>
</property>

<property>
 <name>hbase.regionserver.ipc.address</name>
 <value>0.0.0.0</value>
</property>

   具体需要增加此配置的原因请参考:

   

4 按照如下的顺序启动Hbase

     hadoop ->zookeeper->hbase  顺序启动

5  使用Java 连接Hbase

    其中如果在工程的resources下把linux下的hbase的配置文件hbase-site.xml 拷贝过来,那么

   configuration=HBaseConfiguration.create();一句代码完成配置,如果不在工程下增加hbase-site.xml ,那么需要使用多行代码配置连接

       configuration=HBaseConfiguration.create();
       configuration.set("hbase.zookeeper.quorum","192.168.0.1:2181");

 

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.filter.CompareFilter;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.apache.hadoop.hbase.KeyValueUtil.copyToNewKeyValue;

public class HbaseTestCase
{

    public static  Configuration configuration;

    static
    {
        configuration=HBaseConfiguration.create();
        //configuration.set("hbase.master","10.16.5.188:16000");
       // configuration.set("hbase.zookeeper.quorum","10.16.5.188:2181");
    }


    public  static void  createTable(String tablename) throws  Exception
    {
         HBaseAdmin admin=new HBaseAdmin(configuration);

         if(admin.tableExists(tablename))
         {
             admin.disableTable(tablename);
             admin.deleteTable(tablename);
             System.out.println("开始创建表");
         }
        HTableDescriptor tableDescriptor=new  HTableDescriptor(tablename);
        tableDescriptor.addFamily(new HColumnDescriptor("cf1"));
        admin.createTable(tableDescriptor);

        Put put1=new Put("123".getBytes());
        put1.add("cf1".getBytes(),"column1".getBytes(),"value1".getBytes());
        put1.add("cf1".getBytes(),"column2".getBytes(),"value2".getBytes());
        put1.add("cf1".getBytes(),"column3".getBytes(),"value3".getBytes());

        HTable table=new HTable(configuration,tablename);
        table.put(put1);
        System.out.println("创建表成功。。。。");
    }

    public static  void main(String[] args)
    {
        try {
            //createTable("TT");
            Connection conn = ConnectionFactory.createConnection(configuration);
            Admin admin = null;
            try {
                admin = conn.getAdmin();

                if (!admin.tableExists(TableName.valueOf("wangxuetaotest"))) {
                    HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf("wangxuetaotest"));
                    tableDescriptor.addFamily(new HColumnDescriptor("cf1"));
                    admin.createTable(tableDescriptor);
                }
                    Table table = conn.getTable(TableName.valueOf("wangxuetaotest"));
                                   Put put = new Put(Bytes.toBytes("r1")); //行的puttos
                put.add(Bytes.toBytes("cf1"), Bytes.toBytes("c1"), Bytes.toBytes("3"));
                put.add(Bytes.toBytes("cf1"), Bytes.toBytes("c2"), Bytes.toBytes("4"));

                    byte [] temp=Bytes.toBytes(3);
                    table.put(put);


                    Get get = new Get(Bytes.toBytes("r1"));
                    Result rs = table.get(get);

                    byte[] value= rs.getValue(Bytes.toBytes("cf1"),Bytes.toBytes("c1"));

                    String ValueString=Bytes.toString(value);
                    System.out.println("值。。。。"+ValueString);

                    Map<String,String> datamap=new HashMap<>();
                    for (KeyValue kv : rs.raw()) {


                    System.out.print(new String(kv.getQualifier()) + " ");


                    System.out.print(new String(kv.getValue()) + " ");
                    System.out.println(kv.getTimestamp() + " ");


                }

            List<Cell> cells=rs.listCells();
                for (Cell kv : cells) {
                    System.out.print(new String(kv.getQualifierArray()) + " ");
                    System.out.print(new String(kv.getValueArray()) + " ");
                    System.out.println(kv.getTimestamp() + " ");
                }

                System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@22@@@");
                Filter filter=new SingleColumnValueFilter(Bytes.toBytes("cf1"),Bytes.toBytes("c1"),CompareFilter.CompareOp.EQUAL,Bytes.toBytes("3"));
                Scan scan=new Scan();
                scan.setFilter(filter);

                ResultScanner scanner=table.getScanner(scan);

                for(Result result:scanner)
                {
                    byte[] row=result.getRow();
                    List<Cell> cells2=result.listCells();
                    for (Cell kv : cells2) {
                        System.out.print(new String(kv.getQualifier()) + " ");
                        System.out.print(new String(kv.getValue()) + " ");
                        System.out.println(kv.getTimestamp() + " ");
                    }
                }

                admin.close();
                conn.close();
            }
            catch (IOException e1)
                {
                e1.printStackTrace();
            }
            System.out.println("创建表成功。。。。");
        }
         catch (Exception e)
         {
            e.printStackTrace();
        }
    }
}

6 Hbase 在非集群模式下,是不能使用外部zookeeper的,只有在hbase.cluster.distribute为ture是才能使用外部Zookeeper

  <property>
    <name>hbase.cluster.distributed</name>
     <value>true</value>
</property>

7 待续。。

  • 3
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值