java后觉,hbase java操作代码简介和NoClassDefFoundError: org/apache/hadoop/hbase/HBaseConfiguratio – 后知后觉的it路 –...

将上面代码打包后 放在hadoop上通过执行:bin/hadoop jar XX.jar 包.类执行的时候,报错如下:Exception in thread “main” java.lang.NoClassDefFoundError: org/apache/hadoop/hbase/HBaseConfiguration

错误原因:  hadoop2 节点无法加载到hbase包

处理方式:将hbase jar拷贝到hadoop 节点上,比如 /usr/local/hbaselib

然后在 hadooop/etc/hadoop/hadoop-env.sh中 增加如下:

for f in /usr/local/hbaselib/*.jar; do

if [ "$HADOOP_CLASSPATH" ]; then

export HADOOP_CLASSPATH=$HADOOP_CLASSPATH:$f

else

export HADOOP_CLASSPATH=$f

fi

done

最后 可以在 hadoop/bin/mapred classpath下 查看是否已经将hbase lib 加载进来。

Java操作hbase代码写法如下:

Java代码  icon_star.pngspinner.gif

import java.io.IOException;

import java.util.ArrayList;

import java.util.List;

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.MasterNotRunningException;

import org.apache.hadoop.hbase.ZooKeeperConnectionException;

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 HbaseTest {

public static String TableName = “stu”;

public static String RowKey = “m1”;

public static String FamilyColumn1 = “base”;

public static String FamilyColumn2 = “more”;

/**

* 1 创建表, 删除表  此时只涉及表名 列族名称

* 2 表中新增记录,追加记录,删除记录   只有在表记录新增,查询时 才涉及rowkey

* 3 表中查询记录

* HBaseAdmin

* HTable

*/

public static void main(String[] args) throws Exception {

// 0 创建配置信息

Configuration conf = getConf();

// 1 ddl语句

ddl(conf,TableName,FamilyColumn1,FamilyColumn2);

// 2 dml语句

//dml(conf);

}

private static void dml(Configuration conf) throws IOException {

// 1 创建dml操作类 HTable

HTable hTable = new HTable(conf, TableName);

// 2 指定新增记录操作类 Put 并初始化

List puts = new ArrayList();

Put put1 = new Put(Bytes.toBytes(RowKey)); // 指定行健是在Put上指定的

put1.add(Bytes.toBytes(FamilyColumn1), Bytes.toBytes(“name”), Bytes.toBytes(“张三”));

Put put2 = new Put(Bytes.toBytes(RowKey)); // 指定行健

put2.add(Bytes.toBytes(FamilyColumn2), Bytes.toBytes(“age”), Bytes.toBytes(29));

puts.add(put1);

puts.add(put2);

// 3 新增到hbase表中

hTable.put(puts);

// 4 查看记录:

Get get = new Get(Bytes.toBytes(RowKey)); // 查询时 指定要查询的是那条记录—》根据rowkey确定

Result result = hTable.get(get);

System.out.println(result); // keyvalues={m1/f1:name/1419389290472/Put/vlen=6/ts=0, m1/f2:age/1419389290474/Put/vlen=4/ts=0}

String nameVal = Bytes.toString(result.getValue(Bytes.toBytes(FamilyColumn1), Bytes.toBytes(“name”))); // 根据列族名和列名来查询对应value数值

System.out.println(nameVal); // 打印出 : 张三

int  ageVal = Bytes.toInt(result.getValue(Bytes.toBytes(FamilyColumn2), Bytes.toBytes(“age”))); // 根据列族名和列名来查询对应value数值

System.out.println(ageVal); // 打印出 :29

}

// 更细节的查询方式  scan

private static void scan() throws IOException {

final Configuration conf = getConf();

final HTable hTable = new HTable(conf, TableName);

//使用scan对象可以设定startRow、stopRow

Scan scan = new Scan();

//scan.addColumn(family, qualifier); 扫描时  详细扫描范围

//scan.addFamily(family)

//scan.setStartRow(startRow);

//scan.setStopRow(stopRow);

//scan.setFilter(filter);

final ResultScanner scanner = hTable.getScanner(scan);

//指定列簇、列

//final ResultScanner scanner = hTable.getScanner(Bytes.toBytes(“f1”), Bytes.toBytes(“c1”));

for (Result result : scanner) {

System.out.println(result);

}

hTable.close();

}

private static void ddl(Configuration conf, String tableName, String…familyNames)

throws MasterNotRunningException, ZooKeeperConnectionException,IOException {

// 1 创建表

HBaseAdmin hBaseAdmin = new HBaseAdmin(conf);

HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);

