Demo entry 6662188

hbase

   

Submitted by anonymous on Nov 25, 2017 at 03:54
Language: Java. Code size: 5.9 kB.

package 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.TableDescriptors;
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 HBaseApp {
	//创建表、插入记录、查询一条记录、遍历所有的记录、删除表
	public static final String TABLE_NAME = "tableName";
	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://hadoop:9000/hbase");//conf.set("hbase.rootdir","http://hadoop:60010/master-status");
		//使用eclipse时必须添加这个,否则无法定位
		conf.set("hbase.zookeeper.quorum", "hadoop");
		//创建表、删除表使用HBaseAdmin
		final HBaseAdmin hBaseAdmin = new HBaseAdmin(conf);
		createTable(hBaseAdmin);
		
		//插入记录、查询一条记录、遍历所有的记录HTable
		final HTable hTable = new HTable(conf, TABLE_NAME);
		putRecord(hTable);
		getRecord(hTable);
		scanTable(hTable);
		hTable.close();
		
		//deleteTable(hBaseAdmin);
	}
	private static void scanTable(final HTable hTable) throws IOException {
		Scan scan = new Scan();
		final ResultScanner scanner = hTable.getScanner(scan);
		for (Result result : scanner) {
			final byte[] value = result.getValue("Information".getBytes(), "age".getBytes());
			System.out.println(result+"\n"+new String(value));
		}
	}
	private static void getRecord(final HTable hTable) throws IOException {
		Get get = new Get("2015001".getBytes());
		final Result result = hTable.get(get);
		final byte[] value = result.getValue("Information".getBytes(), "name".getBytes());
		System.out.println(result+"\n"+new String(value));
	}
	private static void putRecord(final HTable hTable) throws IOException {
		Put put_1 = new Put("2015001".getBytes());
		put_1.add("Math".getBytes(), "SC_Cno".getBytes(), "123001".getBytes());
		put_1.add("Math".getBytes(), "SC_Score".getBytes(), "86".getBytes());
		put_1.add("Math".getBytes(), "C_credit".getBytes(), "2.0".getBytes());
		put_1.add("English".getBytes(), "SC_Cno".getBytes(), "123003".getBytes());
		put_1.add("English".getBytes(), "SC_Score".getBytes(), "69".getBytes());
		put_1.add("English".getBytes(), "C_credit".getBytes(), "3.0".getBytes());
		put_1.add("CS".getBytes(), "SC_Cno".getBytes(), "123002".getBytes());
		put_1.add("CS".getBytes(), "C_credit".getBytes(), "5.0".getBytes());
		put_1.add("Information".getBytes(), "name".getBytes(), "zhangsan".getBytes());
		put_1.add("Information".getBytes(), "gender".getBytes(), "male".getBytes());
		put_1.add("Information".getBytes(), "age".getBytes(), "23".getBytes());
		Put put_2 = new Put("2015002".getBytes());
		put_2.add("Math".getBytes(), "SC_Cno".getBytes(), "123001".getBytes());
		put_2.add("Math".getBytes(), "C_credit".getBytes(), "2.0".getBytes());
		put_2.add("English".getBytes(), "SC_Cno".getBytes(), "123003".getBytes());
		put_2.add("English".getBytes(), "SC_Score".getBytes(), "99".getBytes());
		put_2.add("English".getBytes(), "C_credit".getBytes(), "3.0".getBytes());
		put_2.add("CS".getBytes(), "SC_Cno".getBytes(), "123002".getBytes());
		put_2.add("CS".getBytes(), "SC_Score".getBytes(), "77".getBytes());
		put_2.add("CS".getBytes(), "C_credit".getBytes(), "5.0".getBytes());
		put_2.add("Information".getBytes(), "name".getBytes(), "Mary".getBytes());
		put_2.add("Information".getBytes(), "gender".getBytes(), "female".getBytes());
		put_2.add("Information".getBytes(), "age".getBytes(), "22".getBytes());
		Put put_3 = new Put("2015003".getBytes());
		put_3.add("Math".getBytes(), "SC_Cno".getBytes(), "123001".getBytes());
		put_3.add("Math".getBytes(), "SC_Score".getBytes(), "98".getBytes());
		put_3.add("Math".getBytes(), "C_credit".getBytes(), "2.0".getBytes());
		put_3.add("English".getBytes(), "SC_Cno".getBytes(), "123003".getBytes());
		put_3.add("English".getBytes(), "C_credit".getBytes(), "3.0".getBytes());
		put_3.add("CS".getBytes(), "SC_Cno".getBytes(), "123002".getBytes());
		put_3.add("CS".getBytes(), "SC_Score".getBytes(), "95".getBytes());
		put_3.add("CS".getBytes(), "C_credit".getBytes(), "5.0".getBytes());
		put_3.add("Information".getBytes(), "name".getBytes(), "Lisi".getBytes());
		put_3.add("Information".getBytes(), "gender".getBytes(), "male".getBytes());
		put_3.add("Information".getBytes(), "age".getBytes(), "24".getBytes());
		hTable.put(put_1);
		hTable.put(put_2);
		hTable.put(put_3);
	}
	
	private static void deleteTable(final HBaseAdmin hBaseAdmin)
			throws IOException {
		hBaseAdmin.disableTable(TABLE_NAME);
		hBaseAdmin.deleteTable(TABLE_NAME);
	}
	private static void createTable(final HBaseAdmin hBaseAdmin)
			throws IOException {
		if(hBaseAdmin.tableExists(TABLE_NAME))
		{
			hBaseAdmin.disableTable(TABLE_NAME);
			hBaseAdmin.deleteTable(TABLE_NAME);
		}
		if(!hBaseAdmin.tableExists(TABLE_NAME)){
			HTableDescriptor tableDescriptor = new HTableDescriptor(TABLE_NAME);
			HColumnDescriptor Math = new HColumnDescriptor("Math");
			HColumnDescriptor English = new HColumnDescriptor("English");
			HColumnDescriptor CS = new HColumnDescriptor("CS");
			HColumnDescriptor Information = new HColumnDescriptor("Information");
			tableDescriptor.addFamily(Math);
			tableDescriptor.addFamily(English);
			tableDescriptor.addFamily(CS);
			tableDescriptor.addFamily(Information);
			hBaseAdmin.createTable(tableDescriptor);
		}
	}
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).