本文主要是介绍【HelloHBase】Hbase-2.1.0 首个操作程序及调试过程,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1 源码
package lsq.HelloHBase;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.*;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;
import org.mortbay.log.Log;
public class Chapter4 {
public static Configuration configuration;
public static Connection connection;
public static Admin admin;
public static void main(String[] args) throws IOException {
createTable("student",new String[] {"score"});
insertData("student","Jack","score","English","69");
insertData("student","Jack","score","Math","86");
insertData("student","Jack","score","Computer","77");
getData("student","Jack","score","English");
}
@SuppressWarnings("deprecation")
public static void init() {
configuration = HBaseConfiguration.create();
//configuration.setConfiguration("hbase.rootdir","hdfs://idx046:9000/hbase");
try {
//connection = ConnectionFactory.createConnection(configuration);
connection = ConnectionFactory.createConnection();
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();
}
}
public static void createTable(String myTableName, String[] colFamily) throws IOException {
init();
TableName tableName = TableName.valueOf(myTableName);
if ( admin.tableExists(tableName )) {
System.out.println("table exists!");
}else {
@SuppressWarnings("deprecation")
HTableDescriptor hTableDescriptor = new HTableDescriptor(tableName);
for (String str: colFamily) {
HColumnDescriptor hColumnDescriptor = new HColumnDescriptor(str);
hTableDescriptor.addFamily(hColumnDescriptor);
}
admin.createTable(hTableDescriptor);
}
close();
}
public static void insertData(String tableName,String rowkey,String colFamily, String col, String value) throws IOException {
init();
Table table = connection.getTable(TableName.valueOf(tableName));
Put put = new Put(Bytes.toBytes(rowkey));
put.addColumn(Bytes.toBytes(colFamily), Bytes.toBytes(col),Bytes.toBytes(value));
table.put(put);
table.close();
close();
}
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(Bytes.toBytes(rowkey));
get.addColumn(Bytes.toBytes(colFamily), Bytes.toBytes(col) );
Result result = table.get(get);
String msg = new String(result.getValue(colFamily.getBytes(),col==null?null:col.getBytes()));
System.out.println(msg);
Log.info(msg );
table.close();
close();
}
}
2 调试过程
问题#1
/*
[lsq@idx046 ~/data]$ hadoop jar HelloHBase-0.0.1-SNAPSHOT.jar lsq.HelloHBase.Chapter4
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/hadoop/hbase/client/TableDesc
riptor at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:348)
at org.apache.hadoop.util.RunJar.run(RunJar.java:214)
at org.apache.hadoop.util.RunJar.main(RunJar.java:136)
Caused by: java.lang.ClassNotFoundException: org.apache.hadoop.hbase.client.TableDescriptor
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 4 more
原因: 缺少 org/apache/hadoop/hbase/client/ ??
编辑 pom.xml , 添加依赖包复制:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<!-- ${project.build.directory}为Maven内置变量,缺省为target -->
<outputDirectory>${project.build.directory}/classes/lib</outputDirectory>
<!-- 表示是否不包含间接依赖的包 -->
<excludeTransitive>true</excludeTransitive>
<!-- 表示复制的jar文件去掉版本信息 -->
<stripVersion>true</stripVersion>
</configuration>
</execution>
</executions>
</plugin>
OK
*/
问题#2
/*
[lsq@idx046 ~/data]$ hadoop jar HelloHBase-0.0.1-SNAPSHOT.jar lsq.HelloHBase.Chapter4
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/hbase/thirdparty/com/google/c
ommon/cache/CacheLoader at org.apache.hadoop.hbase.client.ConnectionFactory.createConnection(ConnectionFactory.java
:202) at org.apache.hadoop.hbase.client.ConnectionFactory.createConnection(ConnectionFactory.java
:114) at lsq.HelloHBase.Chapter4.init(Chapter4.java:31)
at lsq.HelloHBase.Chapter4.createTable(Chapter4.java:50)
at lsq.HelloHBase.Chapter4.main(Chapter4.java:18)
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:497)
这篇关于【HelloHBase】Hbase-2.1.0 首个操作程序及调试过程的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!