// 1.1 给表增加列族

for(String familyName : familyNames) {

HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(familyName);

hTableDescriptor.addFamily(hColumnDescriptor);

}

// 1.2 开始创建表

if(!hBaseAdmin.tableExists(TableName)){

hBaseAdmin.createTable(hTableDescriptor);

}

System.out.println(“创建后,表”+TableName+“是否存在: “ + hBaseAdmin.tableExists(TableName));

// 2 更改表状态

//hBaseAdmin.disableTable(TableName); // 停用此表 会输出: Started disable of myhbasetable   Disabled myhbasetable

//hBaseAdmin.enableTable(TableName); // 启动此表

// 3 删除表

//hBaseAdmin.deleteTable(TableName); // 删除表

//System.out.println(“删除后,表”+TableName+”是否存在: ” + hBaseAdmin.tableExists(TableName));

}

/**

* master在本地host配置为 : master 192.168.1.110

* 搭建hbase 集群为:

* h2master 192.168.1.110

* h2sliver113 192.168.1.113

* h2sliver114 192.168.1.114

* hadoop单节点:

* h2single 192.168.1.221

* @return

*/

private static Configuration getConf() {

Configuration conf = HBaseConfiguration.create();

conf.set(“hbase.rootdir”, “hdfs://h2single:9000/hbase”); // 指定hbase在hdfs的路径

conf.set(“hbase.zookeeper.quorum”, “h2sliver113:2181”);   // 指定zk集群 这里只写一个zk节点就可以了

return conf;

}

}

import java.io.IOException;

import java.util.ArrayList;

import java.util.List;

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.MasterNotRunningException;

import org.apache.hadoop.hbase.ZooKeeperConnectionException;

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 HbaseTest {

public static String TableName = “stu”;

public static String RowKey = “m1”;

public static String FamilyColumn1 = “base”;

public static String FamilyColumn2 = “more”;

/**

* 1 创建表, 删除表 此时只涉及表名 列族名称

* 2 表中新增记录,追加记录,删除记录 只有在表记录新增,查询时 才涉及rowkey

* 3 表中查询记录

* HBaseAdmin

* HTable

*/

public static void main(String[] args) throws Exception {

// 0 创建配置信息

Configuration conf = getConf();

// 1 ddl语句

ddl(conf,TableName,FamilyColumn1,FamilyColumn2);

// 2 dml语句

//dml(conf);

}

private static void dml(Configuration conf) throws IOException {

// 1 创建dml操作类 HTable

HTable hTable = new HTable(conf, TableName);

// 2 指定新增记录操作类 Put 并初始化

List puts = new ArrayList();

Put put1 = new Put(Bytes.toBytes(RowKey)); // 指定行健是在Put上指定的

put1.add(Bytes.toBytes(FamilyColumn1), Bytes.toBytes(“name”), Bytes.toBytes(“张三”));

Put put2 = new Put(Bytes.toBytes(RowKey)); // 指定行健

put2.add(Bytes.toBytes(FamilyColumn2), Bytes.toBytes(“age”), Bytes.toBytes(29));

puts.add(put1);

puts.add(put2);

// 3 新增到hbase表中

hTable.put(puts);

// 4 查看记录:

Get get = new Get(Bytes.toBytes(RowKey)); // 查询时 指定要查询的是那条记录—》根据rowkey确定

Result result = hTable.get(get);

System.out.println(result); // keyvalues={m1/f1:name/1419389290472/Put/vlen=6/ts=0, m1/f2:age/1419389290474/Put/vlen=4/ts=0}

String nameVal = Bytes.toString(result.getValue(Bytes.toBytes(FamilyColumn1), Bytes.toBytes(“name”))); // 根据列族名和列名来查询对应value数值

System.out.println(nameVal); // 打印出 : 张三

int ageVal = Bytes.toInt(result.getValue(Bytes.toBytes(FamilyColumn2), Bytes.toBytes(“age”))); // 根据列族名和列名来查询对应value数值

System.out.println(ageVal); // 打印出 :29

}

// 更细节的查询方式 scan

private static void scan() throws IOException {

final Configuration conf = getConf();

final HTable hTable = new HTable(conf, TableName);

//使用scan对象可以设定startRow、stopRow

Scan scan = new Scan();

//scan.addColumn(family, qualifier); 扫描时 详细扫描范围

//scan.addFamily(family)

//scan.setStartRow(startRow);

//scan.setStopRow(stopRow);

//scan.setFilter(filter);

final ResultScanner scanner = hTable.getScanner(scan);

//指定列簇、列

//final ResultScanner scanner = hTable.getScanner(Bytes.toBytes(“f1”), Bytes.toBytes(“c1”));

for (Result result : scanner) {

System.out.println(result);

}

hTable.close();

}

private static void ddl(Configuration conf, String tableName, String…familyNames)

throws MasterNotRunningException, ZooKeeperConnectionException,IOException {

// 1 创建表

HBaseAdmin hBaseAdmin = new HBaseAdmin(conf);

HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);

// 1.1 给表增加列族

for(String familyName : familyNames) {

HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(familyName);

hTableDescriptor.addFamily(hColumnDescriptor);

}

// 1.2 开始创建表

if(!hBaseAdmin.tableExists(TableName)){

hBaseAdmin.createTable(hTableDescriptor);

}

System.out.println(“创建后,表”+TableName+”是否存在: ” + hBaseAdmin.tableExists(TableName));

// 2 更改表状态

//hBaseAdmin.disableTable(TableName); // 停用此表 会输出: Started disable of myhbasetable Disabled myhbasetable

//hBaseAdmin.enableTable(TableName); // 启动此表

// 3 删除表

//hBaseAdmin.deleteTable(TableName); // 删除表

//System.out.println(“删除后,表”+TableName+”是否存在: ” + hBaseAdmin.tableExists(TableName));

}

