Hbase初探,
安装配置
[xxxycentos@xxxycentos7 hbase-1.1.5]$ bin/start-hbase.sh
localhost: starting zookeeper, logging to /app/lxxxib/hbase-1.1.5/bin/../logs/hbase-xxxycentos-zookeeper-xxxycentos7.com.out
starting master, logging to /app/lib/hbase-1.1.5/bin/../logs/hbase-xxxycentos-master-xxxycentos7.com.out
starting regionserver, logging to /app/lib/hbase-1.1.5/bin/../logs/hbase-xxxycentos-1-regionserver-xxxycentos7.com.out
[xxxycentos@xxxycentos7 hbase-1.1.5]$ jps
20129 HMaster
9557 ResourceManager
10310 DataNode
9112 NameNode
9400 SecondaryNameNode
20074 HQuorumPeer
20315 Jps
9661 NodeManager
20270 GetJavaProperty
利用shell命令在Hbase中创建表
[xxxycentos@xxxycentos7 hbase-1.1.5]$ hbase shell
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/app/lib/hbase-1.1.5/lib/slf4j-log4j12-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/app/lib/hadoop-2.7.3/share/hadoop/common/lib/slf4j-log4j12-1.7.10.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]
2019-04-18 11:50:29,387 WARN [main] util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
HBase Shell; enter 'help<RETURN>' for list of supported commands.
Type "exit<RETURN>" to leave the HBase Shell
Version 1.1.5, r239b80456118175b340b2e562a5568b5c744252e, Sun May 8 20:29:26 PDT 2016
hbase(main):001:0> create 'student','Sname','Ssex','Sage','Sdept','course'
0 row(s) in 9.0780 seconds
=> Hbase::Table - student
hbase(main):002:0>
利用Java API与HBase进行交互
编写java程序,来对HBase数据库进行增删改查等操作
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import java.io.IOException;
public class ExampleForHbase{
public static Configuration configuration;
public static Connection connection;
public static Admin admin;
public static void main(String[] args)throws IOException{
createTable("Score",new String[]{"sname","course"});
}
public static void init(){
configuration = HBaseConfiguration.create();
configuration.set("hbase.rootdir","hdfs://192.168.1.1xx:9000/hbase");
try{
connection = ConnectionFactory.createConnection(configuration);
admin = connection.getAdmin();
}catch (IOException e){
e.printStackTrace();
}
}
//
public static void close(){
try{
if(admin != null){
admin.close();
}
if(null != connection){
connection.close();
}
}catch (IOException e){
e.printStackTrace();
}
}
/**
* * @param myTableName
* @param colFamily
* @throws IOException
*/
public static void createTable(String myTableName,String[] colFamily) throws IOException {
init();
TableName tableName = TableName.valueOf(myTableName);
if(admin.tableExists(tableName)){
System.out.println("talbe is exists!");
}else {
HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
for(String str:colFamily){
HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(str);
hTableDescriptor.addFamily(hColumnDescriptor);
}
admin.createTable(hTableDescriptor);
System.out.println("create table success");
}
close();
}
/**
*
* @param tableName
* @throws IOException
*/
public static void deleteTable(String tableName) throws IOException {
init();
TableName tn = TableName.valueOf(tableName);
if (admin.tableExists(tn)) {
admin.disableTable(tn);
admin.deleteTable(tn);
}
close();
}
/**
*
* @throws IOException
*/
public static void listTables() throws IOException {
init();
HTableDescriptor hTableDescriptors[] = admin.listTables();
for(HTableDescriptor hTableDescriptor :hTableDescriptors){
System.out.println(hTableDescriptor.getNameAsString());
}
close();
}
/**
*
* @param tableName
* @param rowKey
* @param colFamily
* @param col
* @param val
* @throws IOException
*/
public static void insertRow(String tableName,String rowKey,String colFamily,String col,String val) throws IOException {
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Put put = new Put(rowKey.getBytes());
put.addColumn(colFamily.getBytes(), col.getBytes(), val.getBytes());
table.put(put);
table.close();
close();
}
/**
* @param tableName
* @param rowKey
* @param colFamily
* @param col
* @throws IOException
*/
public static void deleteRow(String tableName,String rowKey,String colFamily,String col) throws IOException {
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Delete delete = new Delete(rowKey.getBytes());
//delete.addFamily(colFamily.getBytes());
//delete.addColumn(colFamily.getBytes(), col.getBytes());
table.delete(delete);
table.close();
close();
}
/**
*
* @param tableName
* @param rowKey
* @param colFamily
* @param col
* @throws IOException
*/
public static void getData(String tableName,String rowKey,String colFamily,String col)throws IOException{
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Get get = new Get(rowKey.getBytes());
get.addColumn(colFamily.getBytes(),col.getBytes());
Result result = table.get(get);
showCell(result);
table.close();
close();
}
/**
*
* @param result
*/
public static void showCell(Result result){
Cell[] cells = result.rawCells();
for(Cell cell:cells){
System.out.println("RowName:"+new String(CellUtil.cloneRow(cell))+" ");
System.out.println("Timetamp:"+cell.getTimestamp()+" ");
System.out.println("column Family:"+new String(CellUtil.cloneFamily(cell))+" ");
System.out.println("row Name:"+new String(CellUtil.cloneQualifier(cell))+" ");
System.out.println("value:"+new String(CellUtil.cloneValue(cell))+" ");
}
}
}
报错
[xxxycentos@xxxycentos7 Chapter04]$ javac -Djava.ext.dirs=/app/lib/hbase-1.1.5/lib/ *.java
[xxxycentos@xxxycentos7 Chapter04]$ echo Main-class: ExampleForHbase >mainfest.txt[xxxycentos@xxxycentos7 Chapter04]$ cat mainfest.txt
Main-class: ExampleForHbase
[xxxycentos@xxxycentos7 Chapter04]$ jar cvfm ExampleForHbase.jar mainfest.txt ExampleForHbase.class
added manifest
adding: ExampleForHbase.class(in = 5408) (out= 2396)(deflated 55%)
[xxxycentos@xxxycentos7 Chapter04]$ hadoop jar ExampleForHbase.jar
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/hadoop/hbase/client/Result
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
at java.lang.Class.getMethod0(Class.java:3018)
at java.lang.Class.getMethod(Class.java:1784)
at org.apache.hadoop.util.RunJar.run(RunJar.java:215)
at org.apache.hadoop.util.RunJar.main(RunJar.java:136)
Caused by: java.lang.ClassNotFoundException: org.apache.hadoop.hbase.client.Result
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 7 more
[xxxycentos@xxxycentos7 Chapter04]$
NoClassDefFoundError错误的发生,是因为Java虚拟机在编译时能找到合适的类,而在运行时不能找到合适的类导致的错误。例如在运行时我们想调用某个类的方法或者访问这个类的静态成员的时候,发现这个类不可用,此时Java虚拟机就会抛出NoClassDefFoundError错误。与ClassNotFoundException的不同在于,这个错误发生只在运行时需要加载对应的类不成功,而不是编译时发生。很多Java开发者很容易在这里把这两个错误搞混。
简单总结就是,NoClassDefFoundError发生在编译时对应的类可用,而运行时在Java的classpath路径中,对应的类不可用导致的错误。发生NoClassDefFoundError错误时,你能看到如下的错误日志:
简化代码先试一下新建表
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import java.io.IOException;
public class ExampleForHbase{
public static Configuration configuration;
public static Connection connection;
public static Admin admin;
public static void main(String[] args)throws IOException{
createTable("Score",new String[]{"sname","course"});
}
public static void init(){
configuration = HBaseConfiguration.create();
configuration.set("hbase.rootdir","hdfs://192.168.1.1xx:9000/hbase");
try{
connection = ConnectionFactory.createConnection(configuration);
admin = connection.getAdmin();
}catch (IOException e){
e.printStackTrace();
}
}
//
public static void close(){
try{
if(admin != null){
admin.close();
}
if(null != connection){
connection.close();
}
}catch (IOException e){
e.printStackTrace();
}
}
/**
* * @param myTableName
* @param colFamily
* @throws IOException
*/
public static void createTable(String myTableName,String[] colFamily) throws IOException {
init();
TableName tableName = TableName.valueOf(myTableName);
if(admin.tableExists(tableName)){
System.out.println("talbe is exists!");
}else {
HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
for(String str:colFamily){
HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(str);
hTableDescriptor.addFamily(hColumnDescriptor);
}
admin.createTable(hTableDescriptor);
System.out.println("create table success");
}
close();
}
// /**
// *
// * @param tableName
// * @throws IOException
// */
// public static void deleteTable(String tableName) throws IOException {
// init();
// TableName tn = TableName.valueOf(tableName);
// if (admin.tableExists(tn)) {
// admin.disableTable(tn);
// admin.deleteTable(tn);
// }
// close();
// }
// /**
// *
// * @throws IOException
// */
// public static void listTables() throws IOException {
// init();
// HTableDescriptor hTableDescriptors[] = admin.listTables();
// for(HTableDescriptor hTableDescriptor :hTableDescriptors){
// System.out.println(hTableDescriptor.getNameAsString());
// }
// close();
// }
// /**
// *
// * @param tableName
// * @param rowKey
// * @param colFamily
// * @param col
// * @param val
// * @throws IOException
// */
// public static void insertRow(String tableName,String rowKey,String colFamily,String col,String val) throws IOException {
// init();
// Table table = connection.getTable(TableName.valueOf(tableName));
// Put put = new Put(rowKey.getBytes());
// put.addColumn(colFamily.getBytes(), col.getBytes(), val.getBytes());
// table.put(put);
// table.close();
// close();
// }
// /**
// * @param tableName
// * @param rowKey
// * @param colFamily
// * @param col
// * @throws IOException
// */
// public static void deleteRow(String tableName,String rowKey,String colFamily,String col) throws IOException {
// init();
// Table table = connection.getTable(TableName.valueOf(tableName));
// Delete delete = new Delete(rowKey.getBytes());
// //delete.addFamily(colFamily.getBytes());
// //delete.addColumn(colFamily.getBytes(), col.getBytes());
// table.delete(delete);
// table.close();
// close();
// }
// /**
// *
// * @param tableName
// * @param rowKey
// * @param colFamily
// * @param col
// * @throws IOException
// */
// public static void getData(String tableName,String rowKey,String colFamily,String col)throws IOException{
// init();
// Table table = connection.getTable(TableName.valueOf(tableName));
// Get get = new Get(rowKey.getBytes());
// get.addColumn(colFamily.getBytes(),col.getBytes());
// Result result = table.get(get);
// showCell(result);
// table.close();
// close();
// }
// /**
// *
// * @param result
// */
// public static void showCell(Result result){
// Cell[] cells = result.rawCells();
// for(Cell cell:cells){
// System.out.println("RowName:"+new String(CellUtil.cloneRow(cell))+" ");
// System.out.println("Timetamp:"+cell.getTimestamp()+" ");
// System.out.println("column Family:"+new String(CellUtil.cloneFamily(cell))+" ");
// System.out.println("row Name:"+new String(CellUtil.cloneQualifier(cell))+" ");
// System.out.println("value:"+new String(CellUtil.cloneValue(cell))+" ");
// }
// }
}
报错
[xxxycentos@xxxycentos7 Chapter04]$ javac -Djava.ext.dirs=/app/lib/hbase-1.1.5/lib/ ExampleForHbase.java
[xxxycentos@xxxycentos7 Chapter04]$ jar cvfm ExampleForHbase.jar mainfest.txt ExampleForHbase.class
added manifest
adding: ExampleForHbase.class(in = 2596) (out= 1252)(deflated 51%)
[xxxycentos@xxxycentos7 Chapter04]$ hadoop jar ExampleForHbase.jar
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/hadoop/hbase/HBaseConfiguration
at ExampleForHbase.init(ExampleForHbase.java:17)
at ExampleForHbase.createTable(ExampleForHbase.java:47)
at ExampleForHbase.main(ExampleForHbase.java:12)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.hadoop.util.RunJar.run(RunJar.java:221)
at org.apache.hadoop.util.RunJar.main(RunJar.java:136)
Caused by: java.lang.ClassNotFoundException: org.apache.hadoop.hbase.HBaseConfiguration
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 9 more
解决:
在hadoop安装目录下找到hadoop-env.sh文件,
添加 : export HADOOP_CLASSPATH=/app/lib/hbase-1.1.5/lib/*
检查是否新建了表
hbase(main):001:0> list
public static void main(String[] args)throws IOException{
// createTable("Score",new String[]{"sname","course"});
insertRow("Score", "95001", "sname", "", "Mary");
insertRow("Score", "95001", "course", "Math", "88");
insertRow("Score", "95001", "course", "English", "85");
// deleteRow("Score", "95001", "course", "Math");
// deleteRow("Score", "95001", "course", "");
// deleteRow("Score", "95001", "", "");
// getData("Score", "95001", "course", "Math");
// getData("Score", "95001", "sname", "");
// deleteTable("Score");
}
public static void main(String[] args)throws IOException{
getData("Score", "95001", "course", "Math");
getData("Score", "95001", "sname", "");
}
RowName:95001
Timetamp:1555577620088
column Family:course
row Name:Math
value:88
RowName:95001
Timetamp:1555577619136
column Family:sname
row Name:
value:Mary
每次修改完java 代码后都要重复以下操作
[xxxycentos@xxxycentos7 Chapter04]$ javac -Djava.ext.dirs=/app/lib/hbase-1.1.5/lib/ ExampleForHbase.java
[xxxycentos@xxxycentos7 Chapter04]$ jar cvfm ExampleForHbase.jar mainfest.txt ExampleForHbase.class
added manifest
adding: ExampleForHbase.class(in = 5442) (out= 2419)(deflated 55%)
[xxxycentos@xxxycentos7 Chapter04]$ hadoop jar ExampleForHbase.jar
本站文章为和通数据库网友分享或者投稿,欢迎任何形式的转载,但请务必注明出处.
同时文章内容如有侵犯了您的权益,请联系QQ:970679559,我们会在尽快处理。