holamundo hbase

Recientemente haciendo un proyectos relacionados con Hadoop-, no tiene tiempo para procesar los datos, el estudio hbase hoy ante la demo colocado:

package com.bdds.utlis;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Map.Entry;

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.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.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.util.Bytes;

public class HBaseUtilTest {
	public Configuration conf;

	static {
		// HBASE_HOME
		File workaround = new File(".");
		System.getProperties().put("hadoop.home.dir", workaround.getAbsolutePath());
		new File("./bin").mkdirs();
		try {
			new File("./bin/winutils.exe").createNewFile();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public HBaseUtilTest() {
		conf = HBaseConfiguration.create();
		conf.set("hbase.master", "192.168.1.55:60000");
		conf.set("hbase.zookeeper.quorum", "192.168.1.64");
	}

	public static void main(String args[]) throws Exception {
		String tableName = "model";
		String[] columnFamily = { "fa_1","fa_2" };
		new HBaseUtilTest().createTable(tableName, columnFamily);

		// 多列族,多字段插入
		Map<String, Map<String, Object>> maps = new HashMap<String, Map<String,Object>>();
		// 列值集合
		Map<String, Object> columnv1 = new HashMap<String, Object>();
		columnv1.put("field1", "测试-1");
		columnv1.put("field2", "测试-3");
		columnv1.put("field3", "测试-3");
		Map<String, Object> columnv2 = new HashMap<String, Object>();
		columnv2.put("field1", "测试-1");
		columnv2.put("field2", "测试-2");
		columnv2.put("field3", "测试-3");
		Map<String, Object> columnv3 = new HashMap<String, Object>();
		columnv3.put("field1", "测试-1");
		columnv3.put("field2", "测试-2");
		columnv3.put("field3", "测试-3");
		maps.put("baseAge", columnv1);
		maps.put("baseSex", columnv2);
		maps.put("baseAddress", columnv3);
		new HBaseUtilTest().insertData2(tableName, maps);
		
 		new HBaseUtilTest().listTable();
 		
		String[] columnValue = {"不一样","0","233","机车"};
		String[] column = {"address3","sex3","age2","aihao"};
		new HBaseUtilTest().insertData(tableName, "1", column, columnValue, "baseAddress");
		
		new HBaseUtilTest().dropTable(tableName);
		new HBaseUtilTest().deleteRow(tableName, "1");
		new HBaseUtilTest().getAllRow(tableName);
		new HBaseUtilTest().getAllRow("algorithm");
		new HBaseUtilTest().getRowByCondition(tableName);
		new HBaseUtilTest().getRowByManyCondition(tableName);
	}

	/**
	 * 创建数据表
	 * @param tableName 表名
	 * @param cols 列族数组
	 * @throws Exception
	 */
	public void createTable(String tableName, String[] columnFamily) throws Exception {
		HBaseAdmin ha = new HBaseAdmin(conf);
		try {
			if (!ha.tableExists(tableName)) {
				HTableDescriptor table = new HTableDescriptor(TableName.valueOf(tableName));
				for (String column : columnFamily) {
					table.addFamily(new HColumnDescriptor(column));
				}
				ha.createTable(table);
				System.out.println("创建成功");
			} else {
				System.out.println("表已存在");
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			ha.close();
		}
	}

	/**
	 * 删除表
	 * @param tableName 便民
	 * @throws Exception
	 */
	public void dropTable(String tableName) throws Exception {
		HBaseAdmin ha = new HBaseAdmin(conf);
		ha.disableTable(tableName);
		ha.deleteTable(tableName);
	}

	/**
	 * 查询hbase下所有表
	 * @throws Exception
	 */
	public TableName[] listTable() throws Exception {
		HBaseAdmin ha = new HBaseAdmin(conf);
		TableName[] tableNames = ha.listTableNames();
		return tableNames;
	}

	/**
	 * 根据表查询列族
	 * @throws Exception
	 */
	public HColumnDescriptor[] getListColumnByTable(String tableName) throws Exception {
		HBaseAdmin ha = new HBaseAdmin(conf);
		HTableDescriptor table = ha.getTableDescriptor(TableName.valueOf(tableName));
		HColumnDescriptor[] columns = table.getColumnFamilies();
		return columns;
	}

	/**
	 * 多列族,多字段插入数据
	 */
	public void insertData2(String tableName, Map<String, Map<String, Object>> maps) throws Exception {
		HTable table = new HTable(conf, tableName);
		Put put = new Put(Bytes.toBytes(new Random().nextInt(99999)));
		for (Entry<String, Map<String, Object>> colu : maps.entrySet()) {
			for (Entry<String, Object> item : colu.getValue().entrySet()) {
				put.add(Bytes.toBytes(colu.getKey()), Bytes.toBytes(item.getKey()), Bytes.toBytes(item.getValue().toString()));
			}
		}
		table.put(put);
		System.out.println("插入数据成功...");
	}

	/**
	 * 插入数据
	 */
	public void insertData(String tableName, String rowKey, String[] column, String[] columnValue, String columnFamily) throws Exception {
		HTable table = new HTable(conf, tableName);
		Put put = new Put(rowKey.getBytes());
		for (int i = 0; i < column.length; i++) {
			put.add(Bytes.toBytes(columnFamily), column[i].getBytes(), columnValue[i].getBytes());
		}
		table.put(put);
		System.out.println("插入数据成功...");
	}

	/**
	 * 获取一行数据
	 */
	public void getRow(String tableName, String rowKey) throws Exception {
		HTable table = new HTable(conf, tableName);
		Get get = new Get(rowKey.getBytes());
		Result result = table.get(get);
		for (Cell cell : result.rawCells()) {
			System.out.println("row family-" + new String(CellUtil.cloneFamily(cell)) 
							+ " row column-" + new String(CellUtil.cloneQualifier(cell))
							+ " row value-" + new String(CellUtil.cloneValue(cell)));
		}
	}

	/**
	 * 删除一行数据
	 */
	public void deleteRow(String tableName, String key) throws Exception {
		HTable table = new HTable(conf, tableName);
		Delete de = new Delete(key.getBytes());
		table.delete(de);
	}

	/**
	 * 获取表所有数据
	 */
	public void getAllRow(String tableName) throws Exception {
		HTable table = new HTable(conf, tableName);
		Scan s = new Scan();
		ResultScanner rs = table.getScanner(s);
		for (Result result : rs) {
			for (Cell cell : result.rawCells()) {
				System.out.println("row key-" + new String(CellUtil.cloneRow(cell))
								+ " row family-" + new String(CellUtil.cloneFamily(cell))
								+ " row column-" + new String(CellUtil.cloneQualifier(cell))
								+ " row value-" + new String(CellUtil.cloneValue(cell)));
			}
		}
	}

	/**
	 * 根据条件查询记录
	 */
	public void getRowByCondition(String tableName) throws Exception {
		HTable table = new HTable(conf, tableName);
		// 列族  列名  比较符号 比较值
		Filter filter = new SingleColumnValueFilter(Bytes.toBytes("baseSex"), Bytes.toBytes("sex"), CompareOp.EQUAL, Bytes.toBytes("1"));
		Scan s = new Scan();
		s.setFilter(filter);
		ResultScanner rs = table.getScanner(s);
		for (Result result : rs) {
			for (Cell cell : result.rawCells()) {
				System.out.println("row key-" + new String(CellUtil.cloneRow(cell))
								+ " row family-" + new String(CellUtil.cloneFamily(cell))
								+ " row column-" + new String(CellUtil.cloneQualifier(cell))
								+ " row value-" + new String(CellUtil.cloneValue(cell)));
			}
		}
	}

	/**
	 * 多条件查询
	 */
	public void getRowByManyCondition(String tableName) throws Exception {
		HTable table = new HTable(conf, tableName);
		// 列族(fa_1)下的列名(age)=16
		Filter filterSex = new SingleColumnValueFilter(Bytes.toBytes("fa_1"), Bytes.toBytes("age"), CompareOp.EQUAL, Bytes.toBytes("16"));
		// 列族(fa_2)下的列名(sex)=1
		Filter filterAge = new SingleColumnValueFilter(Bytes.toBytes("fa_2"), Bytes.toBytes("sex"), CompareOp.EQUAL, Bytes.toBytes("1"));
		List<Filter> filterList = new ArrayList<Filter>();
		filterList.add(filterAge);
		filterList.add(filterSex);

		Scan s = new Scan();
		FilterList filterListS = new FilterList(filterList);
		s.setFilter(filterListS);

		// 设置查询位置 rowkey
		s.setStartRow("1".getBytes());
		s.setStopRow("5".getBytes());

		ResultScanner rs = table.getScanner(s);
		for (Result result : rs) {
			for (Cell cell : result.rawCells()) {
				System.out.println("row key-" + new String(CellUtil.cloneRow(cell))
								+ " row family-" + new String(CellUtil.cloneFamily(cell))
								+ " row column-" + new String(CellUtil.cloneQualifier(cell))
								+ " row value-" + new String(CellUtil.cloneValue(cell)));
			}
		}
	}
}


Publicado 90 artículos originales · ganado elogios 21 · vistas 470 000 +

Supongo que te gusta

Origin blog.csdn.net/yx13649017813/article/details/48786535
Recomendado
Clasificación