码迷,mamicode.com
首页 > 其他好文 > 详细

hbase jave编程

时间:2014-07-25 16:48:41      阅读:266      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   java   color   使用   os   

1、搭建环境

  复制linux下已配置搭建好的hbase目录进入windows里面,当然使用linux开发的可以不用。

  新建java工程,将hbase目录下的jar和lib下的jar包添加到类路径下。

  同时增加外部Class Folder将hbase的conf目录增加进来 多数连接操作就是因为没有配置这个部分。

2、OK

  编辑代码,自己试着运行运行吧。

  

package hbase;

import java.util.ArrayList;
import java.util.List;

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.Delete;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.HTableInterface;
import org.apache.hadoop.hbase.client.HTablePool;
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 HbaseDemo {

	public static Configuration conf;
	static{
		conf = HBaseConfiguration.create();
	}
	public static void main(String[] args) throws Exception {
		createTable("test1");
	}
	/**
	 * 查询所有数据
	 * @param tableName
	 * @throws Exception
	 */
	public static void queryAll(String tableName) throws Exception{
		System.out.println("query all start....");
		HTablePool pool = new HTablePool(conf, 1000);
		HTableInterface table = pool.getTable(tableName);
		ResultScanner scanner = table.getScanner(new Scan());
		for (Result result : scanner) {
			System.out.println("rowkey:\t"+new String(result.getRow()));
			for (org.apache.hadoop.hbase.KeyValue kv : result.raw()) {
				System.out.println("column:\t"+new String(kv.getFamily())+":"+new String(kv.getValue()));
			}
		}
	}
	/**
	 * 删除行
	 * @param tableName
	 * @param rowkey
	 * @throws Exception
	 */
	public static void deleteRow(String tableName,String rowkey) throws Exception{
		HTable table = new HTable(conf, tableName);
		List list = new ArrayList();
		Delete d1 = new Delete(rowkey.getBytes());
		list.add(d1);
		table.delete(list);
		System.out.println("delete row end ....");
	}
	/**
	 * 删除表
	 * @param tableName
	 * @throws Exception
	 */
	public static void dropTable(String tableName) throws Exception{
		HBaseAdmin admin = new HBaseAdmin(conf);
		admin.disableTable(tableName);
		admin.deleteTable(tableName);
		admin.close();
	}
	/**
	 * 插入数据
	 * @param tableName
	 * @throws Exception
	 */
	public static void insertData(String tableName) throws Exception{
		System.out.println("insert data start.....");
		HTablePool pool = new HTablePool(conf, 1000);
		HTableInterface table = pool.getTable(tableName);
		Put put = new Put("row001".getBytes());
		put.add("tf1".getBytes(), null, "aaa".getBytes());
		put.add("tf2".getBytes(), null, "aaa".getBytes());
		put.add("tf3".getBytes(), null, "aaa".getBytes());
		table.put(put);
		System.out.println("insert data end.....");
	}
	/**
	 * 创建表
	 * @param tableName
	 * @throws Exception
	 */
	public static void createTable(String tableName) throws Exception{
		System.out.println("create table ....");
		HBaseAdmin admin = new HBaseAdmin(conf);
		// 如果存在要创建的表,那么先删除,再创建
		if(admin.tableExists(tableName)){
			admin.disableTable(tableName);
			admin.deleteTable(tableName);
			System.out.println(tableName+" table exists,delete ....");
		}
		HTableDescriptor descriptor = new HTableDescriptor(tableName);
		descriptor.addFamily(new HColumnDescriptor("tf1"));
		descriptor.addFamily(new HColumnDescriptor("tf2"));
		descriptor.addFamily(new HColumnDescriptor("tf3"));
		admin.createTable(descriptor);
		admin.close();
		System.out.println("create table finished....");
	}
}

 3、linux hbase shell

  >list

  列出所有的table

  >status

  >version

  >scan ‘tableName‘

  浏览数据

  》describe ‘tableName‘

  >descrive extended ‘tableName‘

  ......................

  参考:http://javacrazyer.iteye.com/blog/1186881

hbase jave编程,布布扣,bubuko.com

hbase jave编程

标签:des   style   blog   http   java   color   使用   os   

原文地址:http://www.cnblogs.com/jsunday/p/3867920.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!