HbaseClient


//1.添加依赖
<dependencies> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-server</artifactId> <version>2.2.4</version> </dependency> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId> <version>2.2.4</version> </dependency> </dependencies>

  


//连接
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.client.Connection; import org.apache.hadoop.hbase.client.ConnectionFactory; import java.io.IOException; import java.net.URISyntaxException; public class ConnectionUtil { public static Connection getConn() throws IOException, URISyntaxException { Configuration conf= HBaseConfiguration.create(); conf.set("hbase.zookeeper.property.clientPort", "2181"); conf.set("hbase.zookeeper.quorum","hadoop"); /* conf.addResource(new Path(ClassLoader.getSystemResource("hbase-site.xml").toURI())); conf.addResource(new Path(ClassLoader.getSystemResource("core-site.xml").toURI())); */ Connection connection=ConnectionFactory.createConnection(conf); return connection; } public static void close(Connection conn) throws IOException { if(null!=conn) conn.close(); } }

  

package hbase;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
public class DataUtil {
    public static Table getTable(Connection conn,String tablename,String namespace) throws IOException {
        TableName tableName1=TableName.valueOf(namespace,tablename);
        return conn.getTable(tableName1);

    }
    public static void put(Connection conn,String tablename,String namespace,String rowkey,String cf,String lie,String value) throws IOException {

        Table table=getTable(conn,tablename,namespace);

        Put put = new Put(Bytes.toBytes(rowkey));

        put.addColumn(Bytes.toBytes(cf),Bytes.toBytes(lie),Bytes.toBytes(value));

        table.put(put);

        table.close();
    }
    public static void get(Connection conn,String tablename,String namespace,String rowkey) throws IOException {

        Table table=getTable(conn,tablename,namespace);

        Get get = new Get(Bytes.toBytes(rowkey));

        Result result=table.get(get);

        if(result!=null){
            Cell[] cells=result.rawCells();
            for (Cell cell :cells){
                String row =Bytes.toString(CellUtil.cloneRow(cell));
                String family =Bytes.toString(CellUtil.cloneFamily(cell));
                String qualifier =Bytes.toString(CellUtil.cloneQualifier(cell));
                String value =Bytes.toString(CellUtil.cloneValue(cell));
                System.out.println(String.format("行:%s  列族: %s  列名:  %s 值: %s",row,family,qualifier,value));
            }


        }
        table.close();
    }

}

  

posted @ 2021-11-18 08:28  小跳不磕脑袋  阅读(340)  评论(0编辑  收藏  举报