Eclipse 操作Hbase 代码一
来源:互联网 发布:qq视频录像软件注册码 编辑:程序博客网 时间:2024/04/30 15:05
package com.gense;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.KeyValue;import org.apache.hadoop.hbase.client.Delete;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;import org.apache.hadoop.hbase.util.Bytes;public class HbaseTest_01{ private static Configuration conf = null; /** * static hadoop configuration object */ static { conf = HBaseConfiguration.create(); conf.set("hbase.zookeeper.quorum", "192.168.146.129"); } /** * create hbase table * @param tableName * @param columnFamilys * @throws IOException * @see [类、类#方法、类#成员] */ private static void createTable(String tableName,String[] columnFamilys) throws IOException { HBaseAdmin hbaseAdmin = new HBaseAdmin(conf); //判断tableName是否存在 if(hbaseAdmin.tableExists(tableName)) { System.out.println("the "+tableName +" is exists"); } else { HTableDescriptor tableDesc = new HTableDescriptor(tableName); for(String column:columnFamilys) { tableDesc.addFamily(new HColumnDescriptor(column)); } hbaseAdmin.createTable(tableDesc); System.out.println("create table "+tableName+" success"); } } /** * delete hbase table * @param tableName * @throws IOException * @see [类、类#方法、类#成员] */ private static void deleteTable(String tableName) throws IOException { HBaseAdmin admin = new HBaseAdmin(conf); //判断tableName是否存在 if(!admin.tableExists(tableName)) { System.out.println("the "+tableName+" not exists"); } else { admin.disableTable(tableName); admin.deleteTable(tableName); System.out.println("the "+tableName+" delete success"); } } /** * insert data into table * @param tableName * @param row * @param family * @param qualifier * @param value * @throws IOException * @see [类、类#方法、类#成员] */ private static void put(String tableName,String row,String family,String qualifier,String value) throws IOException { HTable hTable = new HTable(conf, tableName); Put put = new Put(Bytes.toBytes(row)); put.add(Bytes.toBytes(family), Bytes.toBytes(qualifier), Bytes.toBytes(value)); hTable.put(put); System.out.println("insert record "+row +" to table "+tableName+" success"); } /** * delete record * @param tableName * @param rowkey * @throws IOException * @see [类、类#方法、类#成员] */ private static void delete(String tableName,String rowkey) throws IOException { HTable hTable = new HTable(conf, tableName); Delete delete = new Delete(Bytes.toBytes(rowkey)); hTable.delete(delete); System.out.println("delete record "+rowkey +" from table "+tableName+" success"); } /** * get data from table * @param tableName * @param rowkey * @param family * @param qualifier * @return * @throws IOException * @see [类、类#方法、类#成员] */ private static void get(String tableName,String rowkey,String family,String qualifier) throws IOException { HTable table = new HTable(conf, tableName); Get get = new Get(Bytes.toBytes(rowkey)); //column family if(family!=null) { //qualifier if(qualifier!=null) { get.addColumn(Bytes.toBytes(family),Bytes.toBytes(qualifier)); }else { get.addFamily(Bytes.toBytes(family)); } } Result result = table.get(get); for(KeyValue value : result.raw()) { System.out.println(new String(value.getRow())+"\t"+new String(value.getFamily())+":"+new String(value.getQualifier())+"\t"+new String(value.getValue())+"\t"+value.getTimestamp()); } } /** * scan all data * @param tableName * @throws IOException * @see [类、类#方法、类#成员] */ private static void scan(String tableName) throws IOException { HTable table = new HTable(conf, tableName); //defined scan Scan scan = new Scan(); //scan table ResultScanner scanner = table.getScanner(scan); for(Result result : scanner) { for(KeyValue value : result.raw()) { System.out.println(new String(value.getRow())+"\t"+new String(value.getFamily())+":"+new String(value.getQualifier())+"\t"+new String(value.getValue())+"\t"+value.getTimestamp()); } } } /** <一句话功能简述> * <功能详细描述> * @param args * @throws IOException * @see [类、类#方法、类#成员] */ public static void main(String[] args) throws IOException { //hbase create table //createTable("test1", new String[]{"baseinfo"}); //delete table //deleteTable("test"); //insert data //put("test1", "kitty", "baseinfo", "", "kitty"); //put("test1", "kitty", "baseinfo", "age", "22"); //delete("test1", "kitty"); //get data //get("test1","kitty",null,null); //scan all data scan("test1"); } }
0 0
- Eclipse 操作Hbase 代码一
- Java客户端操作HBase代码
- 一 HBase基础API操作
- 在Eclipse中运行JAVA代码远程操作HBase的示例
- 在Eclipse中运行JAVA代码远程操作HBase的示例
- eclipse 连接并操作单机版Hbase
- HBase的eclipse操作环境配置
- HBase的eclipse操作Java API
- eclipse提交mr(操作hbase)出错
- Spark访问HBase的Eclipse代码实现
- 【hbase】HBase基本数据操作代码实现详解
- eclipse操作(一)
- hbase 0.94一些基本操作测试代码
- JAVA API 操作HBASE(一)
- HBase基本代码实现(一)
- eclipse + hbase
- 简单Eclipse操作(一)
- Hbase的安装及配置、eclipse 操作示例
- jquery美化插件uniform
- NSRecursiveLock
- android 大尺寸图片处理
- 一元二次方程求解
- IDR机制
- Eclipse 操作Hbase 代码一
- 读《烽烟中的敏捷》有感
- 【LeetCode从零单排】No83 Remove Duplicates from Sorted List
- fgets()学习
- android bitmap缓存机制(下)
- JAVA技术路线图
- Request的getParameter和getAttribute方法的区别
- DBus 入门与应用 -- DBus 的 C 编程接口
- IDR--Integer ID Management