HBase API 实战

BulkLoad

package com.chinahadoop.testbasicapi;

import java.io.IOException;
import java.util.Arrays;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.KeyValue;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.HFileOutputFormat;
import org.apache.hadoop.hbase.mapreduce.KeyValueSortReducer;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

@SuppressWarnings("deprecation")
public class BulkLoad {

	public static class BulkLoadMapper extends Mapper<LongWritable, Text, ImmutableBytesWritable, KeyValue>  {
		@Override
		protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
			String[] values = value.toString().split("\t");
			System.out.println(Arrays.toString(values));
			byte[] row = Bytes.toBytes(values[0]);
			ImmutableBytesWritable k = new ImmutableBytesWritable(row);
			KeyValue kvProtocol = new KeyValue(row, Bytes.toBytes("testfml"), null, values[1].getBytes());
			context.write(k, kvProtocol);
		}
	}

	public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
		Configuration conf = HBaseConfiguration.create();
		Job job = new Job(conf, "BulkLoad");
		
		job.setJarByClass(BulkLoad.class);
		job.setOutputKeyClass(ImmutableBytesWritable.class);
		job.setOutputValueClass(KeyValue.class);
		job.setMapperClass(BulkLoadMapper.class);
		job.setReducerClass(KeyValueSortReducer.class);
		
//		job.setOutputFormatClass(HFileOutputFormat2.class);
		job.setOutputFormatClass(HFileOutputFormat.class);
		HTable table = new HTable(conf, "test_table");
		HFileOutputFormat.configureIncrementalLoad(job, table);
		FileInputFormat.addInputPath(job, new Path("/user/hbase/bulkloadfile2"));
		FileOutputFormat.setOutputPath(job, new Path("/user/hbase/bulkloadfile2res"));

		System.exit(job.waitForCompletion(true) ? 0 : 1);
	}
}

FixRepair

package com.chinahadoop.testrepair;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HConstants;
import org.apache.hadoop.hbase.HRegionInfo;
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;
import org.apache.hadoop.hbase.util.Writables;


public class FixRepair {

	public static void main(String[] args) throws Exception {
		Configuration conf = HBaseConfiguration.create();
		HTable table = new HTable(conf, Bytes.toBytes(".META."));
		Scan scan = new Scan();
		scan.addColumn(HConstants.CATALOG_FAMILY, HConstants.REGIONINFO_QUALIFIER);
		ResultScanner scanner = table.getScanner(scan);
		Result rs = null;
		Map<byte[], List<HRegionInfo>> startKeyToRegionInfos = new TreeMap<byte[], List<HRegionInfo>>(Bytes.BYTES_COMPARATOR);
		while ((rs = scanner.next()) != null) {
			HRegionInfo regionInfo = Writables.getHRegionInfo(
					rs.getValue(HConstants.CATALOG_FAMILY, HConstants.REGIONINFO_QUALIFIER));
			if (args[0].equals(regionInfo.getTableDesc().getNameAsString())) {
				byte[] startKey = regionInfo.getStartKey();
				List<HRegionInfo> infos = startKeyToRegionInfos.get(startKey);
				if (infos == null) {
					infos = new LinkedList<HRegionInfo>();
					startKeyToRegionInfos.put(startKey, infos);
				}
				infos.add(regionInfo);
			}
		}
		HBaseAdmin ha = new HBaseAdmin(conf);
		FileSystem fs = FileSystem.get(conf);
		for (List<HRegionInfo> infos : startKeyToRegionInfos.values()) {
			if (infos.size() > 1) {
				HRegionInfo parentRegion = (Bytes.compareTo(infos.get(0).getEndKey(), infos.get(1).getEndKey()) > 0) ?
						infos.get(0) : infos.get(1);
				HRegionInfo subRegionInfo = (infos.get(0) == parentRegion) ? infos.get(1) : infos.get(0);
				List<HRegionInfo> foundRegionInfos = new LinkedList<HRegionInfo>();
				foundRegionInfos.add(subRegionInfo);
				while (Bytes.compareTo(subRegionInfo.getEndKey(), parentRegion.getEndKey()) <= 0) {
					List<HRegionInfo> next = startKeyToRegionInfos.get(subRegionInfo.getEndKey());
					if (next == null) {
						break;
					}
					foundRegionInfos.add(next.get(0));
					subRegionInfo = next.get(0);
				}
				if (Bytes.compareTo(subRegionInfo.getEndKey(), parentRegion.getEndKey()) == 0) {
					for (HRegionInfo regionInfo : foundRegionInfos) {
						regionInfo.setOffline(true);
						Put p = new Put(regionInfo.getRegionName());
						p.add(HConstants.CATALOG_FAMILY, HConstants.REGIONINFO_QUALIFIER, 
								Writables.getBytes(regionInfo));
						table.put(p);
						table.flushCommits();
						ha.closeRegion(regionInfo.getRegionName());
					}
					Path parentPath = new Path("/hbase/" + args[0] + "/" + parentRegion.getEncodedName());
					for (HColumnDescriptor fmlDesc : parentRegion.getTableDesc().getFamilies()) {
						Path parentFmlPath = new Path(parentPath, fmlDesc.getNameAsString());
						for (HRegionInfo regionInfo : foundRegionInfos) {
							Path subPath = new Path("/hbase/" + args[0] + "/" + regionInfo.getEncodedName());
							Path subFmlPath = new Path(subPath, fmlDesc.getNameAsString());
							for (FileStatus storeFile : fs.listStatus(subFmlPath)) {
								Path storeFilePath = storeFile.getPath();
								if (!storeFilePath.getName().contains(".")) {
									fs.rename(storeFilePath, new Path(parentFmlPath, storeFilePath.getName())); 
								}
							}
						}
					}
					ha.closeRegion(parentRegion.getRegionName());
				} else {
					throw new Exception("Can not find right subregion set of " + parentRegion.toString());
				}
			}
		}
	}

}


