hbase java操作代码简介和NoClassDefFoundError: org/apache/hadoop/hbase/HBaseConfiguratio – 后知后觉的it路 – ITeye技术网站

将上面代码打包后 放在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代码  收藏代码
  1. import java.io.IOException;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import org.apache.hadoop.conf.Configuration;
  5. import org.apache.hadoop.hbase.HBaseConfiguration;
  6. import org.apache.hadoop.hbase.HColumnDescriptor;
  7. import org.apache.hadoop.hbase.HTableDescriptor;
  8. import org.apache.hadoop.hbase.MasterNotRunningException;
  9. import org.apache.hadoop.hbase.ZooKeeperConnectionException;
  10. import org.apache.hadoop.hbase.client.Get;
  11. import org.apache.hadoop.hbase.client.HBaseAdmin;
  12. import org.apache.hadoop.hbase.client.HTable;
  13. import org.apache.hadoop.hbase.client.Put;
  14. import org.apache.hadoop.hbase.client.Result;
  15. import org.apache.hadoop.hbase.client.ResultScanner;
  16. import org.apache.hadoop.hbase.client.Scan;
  17. import org.apache.hadoop.hbase.util.Bytes;
  18. public class HbaseTest {
  19.     public static String TableName = “stu”;
  20.     public static String RowKey = “m1”;
  21.     public static String FamilyColumn1 = “base”;
  22.     public static String FamilyColumn2 = “more”;
  23.     /**
  24.      * 1 创建表, 删除表  此时只涉及表名 列族名称
  25.      * 2 表中新增记录,追加记录,删除记录   只有在表记录新增,查询时 才涉及rowkey
  26.      * 3 表中查询记录
  27.      * HBaseAdmin
  28.      * HTable
  29.      */
  30.     public static void main(String[] args) throws Exception {
  31.         // 0 创建配置信息
  32.         Configuration conf = getConf();
  33.         // 1 ddl语句
  34.         ddl(conf,TableName,FamilyColumn1,FamilyColumn2);
  35.         // 2 dml语句
  36.         //dml(conf);
  37.     }
  38.     private static void dml(Configuration conf) throws IOException {
  39.         // 1 创建dml操作类 HTable
  40.         HTable hTable = new HTable(conf, TableName);
  41.         // 2 指定新增记录操作类 Put 并初始化
  42.         List<Put> puts = new ArrayList<Put>();
  43.         Put put1 = new Put(Bytes.toBytes(RowKey)); // 指定行健是在Put上指定的
  44.         put1.add(Bytes.toBytes(FamilyColumn1), Bytes.toBytes(“name”), Bytes.toBytes(“张三”));
  45.         Put put2 = new Put(Bytes.toBytes(RowKey)); // 指定行健
  46.         put2.add(Bytes.toBytes(FamilyColumn2), Bytes.toBytes(“age”), Bytes.toBytes(29));
  47.         puts.add(put1);
  48.         puts.add(put2);
  49.         // 3 新增到hbase表中
  50.         hTable.put(puts);
  51.         // 4 查看记录:
  52.         Get get = new Get(Bytes.toBytes(RowKey)); // 查询时 指定要查询的是那条记录—》根据rowkey确定
  53.         Result result = hTable.get(get);
  54.         System.out.println(result); // keyvalues={m1/f1:name/1419389290472/Put/vlen=6/ts=0, m1/f2:age/1419389290474/Put/vlen=4/ts=0}
  55.         String nameVal = Bytes.toString(result.getValue(Bytes.toBytes(FamilyColumn1), Bytes.toBytes(“name”))); // 根据列族名和列名来查询对应value数值
  56.         System.out.println(nameVal); // 打印出 : 张三
  57.         int  ageVal = Bytes.toInt(result.getValue(Bytes.toBytes(FamilyColumn2), Bytes.toBytes(“age”))); // 根据列族名和列名来查询对应value数值
  58.         System.out.println(ageVal); // 打印出 :29
  59.     }
  60.     // 更细节的查询方式  scan 
  61.     private static void scan() throws IOException {
  62.         final Configuration conf = getConf();
  63.         final HTable hTable = new HTable(conf, TableName);
  64.         //使用scan对象可以设定startRow、stopRow
  65.         Scan scan = new Scan();
  66.         //scan.addColumn(family, qualifier); 扫描时  详细扫描范围
  67.         //scan.addFamily(family) 
  68.         //scan.setStartRow(startRow);
  69.         //scan.setStopRow(stopRow);
  70.         //scan.setFilter(filter);
  71.         final ResultScanner scanner = hTable.getScanner(scan);
  72.         //指定列簇、列
  73.         //final ResultScanner scanner = hTable.getScanner(Bytes.toBytes(“f1”), Bytes.toBytes(“c1”));
  74.         for (Result result : scanner) {
  75.             System.out.println(result);
  76.         }
  77.         hTable.close();
  78.     }
  79.     private static void ddl(Configuration conf, String tableName, String…familyNames)
  80.             throws MasterNotRunningException, ZooKeeperConnectionException,IOException {
  81.         // 1 创建表
  82.         HBaseAdmin hBaseAdmin = new HBaseAdmin(conf);
  83.         HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
  84.         // 1.1 给表增加列族
  85.         for(String familyName : familyNames) {
  86.             HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(familyName);
  87.             hTableDescriptor.addFamily(hColumnDescriptor);
  88.         }
  89.         // 1.2 开始创建表
  90.         if(!hBaseAdmin.tableExists(TableName)){
  91.             hBaseAdmin.createTable(hTableDescriptor);
  92.         }
  93.         System.out.println(“创建后,表”+TableName+“是否存在: “ + hBaseAdmin.tableExists(TableName));
  94.         // 2 更改表状态
  95.         //hBaseAdmin.disableTable(TableName); // 停用此表 会输出: Started disable of myhbasetable   Disabled myhbasetable
  96.         //hBaseAdmin.enableTable(TableName); // 启动此表
  97.         // 3 删除表
  98.         //hBaseAdmin.deleteTable(TableName); // 删除表
  99.         //System.out.println(“删除后,表”+TableName+”是否存在: ” + hBaseAdmin.tableExists(TableName));
  100.     }
  101.     /**
  102.      * master在本地host配置为 : master 192.168.1.110
  103.      * 搭建hbase 集群为:
  104.      * h2master 192.168.1.110
  105.      * h2sliver113 192.168.1.113
  106.      * h2sliver114 192.168.1.114
  107.      * hadoop单节点:
  108.      * h2single 192.168.1.221
  109.      * @return
  110.      */
  111.     private static Configuration getConf() {
  112.         Configuration conf = HBaseConfiguration.create();
  113.         conf.set(“hbase.rootdir”“hdfs://h2single:9000/hbase”); // 指定hbase在hdfs的路径
  114.         conf.set(“hbase.zookeeper.quorum”“h2sliver113:2181”);   // 指定zk集群 这里只写一个zk节点就可以了
  115.         return conf;
  116.     }
  117. }
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<Put> puts = new ArrayList<Put>();

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代码  收藏代码
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import org.apache.hadoop.conf.Configuration;
  4. import org.apache.hadoop.hbase.HBaseConfiguration;
  5. import org.apache.hadoop.hbase.HColumnDescriptor;
  6. import org.apache.hadoop.hbase.HTableDescriptor;
  7. import org.apache.hadoop.hbase.client.Delete;
  8. import org.apache.hadoop.hbase.client.Get;
  9. import org.apache.hadoop.hbase.client.HBaseAdmin;
  10. import org.apache.hadoop.hbase.client.HTable;
  11. import org.apache.hadoop.hbase.client.Put;
  12. import org.apache.hadoop.hbase.client.Result;
  13. import org.apache.hadoop.hbase.client.ResultScanner;
  14. import org.apache.hadoop.hbase.client.Scan;
  15. public class HBaseTest1 {
  16.     private static final String TABLE_NAME = “stu”;
  17.     private static final String FAMILY_NAME = “f1”;
  18.     private static final String COLUMN_NAME = “name”;
  19.     private static final String COLUMN_AGE = “age”;
  20.     private static final String ROW_KEY1 = “r1”;
  21.     private static final String ROW_KEY2 = “r2”;
  22.     public static void main(String[] args) throws Exception {
  23.         //构造能够访问HBase的configuration对象
  24.         Configuration conf = HBaseConfiguration.create();
  25.         conf.set(“hbase.rootdir”“hdfs://h2single:9000/hbase”);
  26.         conf.set(“hbase.zookeeper.quorum”“h2sliver113:2181”);
  27.         //HBaseAdmin是对HBase进行ddl操作的核心类
  28.         HBaseAdmin hBaseAdmin = new HBaseAdmin(conf);
  29.         if(!hBaseAdmin.tableExists(TABLE_NAME)){
  30.             HTableDescriptor htableDescriptor = new HTableDescriptor(TABLE_NAME);
  31.             htableDescriptor.addFamily(new HColumnDescriptor(FAMILY_NAME));
  32.             hBaseAdmin.createTable(htableDescriptor);
  33.             System.out.println(“table create success”);
  34.         }else{
  35.             System.out.println(“table exists”);
  36.         }
  37.         //使用HTable可以对HBase的表中的数据进行增删改查
  38.         HTable hTable = new HTable(conf, TABLE_NAME);
  39.         // 增加数据
  40.         List<Put> putList = new ArrayList<Put>();
  41.         Put put1 = new Put(ROW_KEY1.getBytes());
  42.         put1.add(FAMILY_NAME.getBytes(), COLUMN_NAME.getBytes(), “zhangsan”.getBytes());
  43.         put1.add(FAMILY_NAME.getBytes(), COLUMN_AGE.getBytes(), “23”.getBytes());
  44.         putList.add(put1);
  45.         Put put2 = new Put(ROW_KEY2.getBytes());
  46.         put2.add(FAMILY_NAME.getBytes(), COLUMN_NAME.getBytes(), “lisi”.getBytes());
  47.         put2.add(FAMILY_NAME.getBytes(), COLUMN_AGE.getBytes(), “24”.getBytes());
  48.         putList.add(put2);
  49.         hTable.put(putList);
  50.         // 根据rowkey得到记录后 获取此记录对应的列信息
  51.         Get get = new Get(ROW_KEY1.getBytes());
  52.         Result get1 = hTable.get(get);
  53.         String name1 = new String(get1.getValue(FAMILY_NAME.getBytes(), COLUMN_NAME.getBytes()));
  54.         String age1 = new String(get1.getValue(FAMILY_NAME.getBytes(), COLUMN_AGE.getBytes()));
  55.         //System.out.println(get1+”\t”+name1+”\t”+age1);
  56.         // 指定行范围来查询多条记录 
  57.         Scan scan = new Scan();
  58.         scan.setStartRow(ROW_KEY1.getBytes());
  59.         scan.setStopRow(ROW_KEY2.getBytes());
  60.         ResultScanner scanner = hTable.getScanner(scan);
  61.         for (Result result : scanner) {
  62.             String rowKey = new String(result.getRow());
  63.             String name = new String(result.getValue(FAMILY_NAME.getBytes(), COLUMN_NAME.getBytes()));
  64.             String age = new String(result.getValue(FAMILY_NAME.getBytes(), COLUMN_AGE.getBytes()));
  65.             System.out.println(rowKey+“\t”+name+“\t”+age1);
  66.         }
  67.         // 根据rowkey删除记录
  68.         Delete delete = new Delete(ROW_KEY1.getBytes());
  69.         hTable.delete(delete);
  70.         // 删除表
  71.         hBaseAdmin.disableTable(TABLE_NAME);
  72.         hBaseAdmin.deleteTable(TABLE_NAME);
  73.     }
  74. }
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<Put> putList = new ArrayList<Put>();

 

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

 

 

来源URL:http://chengjianxiaoxue.iteye.com/blog/2169688