Hadoop_Java操作Hbase

Hadoop_Java操作Hbase

package com.lius.hadoop.hbase;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
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.Delete;
import org.apache.hadoop.hbase.client.Durability;
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.ResultScanner;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.log4j.Logger;

/**
 * java操作Hbase
 * @author Administrator
 *
 */
public class operationHbase {

	public static Logger log = Logger.getLogger(operationHbase.class);
	public  static Configuration conf = null;
	private static Connection connection = null;
	//初始化配置
	static {
		conf = HBaseConfiguration.create();
		conf.set("hbase.zookeeper.quorum", "hadoop1,hadoop2");
		conf.set("hbase.zookeeper.property.clientPort", "2181");
	}
	
	public static void main(String[] args) throws IOException {
		
		init();
		//创建表
//		createTable(TableName.valueOf("test"));
		//插入数据
//		insertData("marry","test");
		//删除一张表
		dropTable("t1");
		//根据rowkey删除一条记录
//		deleteRow("xiaoming","test");
		//查询所有数据
//		QueryAll("test");
		//单条件查询,根据rowkey查询唯一一条记录
//		QueryByCondition1("marry","test");
		
		connection.close();
	}

	/**
	 * 删除一张表
	 * @param string
	 * @throws IOException 
	 */
	private static void dropTable(String tableName) throws IOException {
		// TODO Auto-generated method stub
		Admin admin = connection.getAdmin();
		TableName tabName = TableName.valueOf(tableName);
		admin.disableTable(tabName);
		admin.deleteTable(tabName);
	}

	/**
	 * 根据rowkey删除一条记录
	 * @param string
	 * @param string2
	 * @throws IOException 
	 */
	private static void deleteRow(String rowkey, String tableName) throws IOException {
		// TODO Auto-generated method stub
		Table table = connection.getTable(TableName.valueOf(tableName));
		List<Delete> lists = new ArrayList();
		Delete delete = new Delete(rowkey.getBytes());
		lists.add(delete);
		table.delete(lists);
		table.close();
	}

	/**
	 * 单条件查询,根据rowkey查询唯一一条记录
	 * @param string
	 * @param string2
	 * @throws IOException 
	 */
	private static void QueryByCondition1(String rowkey, String tableName) throws IOException {
		// TODO Auto-generated method stub
		Table table = connection.getTable(TableName.valueOf(tableName));
		Get scan = new Get(rowkey.getBytes());
		Result r = table.get(scan);
		List<Cell> cells = r.listCells();
		for(Cell cell:cells) {
			System.out.println(String.format("rowkey:%s family:%s qualifier:%s value:%s", 
					Bytes.toString(CellUtil.cloneRow(cell)),
					Bytes.toString(CellUtil.cloneFamily(cell)),
					Bytes.toString(CellUtil.cloneQualifier(cell)),
					Bytes.toString(CellUtil.cloneValue(cell))));
		}
		table.close();
	}

	/**
	 * 查询所有数据
	 * @param string
	 * @throws IOException 
	 */
	private static void QueryAll(String tableName) throws IOException {
		// TODO Auto-generated method stub
		Table table = connection.getTable(TableName.valueOf(tableName));
		ResultScanner rs = table.getScanner(new Scan());
		for(Result r:rs) {
			System.out.println("获得的rowkey:"+new String(r.getRow()));
			List<Cell> cells = r.listCells();
			for(Cell cell:cells) {
			
				System.out.println(String.format("rowkey:%s family:%s qualifier:%s value:%s ",
						Bytes.toString(CellUtil.cloneRow(cell)),
						Bytes.toString(CellUtil.cloneFamily(cell)),
						Bytes.toString(CellUtil.cloneQualifier(cell)),
						Bytes.toString(CellUtil.cloneValue(cell))));
			}
		}
		table.close();
	}

	private static void init() throws IOException {
		// TODO Auto-generated method stub
		connection = ConnectionFactory.createConnection(conf);
	}

	/**
	 * 插入数据
	 * @param rowkey
	 * @param tableName
	 * @throws IOException 
	 */
	private static void insertData(String rowkey, String tableName) throws IOException {
		// TODO Auto-generated method stub
		Table table = connection.getTable(TableName.valueOf(tableName));  //create Table
		Put put = new Put(rowkey.getBytes());  							  //create Put	
		put.addColumn(Bytes.toBytes("column2"),"screen_width".getBytes(),"1080".getBytes());//addColumn(Family,key,Value)
		put.addColumn("column2".getBytes(), "screen_height".getBytes(), "1920".getBytes());
		put.addColumn("column2".getBytes(), "url".getBytes(), "www.baidu.com".getBytes());
		put.addColumn("column2".getBytes(), "event_data".getBytes(), "12|16|13|17|12|16".getBytes());
		put.setDurability(Durability.SYNC_WAL);
		table.put(put);
		table.close();
	}

	/**
	 * 创建表
	 * @param string
	 * @throws IOException 
	 */
	private static void createTable(TableName tableName) throws IOException {
		// TODO Auto-generated method stub
		log.info("start create table...");
		Admin hbaseAdmin = connection.getAdmin();
		if(hbaseAdmin.tableExists(tableName)) {
			hbaseAdmin.disableTable(tableName);
			hbaseAdmin.deleteTable(tableName);
			log.info(String.format("table %s is exists,delete...", tableName.toString()));
		}
//		TableDescriptor table = TableDescriptorBuilder.newBuilder(tableName).build();
//		table.getColumnFamilies();
		HTableDescriptor hTable = new HTableDescriptor(tableName);
		hTable.addFamily(new HColumnDescriptor("column1"));
		hTable.addFamily(new HColumnDescriptor("column2"));
		hTable.addFamily(new HColumnDescriptor("column3"));
		hbaseAdmin.createTable(hTable);
		log.info(String.format("end create table %s", tableName.toString()));
	}
}

 

好的,关于在HBase数据库中使用Java创建表的方法如下: 首先,需要导入HBaseJava API相关的包: ```java import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; 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.TableDescriptor; import org.apache.hadoop.hbase.client.TableDescriptorBuilder; import org.apache.hadoop.hbase.io.compress.CompressionAlgorithm; import org.apache.hadoop.hbase.regionserver.BloomType; import org.apache.hadoop.hbase.util.Bytes; ``` 然后,通过以下代码建立与HBase数据库的连接: ```java Configuration config = HBaseConfiguration.create(); config.set("hbase.zookeeper.quorum", "localhost"); Connection connection = ConnectionFactory.createConnection(config); Admin admin = connection.getAdmin(); ``` 其中,"localhost"为HBase数据库所在的主机名。接下来,可以使用以下代码创建表: ```java String tableName = "myTable"; TableName name = TableName.valueOf(tableName); TableDescriptor tableDescriptor = TableDescriptorBuilder.newBuilder(name) .setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("cf")) .setCompressionType(CompressionAlgorithm.GZ) .setBloomFilterType(BloomType.ROWCOL) .build()) .build(); admin.createTable(tableDescriptor); ``` 这里创建了一个名为"myTable"的表,并设置了一个名为"cf"的列族,其中设置了压缩算法和Bloom过滤器类型。最后,使用admin.createTable()方法创建表。 需要注意的是,在使用完后需要关闭与HBase数据库的连接: ```java admin.close(); connection.close(); ``` 以上就是使用JavaHBase数据库中创建表的方法,希望对你有所帮助。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值