RepareMeta

package com.chinahadoop.testrepair;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HConstants;
import org.apache.hadoop.hbase.HRegionInfo;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.util.Writables;

public class RepareMeta {

	public static void main(String[] args) throws IOException {
		String regionFilePath = args[0];
		Configuration conf = HBaseConfiguration.create();
		HTable table = new HTable(conf, Bytes.toBytes(".META.")); 
		FileSystem fs = FileSystem.get(conf);
		FSDataInputStream in = fs.open(new Path(regionFilePath));
		HRegionInfo regionInfo = new HRegionInfo();
		regionInfo.readFields(in);
		System.out.println(regionInfo.toString());
		Put p = new Put(regionInfo.getRegionName());
		p.add(HConstants.CATALOG_FAMILY, HConstants.REGIONINFO_QUALIFIER, 
				Writables.getBytes(regionInfo));
		table.put(p);
		table.close();
		in.close();
		fs.close();
	}
	
}

TestAdvanceApi

package com.chinahadoop.testbasicapi;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Increment;
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.BinaryComparator;
import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.filter.RowFilter;
import org.apache.hadoop.hbase.util.Bytes;

public class TestAdvanceApi {

	public static void main(String[] args) throws IOException {
		Configuration conf = HBaseConfiguration.create();
		HTable table = new HTable(conf, "test_table");
		Scan scan = new Scan();
		scan.setFilter(new RowFilter(CompareOp.EQUAL, new BinaryComparator(Bytes.toBytes("test_key3"))));
		ResultScanner scanner = table.getScanner(scan);
		Result resultInScan = null;
		while ((resultInScan = scanner.next()) != null) {
			String key = Bytes.toString(resultInScan.getRow());
			String value = Bytes.toString(resultInScan.getValue(Bytes.toBytes("testfml"), null));
			System.out.println("key:" + key);
			System.out.println("value:" + value); 
		}
		scanner.close();
		table.incrementColumnValue(Bytes.toBytes("test_key3"), Bytes.toBytes("testfml"), Bytes.toBytes("testcounter"), 1L);
		Increment icr = new Increment(Bytes.toBytes("test_key3"));
		icr.addColumn(Bytes.toBytes("testfml"), Bytes.toBytes("testcounter"), 1L);
		table.increment(icr);
		Get get = new Get(Bytes.toBytes("test_key3"));
		get.addColumn(Bytes.toBytes("testfml"), Bytes.toBytes("testcounter"));
		Result result = table.get(get);
		String key = Bytes.toString(result.getRow());
		long value = Bytes.toLong(result.getValue(Bytes.toBytes("testfml"), Bytes.toBytes("testcounter")));
		System.out.println("key:" + key);
		System.out.println("value:" + value); 
		table.close();
	}
	
}