/**

* master在本地host配置为 : master 192.168.1.110

* 搭建hbase 集群为:

* h2master 192.168.1.110

* h2sliver113 192.168.1.113

* h2sliver114 192.168.1.114

* hadoop单节点:

* h2single 192.168.1.221

* @return

*/

private static Configuration getConf() {

Configuration conf = HBaseConfiguration.create();

conf.set(“hbase.rootdir”, “hdfs://h2single:9000/hbase”); // 指定hbase在hdfs的路径

conf.set(“hbase.zookeeper.quorum”, “h2sliver113:2181”); // 指定zk集群 这里只写一个zk节点就可以了

return conf;

}

}

2  java操作hbase精简版代码:

Java代码  icon_star.pngspinner.gif

import java.util.ArrayList;

import java.util.List;

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.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;

public class HBaseTest1 {

private static final String TABLE_NAME = “stu”;

private static final String FAMILY_NAME = “f1”;

private static final String COLUMN_NAME = “name”;

private static final String COLUMN_AGE = “age”;

private static final String ROW_KEY1 = “r1”;

private static final String ROW_KEY2 = “r2”;

public static void main(String[] args) throws Exception {

//构造能够访问HBase的configuration对象

Configuration conf = HBaseConfiguration.create();

conf.set(“hbase.rootdir”, “hdfs://h2single:9000/hbase”);

conf.set(“hbase.zookeeper.quorum”, “h2sliver113:2181”);

//HBaseAdmin是对HBase进行ddl操作的核心类

HBaseAdmin hBaseAdmin = new HBaseAdmin(conf);

if(!hBaseAdmin.tableExists(TABLE_NAME)){

HTableDescriptor htableDescriptor = new HTableDescriptor(TABLE_NAME);

htableDescriptor.addFamily(new HColumnDescriptor(FAMILY_NAME));

hBaseAdmin.createTable(htableDescriptor);

System.out.println(“table create success”);

}else{

System.out.println(“table exists”);

}

//使用HTable可以对HBase的表中的数据进行增删改查

HTable hTable = new HTable(conf, TABLE_NAME);

// 增加数据

List putList = new ArrayList();

Put put1 = new Put(ROW_KEY1.getBytes());

put1.add(FAMILY_NAME.getBytes(), COLUMN_NAME.getBytes(), “zhangsan”.getBytes());

put1.add(FAMILY_NAME.getBytes(), COLUMN_AGE.getBytes(), “23”.getBytes());

putList.add(put1);

Put put2 = new Put(ROW_KEY2.getBytes());

put2.add(FAMILY_NAME.getBytes(), COLUMN_NAME.getBytes(), “lisi”.getBytes());

put2.add(FAMILY_NAME.getBytes(), COLUMN_AGE.getBytes(), “24”.getBytes());

putList.add(put2);

hTable.put(putList);

// 根据rowkey得到记录后 获取此记录对应的列信息

Get get = new Get(ROW_KEY1.getBytes());

Result get1 = hTable.get(get);

String name1 = new String(get1.getValue(FAMILY_NAME.getBytes(), COLUMN_NAME.getBytes()));

String age1 = new String(get1.getValue(FAMILY_NAME.getBytes(), COLUMN_AGE.getBytes()));

//System.out.println(get1+”\t”+name1+”\t”+age1);

// 指定行范围来查询多条记录

Scan scan = new Scan();

scan.setStartRow(ROW_KEY1.getBytes());

scan.setStopRow(ROW_KEY2.getBytes());

ResultScanner scanner = hTable.getScanner(scan);

for (Result result : scanner) {

String rowKey = new String(result.getRow());

String name = new String(result.getValue(FAMILY_NAME.getBytes(), COLUMN_NAME.getBytes()));

String age = new String(result.getValue(FAMILY_NAME.getBytes(), COLUMN_AGE.getBytes()));

System.out.println(rowKey+“\t”+name+“\t”+age1);

}

// 根据rowkey删除记录

Delete delete = new Delete(ROW_KEY1.getBytes());

hTable.delete(delete);

// 删除表

hBaseAdmin.disableTable(TABLE_NAME);

hBaseAdmin.deleteTable(TABLE_NAME);

}

}

import java.util.ArrayList;

import java.util.List;

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.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;

public class HBaseTest1 {

private static final String TABLE_NAME = “stu”;

private static final String FAMILY_NAME = “f1”;

private static final String COLUMN_NAME = “name”;

private static final String COLUMN_AGE = “age”;

private static final String ROW_KEY1 = “r1”;

private static final String ROW_KEY2 = “r2”;

public static void main(String[] args) throws Exception {

//构造能够访问HBase的configuration对象

Configuration conf = HBaseConfiguration.create();

conf.set(“hbase.rootdir”, “hdfs://h2single:9000/hbase”);

conf.set(“hbase.zookeeper.quorum”, “h2sliver113:2181”);

//HBaseAdmin是对HBase进行ddl操作的核心类

HBaseAdmin hBaseAdmin = new HBaseAdmin(conf);

if(!hBaseAdmin.tableExists(TABLE_NAME)){

HTableDescriptor htableDescriptor = new HTableDescriptor(TABLE_NAME);

htableDescriptor.addFamily(new HColumnDescriptor(FAMILY_NAME));

hBaseAdmin.createTable(htableDescriptor);

System.out.println(“table create success”);

}else{

System.out.println(“table exists”);

}

//使用HTable可以对HBase的表中的数据进行增删改查

HTable hTable = new HTable(conf, TABLE_NAME);

// 增加数据

List putList = new ArrayList();

Put put1 = new Put(ROW_KEY1.getBytes());

put1.add(FAMILY_NAME.getBytes(), COLUMN_NAME.getBytes(), “zhangsan”.getBytes());

put1.add(FAMILY_NAME.getBytes(), COLUMN_AGE.getBytes(), “23”.getBytes());

putList.add(put1);

Put put2 = new Put(ROW_KEY2.getBytes());

put2.add(FAMILY_NAME.getBytes(), COLUMN_NAME.getBytes(), “lisi”.getBytes());

put2.add(FAMILY_NAME.getBytes(), COLUMN_AGE.getBytes(), “24”.getBytes());

putList.add(put2);

hTable.put(putList);

// 根据rowkey得到记录后 获取此记录对应的列信息

Get get = new Get(ROW_KEY1.getBytes());

Result get1 = hTable.get(get);

String name1 = new String(get1.getValue(FAMILY_NAME.getBytes(), COLUMN_NAME.getBytes()));

String age1 = new String(get1.getValue(FAMILY_NAME.getBytes(), COLUMN_AGE.getBytes()));

//System.out.println(get1+”\t”+name1+”\t”+age1);

// 指定行范围来查询多条记录

Scan scan = new Scan();

scan.setStartRow(ROW_KEY1.getBytes());

scan.setStopRow(ROW_KEY2.getBytes());

ResultScanner scanner = hTable.getScanner(scan);

for (Result result : scanner) {

String rowKey = new String(result.getRow());

String name = new String(result.getValue(FAMILY_NAME.getBytes(), COLUMN_NAME.getBytes()));

String age = new String(result.getValue(FAMILY_NAME.getBytes(), COLUMN_AGE.getBytes()));

System.out.println(rowKey+”\t”+name+”\t”+age1);

}

// 根据rowkey删除记录

Delete delete = new Delete(ROW_KEY1.getBytes());

hTable.delete(delete);

// 删除表

hBaseAdmin.disableTable(TABLE_NAME);

hBaseAdmin.deleteTable(TABLE_NAME);

}

}

3  在 hbase + hadoop2 + zk 构建的集群的时候注意事项:

hbase: h2master主  h2sliver113 从  h2sliver114从

hadoop: h2single

zookeeper:  h2master h2sliver113  h2sliver114

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值