首页 > 代码库 > JavaHbase连接代码示例
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连接代码示例
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。