TestMrApi

package com.chinahadoop.testbasicapi;

import java.io.IOException;

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.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableInputFormat;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import org.apache.hadoop.hbase.mapreduce.TableMapper;
import org.apache.hadoop.hbase.mapreduce.TableReducer;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.mapreduce.Job;

public class TestMrApi {
	public static void main(String[] args) throws Exception {
		Configuration conf = HBaseConfiguration.create();
		Job job = createSubmittableJob(conf);
		if (job != null) {
			System.exit(job.waitForCompletion(true) ? 0 : 1);
		}
	}
	static class TestTableMap extends
			TableMapper<ImmutableBytesWritable, ImmutableBytesWritable> {
		@Override
		public void map(ImmutableBytesWritable row, Result result,
				Context context) throws IOException, InterruptedException {
			for (Cell cell : result.rawCells()) {
				context.write(new ImmutableBytesWritable(CellUtil.cloneRow(cell)), 
						new ImmutableBytesWritable(CellUtil.cloneValue(cell)));
			}
		}
	}
	static class TestTableReducer extends
			TableReducer<ImmutableBytesWritable, ImmutableBytesWritable, ImmutableBytesWritable> {
		protected void reduce(ImmutableBytesWritable key, Iterable<ImmutableBytesWritable> values, 
				Context context) throws IOException, InterruptedException {
			for(ImmutableBytesWritable value: values) {
				Put p = new Put(key.get());
				p.add(Bytes.toBytes("testfml"), Bytes.toBytes("testcl"), value.get());
				context.write(key, p);
			}
		}
	}
	private static Job createSubmittableJob(Configuration conf) throws IOException {
	    conf.set(TableInputFormat.INPUT_TABLE, "test_table");
	    conf.set(TableInputFormat.SCAN_COLUMNS, "testfml");
		@SuppressWarnings("deprecation")
		Job job = new Job(conf, "Scan_test_table");
		job.setJarByClass(TestMrApi.class);
		job.setInputFormatClass(TableInputFormat.class);
		job.setMapperClass(TestTableMap.class);
		job.setMapOutputKeyClass(ImmutableBytesWritable.class);
		job.setMapOutputValueClass(ImmutableBytesWritable.class);
		TableMapReduceUtil.initTableReducerJob("test_table4", TestTableReducer.class, job);
		return job;
	}
}


TestObserve

package com.chinahadoop.testbasicapi;

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

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Durability;
import org.apache.hadoop.hbase.client.HTableInterface;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.coprocessor.BaseRegionObserver;
import org.apache.hadoop.hbase.coprocessor.ObserverContext;
import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment;
import org.apache.hadoop.hbase.regionserver.wal.WALEdit;

public class TestObserve extends BaseRegionObserver {
	public static final Log LOG = LogFactory.getLog(TestObserve.class);
	private HTableInterface table2 = null;
	@Override
	public void prePut(final ObserverContext<RegionCoprocessorEnvironment> e,
			final Put put, final WALEdit edit, final Durability durability)
			throws IOException {
		if (table2 == null) {
			synchronized (this) {
				if (table2 == null) {
					table2 = e.getEnvironment().getTable(TableName.valueOf("test_table2"));
				}
			}
		} 
		Put newPut = new Put(put.getRow());
		for(Map.Entry<byte [], List<Cell>> entry: put.getFamilyCellMap().entrySet()) {
			newPut.getFamilyCellMap().put(entry.getKey(), entry.getValue());
	    }
		put.setDurability(durability);
		table2.put(put);
	}
	@Override
	protected void finalize() throws Throwable {
		if (table2 != null) {
			try { 
				table2.close();
			} catch (Exception e) {
				LOG.error("Error when finalize", e);
			}
		}
		super.finalize();
	}
	
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值