JavaHbase连接代码示例

Posted zhzhang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaHbase连接代码示例相关的知识,希望对你有一定的参考价值。

package com.rokid.hbase;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HConstants;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Table;

public class HbaseModify
{
    private static final String TABLE_NAME = "speech1";
    private static final String CF_DEFAULT = "cf";
    private static final byte[] QUALIFIER = "col1".getBytes();
    private static final byte[] ROWKEY = "rowkey1".getBytes();
    
    public static void main( String[] args )
    {
        Configuration config = HBaseConfiguration.create();
        String zkAddress = "zk1:2181,zk2:2181,zk3:2181";
        config.set(HConstants.ZOOKEEPER_QUORUM, zkAddress);
        Connection connection = null;

        try {
            connection = ConnectionFactory.createConnection(config);
            HTableDescriptor tableDescriptor = new HTableDescriptor(TableName.valueOf(TABLE_NAME));
            tableDescriptor.addFamily(new HColumnDescriptor(CF_DEFAULT));
            System.out.println("Creating table.");
            Admin admin = connection.getAdmin();
            admin.createTable(tableDescriptor);
            System.out.print("Done.");
            Table table = connection.getTable(TableName.valueOf(TABLE_NAME));
            try {
                Put put = new Put(ROWKEY);
                put.addColumn(CF_DEFAULT.getBytes(), QUALIFIER, "this is value".getBytes());
                table.put(put);
                Get get = new Get(ROWKEY);
                Result r = table.get(get);
                byte[] b = r.getValue(CF_DEFAULT.getBytes(), QUALIFIER); // returns current version of value
                System.out.println(new String(b));
            } finally {
                if (table != null) 
                    table.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (connection != null){
                try {
                    connection.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

备份,待续,供学习参考。。。

以上是关于JavaHbase连接代码示例的主要内容,如果未能解决你的问题,请参考以下文章

处理屏幕旋转上的片段重复(带有示例代码)

这些角度电子邮件指令代码片段如何连接

html PHP代码片段: - AJAX基本示例:此代码演示了使用PHP和JavaScript实现的基本AJAX功能。

LockSupport.java 中的 FIFO 互斥代码片段

Python 向 Postman 请求代码片段

使用实体框架迁移时 SQL Server 连接抛出异常 - 添加代码片段