java调用HBase api创建表,插入数据_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > java调用HBase api创建表,插入数据

java调用HBase api创建表,插入数据

 2014/8/25 0:12:49  beat_it_  程序员俱乐部  我要评论(0)
  • 摘要:今天看说学习了下通过java调用HBaseapi实习表的创建和数据插入的操作,贴下代码:packagecom.lyq.study.api;importjava.io.IOException;importjava.util.ArrayList;importjava.util.List;importorg.apache.hadoop.conf.Configuration;importorg.apache.hadoop.hbase.HColumnDescriptor;importorg.apache
  • 标签:API 创建 Java 数据 ASE

今天看说学习了下通过java 调用HBase api 实习表的创建和数据插入的操作,贴下代码:

class="java">package com.lyq.study.api;

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

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;

import com.lyq.study.util.HBaseConfigUtils;

public class HBase_API {

	public static void main(String[] args) throws MasterNotRunningException, ZooKeeperConnectionException, IOException {
		String tableName = "hbasetest";
		String familyName = "info";
		Configuration conf = HBaseConfigUtils.getHBaseConfig(1);
		//创建表		
		HBaseAdmin admin = new HBaseAdmin(conf);
		HTableDescriptor tableDesc = new HTableDescriptor(tableName);
		tableDesc.addFamily(new HColumnDescriptor(familyName));
		admin.createTable(tableDesc);
		
		//插入表数据
		HTable table = new HTable(conf, tableName);
		//单条插入
		//row1为rowkey
		Put putRow1 = new Put("row1".getBytes());
		putRow1.add(familyName.getBytes(), "name".getBytes(), "zhangsan".getBytes());
		putRow1.add(familyName.getBytes(), "age".getBytes(), "24".getBytes());
		putRow1.add(familyName.getBytes(), "city".getBytes(), "chengde".getBytes());
		putRow1.add(familyName.getBytes(), "sex".getBytes(), "male".getBytes());
		table.put(putRow1);
		
		//多条插入
		List<Put> list = new ArrayList<Put>();
		Put p = null;
		p = new Put("rowkey1".getBytes());
		p.add(familyName.getBytes(), "name".getBytes(), "wangwu".getBytes());
		p.add(familyName.getBytes(), "sex".getBytes(), "male".getBytes());
		p.add(familyName.getBytes(), "city".getBytes(), "beijing".getBytes());
		p.add(familyName.getBytes(), "age".getBytes(), "25".getBytes());
		list.add(p);
		p = new Put("rowkey2".getBytes());
		p.add(familyName.getBytes(), "name".getBytes(), "zhangliu".getBytes());
		p.add(familyName.getBytes(), "sex".getBytes(), "male".getBytes());
		p.add(familyName.getBytes(), "city".getBytes(), "handan".getBytes());
		p.add(familyName.getBytes(), "age".getBytes(), "28".getBytes());
		list.add(p);
		p = new Put("rowkey3".getBytes());
		p.add(familyName.getBytes(), "name".getBytes(), "liqing".getBytes());
		p.add(familyName.getBytes(), "sex".getBytes(), "female".getBytes());
		p.add(familyName.getBytes(), "city".getBytes(), "guangzhou".getBytes());
		p.add(familyName.getBytes(), "age".getBytes(), "18".getBytes());
		list.add(p);
		table.put(list); 
	}
}

?

创建表之接调用了HBaseAdmin对象的create(HTableDescriptor desc)方法,

插入数据分别掉用了HTable对象的put(Put put)方法,和put(List<Put> list)方法:

其中put(Put put)方法实习了数据的单条插入,

put(List<Put> list)方法实现了数据的多条批量插入。

Configuration conf = HBaseConfigUtils.getHBaseConfig(1);

这句代码中的HBaseConfigUtils.getHBaseConfig(int flag)方法代码如下:

package com.lyq.study.util;

import org.apache.hadoop.conf.Configuration;

 
public class HBaseConfigUtils {
	/**
	 * 获取HBaseConfiguration
	 * @param flag	
	 * 			集群标识:0,单机;1集群
	 * @return
	 */
	public static Configuration getHBaseConfig(int flag){
		Configuration conf = new Configuration();
		if(flag > 0){
			//集群
			conf.set("fs.defaultFS", "hdfs://master129:9000/");
			conf.set("mapreduce.framework.name", "local");
			conf.set("mapred.job.tracker", "master129:9001");
			conf.set("hbase.zookeeper.quorum", "master129,slave130,slave131,slave132");
		}else{
			//单机
			conf.set("fs.defaultFS", "hdfs://ubuntu:9000/");
			conf.set("mapreduce.framework.name", "local");
			conf.set("mapred.job.tracker", "ubuntu:9001");
			conf.set("hbase.zookeeper.quorum", "ubuntu");
		}
		
		return conf;
	}
	
}

?

?

  • 相关文章
发表评论
用户名: 匿名