HBase中Java操作数据库增删改查——增加数据

已经创建了一个学生宿舍管理系统

数据表表名:student_info

列族1:students

列族2:dormitorys

列族3:staff_members

添加方法:

package myhbase;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes;
public class PutData {
	public static void main(String[] args) throws MasterNotRunningException,
			ZooKeeperConnectionException, IOException {
		String tableName = "student_info";
		String columnFamily1 = "students";
		String columnFamily2 = "dormitorys";
		String columnFamily3 = "staff_members";
		put(tableName, "001", columnFamily1, "student_id", "164804098");
		put(tableName, "001", columnFamily1, "student_name", "tingtingrugai");
		put(tableName, "001", columnFamily1, "gender", "male");
		put(tableName, "001", columnFamily1, "tel", "123456789");
		put(tableName, "001", columnFamily2, "hostel_No.", "115");
		put(tableName, "001", columnFamily2, "bed", "3");
		put(tableName, "001", columnFamily3, "staff_nameA", "zhangsan");
		put(tableName, "001", columnFamily3, "staff_ageA", "40");
		put(tableName, "001", columnFamily3, "staff_genderA", "male");

	}

	public static Configuration getConfiguration() {
		Configuration conf = HBaseConfiguration.create();
		conf.set("hbase.rootdir", "hdfs://localhost:9000/hbase");
		conf.set("hbase.zookeeper.quorum", "localhost");
		return conf;
	}
	public static void put(String tableName, String row, String columnFamily,
			String column, String data) throws IOException {
		HTable table = new HTable(getConfiguration(), tableName);
		Put put = new Put(Bytes.toBytes(row));
		put.add(Bytes.toBytes(columnFamily),
				Bytes.toBytes(column),
				Bytes.toBytes(data));
		table.put(put);
		System.err.println("SUCCESS");
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值