用java API来操作HBase
来源:互联网 发布:电影 人工智能 编辑:程序博客网 时间:2024/06/05 06:44
我们在日常工作中,用eclipse来编写具体的程序,来操作HBase数据库,首先在eclispe中配置HBase环境
新建项目--》导入HBase中的jar包
下面直接贴代码
package com.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.HTableDescriptor;import org.apache.hadoop.hbase.client.Get;import org.apache.hadoop.hbase.client.HBaseAdmin;import org.apache.hadoop.hbase.client.HTable;import org.apache.hadoop.hbase.client.Put;import org.apache.hadoop.hbase.client.Result;import org.apache.hadoop.hbase.client.ResultScanner;import org.apache.hadoop.hbase.client.Scan;public class HBaseTest {public static final String TABLE_NAME = "table1";public static final String FAMILY_NAME = "family1";public static final String ROW_KEY = "rowkey1";public static void main(String[] args) throws Exception {Configuration conf = HBaseConfiguration.create();conf.set("hbase.rootdir", "hdfs://master.dragon.org:9000/hbase");// 使用eclipse,必须使用这个配置定位conf.set("hbase.zookeeper.quorum", "master.dragon.org");// HBaseAdmin用于创建表和删除表HBaseAdmin admin = new HBaseAdmin(conf);createTable(admin);//创建表deleteTable(admin);//删除表// HTable适用于添加记录,查看记录HTable hTable = new HTable(conf, TABLE_NAME);addRecord(hTable);//添加记录getRecord(hTable);//得到记录scannerTable(hTable);//全表扫描hTable.close();}private static void scannerTable(HTable hTable) throws IOException {Scan scan = new Scan();ResultScanner scanner = hTable.getScanner(scan);for (Result result : scanner) {byte[] value = result.getValue(FAMILY_NAME.getBytes(), "info".getBytes());System.out.println(result + "\t" + new String(value));}}private static void getRecord(HTable hTable) throws IOException {Get get = new Get(ROW_KEY.getBytes());Result result = hTable.get(get);byte[] value = result.getValue(FAMILY_NAME.getBytes(), "info".getBytes());System.out.println(result + "\t" + new String(value));}private static void addRecord(HTable hTable) throws IOException {Put put = new Put(ROW_KEY.getBytes());put.add(FAMILY_NAME.getBytes(), "info".getBytes(), "50".getBytes());hTable.put(put);}private static void deleteTable(HBaseAdmin admin) throws IOException {admin.disableTable(TABLE_NAME);admin.deleteTable(TABLE_NAME);}private static void createTable(HBaseAdmin admin) throws IOException {if (!admin.tableExists(TABLE_NAME)) {HTableDescriptor desc = new HTableDescriptor(TABLE_NAME);HColumnDescriptor family = new HColumnDescriptor(FAMILY_NAME);desc.addFamily(family);admin.createTable(desc);}}}
0 0
- 用java API来操作HBase
- hbase java api操作
- hbase java api操作
- hbase Java Api 操作
- hbase java api操作
- Hbase-Java API操作
- Java API 操作Hbase
- Java API操作hbase
- java api操作 hbase
- java api操作hbase
- java api操作hbase
- 用JAVA的API操作HBASE
- hbase编程:通过Java api操作hbase
- Hbase Java Api 基本操作
- HBase和Java API操作
- 使用 Java API 操作 HBase
- 使用用Phoenix的Java api操作HBase
- HBase Java API使用操作例子
- GCD
- iOS - UISearchController搜索框与NSPredicate谓词
- Onvif 资料收集
- 支付系统开发中可能遇到的问题
- 在AIX环境下解压tar.gz文件报错tar: Not a recognized flag: z
- 用java API来操作HBase
- mysqlbinlog 查看二进制日志
- AngularJs $apply
- centos MYSQL 主从同步配置
- APKTool生成APK包时遇到的错误
- 简单的C#导出word(一)
- js实现360度拖拽图片
- 信号完整性:信号反射
- [Android实例] Handler+ExecutorService(线程池)+MessageQueue模式+缓存模式