`
lwx522
  • 浏览: 35153 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

hbase客户端调用实例

阅读更多
package com.dis.test.hbase;

import java.io.IOException;
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.KeyValue;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
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 HbaseClientTest {
	private static Configuration conf = HBaseConfiguration.create();
	private static String[] cfs = new String[] { "f1", "f2" };

	public static void main(String[] args) throws IOException {
		HbaseClientTest test = new HbaseClientTest();
		//test.createTable("test2", cfs);
		//test.deleteTable("test2");
		//test.writeRow("test2", new String[]{"f1", "f2"});
		//test.selectRow("test2", "rows1");
		//test.scaner("test2");
		test.deleteRow("test2", "rows1");
	}

	public void createTable(String tablename, String[] cfs) throws IOException {
		HBaseAdmin admin = new HBaseAdmin(conf);
		if (admin.tableExists(tablename)) {
			System.out.println("表已经存在");
			return;
		}
		HTableDescriptor desc = new HTableDescriptor(tablename);
		for (String f : cfs) {
			desc.addFamily(new HColumnDescriptor(f));
		}
		admin.createTable(desc);
		System.out.println("表创建成功");
	}


	/**
	 * 删除表操作 
	 * @param tablename
	 * @throws IOException
	 */
	public void deleteTable(String tablename) throws IOException {
		try {
			HBaseAdmin admin = new HBaseAdmin(conf);
			admin.disableTable(tablename);
			admin.deleteTable(tablename);
			System.out.println("表删除成功!");
		} catch (MasterNotRunningException e) {
			e.printStackTrace();
		} catch (ZooKeeperConnectionException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 插入一条数据
	 * @param tablename
	 * @param cfs
	 */
	public void writeRow(String tablename, String[] cfs) {
		try {
			HTable table = new HTable(conf, tablename);
			Put put = new Put(Bytes.toBytes("rows1"));
			for (int j = 0; j < cfs.length; j++) {
				put.add(Bytes.toBytes(cfs[j]),Bytes.toBytes(String.valueOf(1)),Bytes.toBytes("value_1"));
				table.put(put); 
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 删除一行数据
	 * @param tablename
	 * @param rowkey
	 * @throws IOException
	 */
	public void deleteRow(String tablename, String rowkey) throws IOException {
		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("删除行成功!");
	}
	
	/**
	 * 查找一行数据
	 * @param tablename
	 * @param rowKey
	 * @throws IOException
	 */
	public static void selectRow(String tablename, String rowKey)
		throws IOException {
		HTable table = new HTable(conf, tablename);
		Get g = new Get(rowKey.getBytes());
		Result rs = table.get(g);
		for (KeyValue kv : rs.raw()) {
			System.out.print(new String(kv.getRow()) + "  ");
			System.out.print(new String(kv.getFamily()) + ":");
			System.out.print(new String(kv.getQualifier()) + "  ");
			System.out.print(kv.getTimestamp() + "  ");
			System.out.println(new String(kv.getValue()));
		}
	}
	
	/**
	 * 查询表中所有行
	 * @param tablename
	 */
	public void scaner(String tablename) {
		try {
			HTable table = new HTable(conf, tablename);
			Scan s = new Scan();
			ResultScanner rs = table.getScanner(s);
			for (Result r : rs) {
				KeyValue[] kv = r.raw();
				for (int i = 0; i < kv.length; i++) {
					System.out.print(new String(kv[i].getRow()) + "  ");
					System.out.print(new String(kv[i].getFamily()) + ":");
					System.out.print(new String(kv[i].getQualifier()) + "  ");
					System.out.print(kv[i].getTimestamp() + "  ");
					System.out.println(new String(kv[i].getValue()));
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

 

 

需要把hbase-site.xml文件复制到classpath下,依赖的jar如下,都可以在hbase的发布包中找到

 

 <classpathentry kind="lib" path="lib/commons-lang-2.5.jar"/>
 <classpathentry kind="lib" path="lib/hadoop-core-1.0.3.jar"/>
 <classpathentry kind="lib" path="lib/hbase-0.94.2.jar"/>
 <classpathentry kind="lib" path="lib/commons-logging-1.1.1.jar"/>
 <classpathentry kind="lib" path="lib/log4j-1.2.16.jar"/>
 <classpathentry kind="lib" path="lib/slf4j-api-1.5.11.jar"/>
 <classpathentry kind="lib" path="lib/slf4j-jdk14-1.5.11.jar"/>
 <classpathentry kind="lib" path="lib/commons-configuration-1.9.jar"/>
 <classpathentry kind="lib" path="lib/zookeeper-3.4.3.jar"/>
 <classpathentry kind="lib" path="lib/protobuf-java-2.4.0a.jar"/>

 

<!--StartFragment -->
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics