1. 程式人生 > >java連接hbase

java連接hbase

tid group base ext 1.0 ava sys cto ons

maven依賴

<dependency>
    <groupId>org.apache.hbase</groupId>
    <artifactId>hbase-client</artifactId>
    <version>1.2.0</version>
</dependency>

hbase-site.xml

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<!-- /** * * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License.
*/ --> <configuration> <property> <name>hbase.rootdir</name> <value>hdfs://node1:9000/hbase</value> </property> <property> <name>hbase.cluster.distributed</name> <value>true</value> </
property> <property> <name>hbase.master</name> <value>node2</value> </property> <property> <name>hbase.zookeeper.property.dataDir</name> <value>/var/lib/zookeeper</value> </property> <property> <name>hbase.zookeeper.quorum</name> <value>node1:2181,node2:2181,node3:2181</value> </property> <property> <name>hbase.zookeeper.property.clientPort</name> <value>2181</value> </property> </configuration>

獲取連接

        Configuration config = HBaseConfiguration.create();
        connection = ConnectionFactory.createConnection(config);
        admin = connection.getAdmin();

插入數據

    public static void PutIntoHbase()throws IOException{
        TableName tn = TableName.valueOf("my_table2");
        HTable hTable = (HTable) connection.getTable(tn);
        Put p = new Put(Bytes.toBytes("3"));
        p.addColumn(Bytes.toBytes("f2"), Bytes.toBytes("ff2"), Bytes.toBytes("hbaseconnect6"));
        p.addColumn(Bytes.toBytes("f2"), Bytes.toBytes("ff3"), Bytes.toBytes("hbaseconnect6"));
        p.addColumn(Bytes.toBytes("f2"), Bytes.toBytes("ff4"), Bytes.toBytes("hbaseconnect6"));
        p.addColumn(Bytes.toBytes("f2"), Bytes.toBytes("ff5"), Bytes.toBytes("hbaseconnect6"));
        hTable.put(p);
        hTable.close();
    }

scan掃描表

    public static  void ScanTable() throws  IOException{
        TableName tn = TableName.valueOf("my_table2");

        HTable hTable = (HTable) connection.getTable(tn);
        Scan scan = new Scan();
        scan.addColumn(Bytes.toBytes("f16"),Bytes.toBytes("ff1"));
        ResultScanner scanner = hTable.getScanner(scan);

//        for (Result result = scanner.next(); result != null; result = scanner.next()) {
//            System.out.println("Found row : " + result);
//        }
        for (Result r :scanner){
            System.out.println("Found row : " + r);
        }
        scanner.close();
    }

列舉所有表

    public static  void ListTable() throws  IOException{
        HTableDescriptor[] tableDescriptor = admin.listTables();
        for (int i = 0; i < tableDescriptor.length; i++) {
            System.out.println(tableDescriptor[i].getNameAsString());
        }
    }

查看表元描述

    public static void ShowTableDes()throws  IOException{
        TableName tn = TableName.valueOf("my_table2");
        HTableDescriptor hd =  admin.getTableDescriptor(tn);
        logger.info("hd.getFamilies():"+hd.getFamilies().toString());
    }

java連接hbase