微信搜索lxw1234bigdata | 邀请体验:数阅–数据管理、OLAP分析与可视化平台 | 赞助作者:赞助作者

Java对hbase的基本操作

HBase lxw1234@qq.com 10799℃ 1评论
		package com.sdo.sjzx.jptj;

		import java.io.IOException;
		import java.util.ArrayList;
		import java.util.HashMap;
		import java.util.Iterator;
		import java.util.List;
		import java.util.Map;
		import java.util.Set;
		
		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 Test {
			private static Configuration conf = null;
			static {
				conf = HBaseConfiguration.create();
				conf.set(
						"hbase.zookeeper.quorum",
						"zk1,zk2,zk3");
			}
		
			//参数:表名,列名(可以有多个)
			public void createTable(String tablename, String[] cfs) throws IOException {
				HBaseAdmin admin = new HBaseAdmin(conf);
				if (admin.tableExists(tablename)) {
					System.out.println("table already exists!");
				} else {
					HTableDescriptor tableDesc = new HTableDescriptor(tablename);
					for (int i = 0; i < cfs.length; i++) {
						tableDesc.addFamily(new HColumnDescriptor(cfs[i]));
					}
					admin.createTable(tableDesc);
					System.out.println("table create OK!");
				}
			}
			
			//删除表,先disable,再delete
			public void deleteTable(String tablename) throws IOException {
				try {
					HBaseAdmin admin = new HBaseAdmin(conf);
					admin.disableTable(tablename);
					admin.deleteTable(tablename);
					System.out.println("delete ok");
				} catch (MasterNotRunningException e) {
					e.printStackTrace();
				} catch (ZooKeeperConnectionException e) {
					e.printStackTrace();
				}
			}
			
			//insert记录,
			public void writeRow(String tablename,String rowKey,Map> m) {
				try {
					HTable table = new HTable(conf, tablename);
					Put put = new Put(Bytes.toBytes(rowKey));//"001"
					Set key = m.keySet();
					for(Iterator it = key.iterator();it.hasNext();){
						String k = it.next();
						Map tmpM = m.get(k);
						Set keyInner = tmpM.keySet();
						for(Iterator itInner = keyInner.iterator();itInner.hasNext();) {
							String kInner = itInner.next();
							String vInner = tmpM.get(kInner);
							put.add(Bytes.toBytes(k), Bytes.toBytes(kInner), Bytes.toBytes(vInner));
							table.put(put);
						}
						
					}
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		
			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("");
			}
		
			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()));
				}
			}
		
			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();
				}
			}
		
			public static void main(String[] agrs) {
				try {
					String tablename = "lxwtest";
					Test t = new Test();
					t.deleteTable(tablename);
					
					//创建一张列为userinfo和userdeptinfo的表
					String[] s = new String[2];
					s[0] = "userinfo";
					s[1] = "userdeptinfo";
					t.createTable(tablename,s);
					
					
					String userId = "003";
					String userName = "wangwu";
					int age = 25;
					String dept = "data analysis";
					
					//向userinfo中写入name=?和age=?
					//向userdeptinfo中写入dept=?
					//这里将源数据构建合适的数据结构,比如Map>
					//来完成insert
					
					Map> m = new HashMap>();
					Map kvUserInfo = new HashMap();
					kvUserInfo.put("name", userName);
					kvUserInfo.put("age",age+"");
					m.put("userinfo", kvUserInfo);
					
					Map kvUserDeptInfo = new HashMap();
					kvUserDeptInfo.put("deptname", dept);
					m.put("userdeptinfo", kvUserDeptInfo);
					
					t.writeRow(tablename,userId,m);
					t.scaner(tablename);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		
		}

scan出来的结果:

		hbase(main):028:0> scan 'lxwtest'
		ROW                                  COLUMN+CELL                                                                                             
		 001                                 column=userdeptinfo:deptname, timestamp=1340092898883, value=data platform                              
		 001                                 column=userinfo:age, timestamp=1340092898883, value=27                                                  
		 001                                 column=userinfo:name, timestamp=1340092898883, value=zhangsan                                           
		 002                                 column=userdeptinfo:deptname, timestamp=1340092951198, value=data platform                              
		 002                                 column=userinfo:age, timestamp=1340092951198, value=25                                                  
		 002                                 column=userinfo:name, timestamp=1340092951198, value=lisi                                               
		 003                                 column=userdeptinfo:deptname, timestamp=1340093006923, value=data analysis                              
		 003                                 column=userinfo:age, timestamp=1340093006923, value=25                                                  
		 003                                 column=userinfo:name, timestamp=1340093006923, value=wangwu  

如果觉得本博客对您有帮助,请 赞助作者

转载请注明:lxw的大数据田地 » Java对hbase的基本操作

喜欢 (5)
分享 (0)
发表我的评论
取消评论
表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
(1)个小伙伴在吐槽
  1. public void writeRow(String tablename,String rowKey,Map> m) { 这一行代码是错的
    夏慕振2016-06-17 16:29 回复