5/04/07 01:14:50 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
15/04/07 01:14:51 INFO zookeeper.RecoverableZooKeeper: Process identifier=hconnection-0x5f556d56 connecting to ZooKeeper ensemble=192.168.237.129:2181,192.168.237.128:2181,192.168.237.130:2181
15/04/07 01:14:51 INFO zookeeper.ZooKeeper: Client environment:zookeeper.version=3.4.6-1569965, built on 02/20/2014 09:09 GMT
15/04/07 01:14:51 INFO zookeeper.ZooKeeper: Client environment:host.name=master
15/04/07 01:14:51 INFO zookeeper.ZooKeeper: Client environment:java.version=1.7.0_71
15/04/07 01:14:51 INFO zookeeper.ZooKeeper: Client environment:java.vendor=Oracle Corporation
15/04/07 01:14:51 INFO zookeeper.ZooKeeper: Client environment:java.home=/usr/java/jdk1.7.0_71/jre
15/04/07 01:14:51 INFO zookeeper.ZooKeeper: Client environment:java.class.path=
15/04/07 01:14:51 INFO zookeeper.ZooKeeper: Client environment:java.library.path=/home/zkpk/hadoop-2.5.1/lib/native
15/04/07 01:14:51 INFO zookeeper.ZooKeeper: Client environment:java.io.tmpdir=/tmp
15/04/07 01:14:51 INFO zookeeper.ZooKeeper: Client environment:java.compiler=<NA>
15/04/07 01:14:51 INFO zookeeper.ZooKeeper: Client environment:os.name=Linux
15/04/07 01:14:51 INFO zookeeper.ZooKeeper: Client environment:os.arch=amd64
15/04/07 01:14:51 INFO zookeeper.ZooKeeper: Client environment:os.version=2.6.32-431.el6.x86_64
15/04/07 01:14:51 INFO zookeeper.ZooKeeper: Client environment:user.name=zkpk
15/04/07 01:14:51 INFO zookeeper.ZooKeeper: Client environment:user.home=/home/zkpk
15/04/07 01:14:51 INFO zookeeper.ZooKeeper: Client environment:user.dir=/home/zkpk/hadoop-2.5.1
15/04/07 01:14:51 INFO zookeeper.ZooKeeper: Initiating client connection, connectString=192.168.237.129:2181,192.168.237.128:2181,192.168.237.130:2181 sessionTimeout=60000 watcher=hconnection-0x5f556d56, quorum=192.168.237.129:2181,192.168.237.128:2181,192.168.237.130:2181, baseZNode=/hbase
java.io.IOException: java.lang.reflect.InvocationTargetException
at org.apache.hadoop.hbase.client.HConnectionManager.createConnection(HConnectionManager.java:411)
at org.apache.hadoop.hbase.client.HConnectionManager.createConnection(HConnectionManager.java:388)
at org.apache.hadoop.hbase.client.HConnectionManager.getConnection(HConnectionManager.java:269)
at org.apache.hadoop.hbase.client.HBaseAdmin.<init>(HBaseAdmin.java:192)
at com.hebut.hbase.SimpleHBase.createTable(SimpleHBase.java:49)
at com.hebut.hbase.SimpleHBase.main(SimpleHBase.java:32)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.hadoop.util.RunJar.main(RunJar.java:212)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at org.apache.hadoop.hbase.client.HConnectionManager.createConnection(HConnectionManager.java:409)
... 10 more
Caused by: java.lang.NoClassDefFoundError: org/cloudera/htrace/Trace
at org.apache.hadoop.hbase.zookeeper.RecoverableZooKeeper.exists(RecoverableZooKeeper.java:218)
at org.apache.hadoop.hbase.zookeeper.ZKUtil.checkExists(ZKUtil.java:479)
at org.apache.hadoop.hbase.zookeeper.ZKClusterId.readClusterIdZNode(ZKClusterId.java:65)
at org.apache.hadoop.hbase.client.ZooKeeperRegistry.getClusterId(ZooKeeperRegistry.java:83)
at org.apache.hadoop.hbase.client.HConnectionManager$HConnectionImplementation.retrieveClusterId(HConnectionManager.java:837)
at org.apache.hadoop.hbase.client.HConnectionManager$HConnectionImplementation.<init>(HConnectionManager.java:640)
... 15 more
Caused by: java.lang.ClassNotFoundException: org.cloudera.htrace.Trace
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
... 21 more
15/04/07 01:14:51 INFO zookeeper.ClientCnxn: Opening socket connection to server 192.168.237.128/192.168.237.128:2181. Will not attempt to authenticate using SASL (unknown error)
3 个解决方案
#1
java代码:
package com.hebut.hbase;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
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.TableName;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
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;
import org.apache.hadoop.hbase.util.Bytes;
public class SimpleHBase {
public static void main(String[] args) {
Configuration configuration=HBaseConfiguration.create();
String tableName="student";
createTable(configuration, tableName);
// addData(configuration, tableName);
// getData(configuration, tableName);
// getAllData(configuration, tableName);
// deleteDate(configuration, tableName);
// dropTable(configuration, tableName);
}
/**
* create a new Table
* @param configuration Configuration
* @param tableName String,the new Table's name
* */
public static void createTable(Configuration configuration,String tableName){
HBaseAdmin admin;
try {
admin = new HBaseAdmin(configuration);
if(admin.tableExists(tableName)){
admin.disableTable(tableName);
admin.deleteTable(tableName);
System.out.println(tableName+"is exist ,delete ......");
}
HTableDescriptor tableDescriptor=new HTableDescriptor(TableName.valueOf(tableName));
tableDescriptor.addFamily(new HColumnDescriptor("info"));
tableDescriptor.addFamily(new HColumnDescriptor("address"));
admin.createTable(tableDescriptor);
System.out.println("end create table");
} catch (MasterNotRunningException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ZooKeeperConnectionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* Delete the existing table
* @param configuration Configuration
* @param tableName String,Table's name
* */
public static void dropTable(Configuration configuration,String tableName){
HBaseAdmin admin;
try {
admin = new HBaseAdmin(configuration);
if(admin.tableExists(tableName)){
admin.disableTable(tableName);
admin.deleteTable(tableName);
System.out.println(tableName+"delete success!");
}else{
System.out.println(tableName+"Table does not exist!");
}
} catch (MasterNotRunningException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ZooKeeperConnectionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* insert a data
* @param configuration Configuration
* @param tableName String,Table's name
* */
public static void addData(Configuration configuration,String tableName){
HBaseAdmin admin;
try {
admin = new HBaseAdmin(configuration);
if(admin.tableExists(tableName)){
HTable table=new HTable(configuration, tableName);
Put put=new Put(Bytes.toBytes("zhangsan"));
put.add(Bytes.toBytes("info"), Bytes.toBytes("age"), Bytes.toBytes("28"));
table.put(put);
System.out.println("add success!");
}else{
System.out.println(tableName+"Table does not exist!");
}
} catch (MasterNotRunningException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ZooKeeperConnectionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* Delete a data
* @param configuration Configuration
* @param tableName String,Table's name
* */
public static void deleteDate(Configuration configuration,String tableName){
HBaseAdmin admin;
try {
admin=new HBaseAdmin(configuration);
if(admin.tableExists(tableName)){
HTable table=new HTable(configuration, tableName);
Delete delete=new Delete(Bytes.toBytes("zhangsan"));
table.delete(delete);
System.out.println("delete success!");
}else{
System.out.println("Table does not exist!");
}
} catch (MasterNotRunningException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ZooKeeperConnectionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* get a data
* @param configuration Configuration
* @param tableName String,Table's name
* */
public static void getData(Configuration configuration,String tableName){
HTable table;
try {
table = new HTable(configuration, tableName);
Get get=new Get(Bytes.toBytes("zhangsan"));
Result result=table.get(get);
for(Cell cell:result.rawCells()){
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))+" ");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* insert all data
* @param configuration Configuration
* @param tableName String,Table's name
* */
public static void getAllData(Configuration configuration,String tableName){
HTable table;
try {
table=new HTable(configuration, tableName);
Scan scan=new Scan();
ResultScanner results=table.getScanner(scan);
for(Result result:results){
for(Cell cell:result.rawCells()){
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))+" ");
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.hebut.hbase;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
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.TableName;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
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;
import org.apache.hadoop.hbase.util.Bytes;
public class SimpleHBase {
public static void main(String[] args) {
Configuration configuration=HBaseConfiguration.create();
String tableName="student";
createTable(configuration, tableName);
// addData(configuration, tableName);
// getData(configuration, tableName);
// getAllData(configuration, tableName);
// deleteDate(configuration, tableName);
// dropTable(configuration, tableName);
}
/**
* create a new Table
* @param configuration Configuration
* @param tableName String,the new Table's name
* */
public static void createTable(Configuration configuration,String tableName){
HBaseAdmin admin;
try {
admin = new HBaseAdmin(configuration);
if(admin.tableExists(tableName)){
admin.disableTable(tableName);
admin.deleteTable(tableName);
System.out.println(tableName+"is exist ,delete ......");
}
HTableDescriptor tableDescriptor=new HTableDescriptor(TableName.valueOf(tableName));
tableDescriptor.addFamily(new HColumnDescriptor("info"));
tableDescriptor.addFamily(new HColumnDescriptor("address"));
admin.createTable(tableDescriptor);
System.out.println("end create table");
} catch (MasterNotRunningException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ZooKeeperConnectionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* Delete the existing table
* @param configuration Configuration
* @param tableName String,Table's name
* */
public static void dropTable(Configuration configuration,String tableName){
HBaseAdmin admin;
try {
admin = new HBaseAdmin(configuration);
if(admin.tableExists(tableName)){
admin.disableTable(tableName);
admin.deleteTable(tableName);
System.out.println(tableName+"delete success!");
}else{
System.out.println(tableName+"Table does not exist!");
}
} catch (MasterNotRunningException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ZooKeeperConnectionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* insert a data
* @param configuration Configuration
* @param tableName String,Table's name
* */
public static void addData(Configuration configuration,String tableName){
HBaseAdmin admin;
try {
admin = new HBaseAdmin(configuration);
if(admin.tableExists(tableName)){
HTable table=new HTable(configuration, tableName);
Put put=new Put(Bytes.toBytes("zhangsan"));
put.add(Bytes.toBytes("info"), Bytes.toBytes("age"), Bytes.toBytes("28"));
table.put(put);
System.out.println("add success!");
}else{
System.out.println(tableName+"Table does not exist!");
}
} catch (MasterNotRunningException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ZooKeeperConnectionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* Delete a data
* @param configuration Configuration
* @param tableName String,Table's name
* */
public static void deleteDate(Configuration configuration,String tableName){
HBaseAdmin admin;
try {
admin=new HBaseAdmin(configuration);
if(admin.tableExists(tableName)){
HTable table=new HTable(configuration, tableName);
Delete delete=new Delete(Bytes.toBytes("zhangsan"));
table.delete(delete);
System.out.println("delete success!");
}else{
System.out.println("Table does not exist!");
}
} catch (MasterNotRunningException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ZooKeeperConnectionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* get a data
* @param configuration Configuration
* @param tableName String,Table's name
* */
public static void getData(Configuration configuration,String tableName){
HTable table;
try {
table = new HTable(configuration, tableName);
Get get=new Get(Bytes.toBytes("zhangsan"));
Result result=table.get(get);
for(Cell cell:result.rawCells()){
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))+" ");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* insert all data
* @param configuration Configuration
* @param tableName String,Table's name
* */
public static void getAllData(Configuration configuration,String tableName){
HTable table;
try {
table=new HTable(configuration, tableName);
Scan scan=new Scan();
ResultScanner results=table.getScanner(scan);
for(Result result:results){
for(Cell cell:result.rawCells()){
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))+" ");
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
#2
求助啊,大神呢?
#3
java.lang.NoClassDefFoundError: org/cloudera/htrace/Trace
出现这个问题是因为缺少htrace-core.jar,该包在Hbase的lib文件中
出现这个问题是因为缺少htrace-core.jar,该包在Hbase的lib文件中
#1
java代码:
package com.hebut.hbase;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
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.TableName;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
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;
import org.apache.hadoop.hbase.util.Bytes;
public class SimpleHBase {
public static void main(String[] args) {
Configuration configuration=HBaseConfiguration.create();
String tableName="student";
createTable(configuration, tableName);
// addData(configuration, tableName);
// getData(configuration, tableName);
// getAllData(configuration, tableName);
// deleteDate(configuration, tableName);
// dropTable(configuration, tableName);
}
/**
* create a new Table
* @param configuration Configuration
* @param tableName String,the new Table's name
* */
public static void createTable(Configuration configuration,String tableName){
HBaseAdmin admin;
try {
admin = new HBaseAdmin(configuration);
if(admin.tableExists(tableName)){
admin.disableTable(tableName);
admin.deleteTable(tableName);
System.out.println(tableName+"is exist ,delete ......");
}
HTableDescriptor tableDescriptor=new HTableDescriptor(TableName.valueOf(tableName));
tableDescriptor.addFamily(new HColumnDescriptor("info"));
tableDescriptor.addFamily(new HColumnDescriptor("address"));
admin.createTable(tableDescriptor);
System.out.println("end create table");
} catch (MasterNotRunningException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ZooKeeperConnectionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* Delete the existing table
* @param configuration Configuration
* @param tableName String,Table's name
* */
public static void dropTable(Configuration configuration,String tableName){
HBaseAdmin admin;
try {
admin = new HBaseAdmin(configuration);
if(admin.tableExists(tableName)){
admin.disableTable(tableName);
admin.deleteTable(tableName);
System.out.println(tableName+"delete success!");
}else{
System.out.println(tableName+"Table does not exist!");
}
} catch (MasterNotRunningException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ZooKeeperConnectionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* insert a data
* @param configuration Configuration
* @param tableName String,Table's name
* */
public static void addData(Configuration configuration,String tableName){
HBaseAdmin admin;
try {
admin = new HBaseAdmin(configuration);
if(admin.tableExists(tableName)){
HTable table=new HTable(configuration, tableName);
Put put=new Put(Bytes.toBytes("zhangsan"));
put.add(Bytes.toBytes("info"), Bytes.toBytes("age"), Bytes.toBytes("28"));
table.put(put);
System.out.println("add success!");
}else{
System.out.println(tableName+"Table does not exist!");
}
} catch (MasterNotRunningException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ZooKeeperConnectionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* Delete a data
* @param configuration Configuration
* @param tableName String,Table's name
* */
public static void deleteDate(Configuration configuration,String tableName){
HBaseAdmin admin;
try {
admin=new HBaseAdmin(configuration);
if(admin.tableExists(tableName)){
HTable table=new HTable(configuration, tableName);
Delete delete=new Delete(Bytes.toBytes("zhangsan"));
table.delete(delete);
System.out.println("delete success!");
}else{
System.out.println("Table does not exist!");
}
} catch (MasterNotRunningException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ZooKeeperConnectionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* get a data
* @param configuration Configuration
* @param tableName String,Table's name
* */
public static void getData(Configuration configuration,String tableName){
HTable table;
try {
table = new HTable(configuration, tableName);
Get get=new Get(Bytes.toBytes("zhangsan"));
Result result=table.get(get);
for(Cell cell:result.rawCells()){
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))+" ");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* insert all data
* @param configuration Configuration
* @param tableName String,Table's name
* */
public static void getAllData(Configuration configuration,String tableName){
HTable table;
try {
table=new HTable(configuration, tableName);
Scan scan=new Scan();
ResultScanner results=table.getScanner(scan);
for(Result result:results){
for(Cell cell:result.rawCells()){
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))+" ");
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package com.hebut.hbase;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
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.TableName;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
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;
import org.apache.hadoop.hbase.util.Bytes;
public class SimpleHBase {
public static void main(String[] args) {
Configuration configuration=HBaseConfiguration.create();
String tableName="student";
createTable(configuration, tableName);
// addData(configuration, tableName);
// getData(configuration, tableName);
// getAllData(configuration, tableName);
// deleteDate(configuration, tableName);
// dropTable(configuration, tableName);
}
/**
* create a new Table
* @param configuration Configuration
* @param tableName String,the new Table's name
* */
public static void createTable(Configuration configuration,String tableName){
HBaseAdmin admin;
try {
admin = new HBaseAdmin(configuration);
if(admin.tableExists(tableName)){
admin.disableTable(tableName);
admin.deleteTable(tableName);
System.out.println(tableName+"is exist ,delete ......");
}
HTableDescriptor tableDescriptor=new HTableDescriptor(TableName.valueOf(tableName));
tableDescriptor.addFamily(new HColumnDescriptor("info"));
tableDescriptor.addFamily(new HColumnDescriptor("address"));
admin.createTable(tableDescriptor);
System.out.println("end create table");
} catch (MasterNotRunningException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ZooKeeperConnectionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* Delete the existing table
* @param configuration Configuration
* @param tableName String,Table's name
* */
public static void dropTable(Configuration configuration,String tableName){
HBaseAdmin admin;
try {
admin = new HBaseAdmin(configuration);
if(admin.tableExists(tableName)){
admin.disableTable(tableName);
admin.deleteTable(tableName);
System.out.println(tableName+"delete success!");
}else{
System.out.println(tableName+"Table does not exist!");
}
} catch (MasterNotRunningException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ZooKeeperConnectionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* insert a data
* @param configuration Configuration
* @param tableName String,Table's name
* */
public static void addData(Configuration configuration,String tableName){
HBaseAdmin admin;
try {
admin = new HBaseAdmin(configuration);
if(admin.tableExists(tableName)){
HTable table=new HTable(configuration, tableName);
Put put=new Put(Bytes.toBytes("zhangsan"));
put.add(Bytes.toBytes("info"), Bytes.toBytes("age"), Bytes.toBytes("28"));
table.put(put);
System.out.println("add success!");
}else{
System.out.println(tableName+"Table does not exist!");
}
} catch (MasterNotRunningException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ZooKeeperConnectionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* Delete a data
* @param configuration Configuration
* @param tableName String,Table's name
* */
public static void deleteDate(Configuration configuration,String tableName){
HBaseAdmin admin;
try {
admin=new HBaseAdmin(configuration);
if(admin.tableExists(tableName)){
HTable table=new HTable(configuration, tableName);
Delete delete=new Delete(Bytes.toBytes("zhangsan"));
table.delete(delete);
System.out.println("delete success!");
}else{
System.out.println("Table does not exist!");
}
} catch (MasterNotRunningException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ZooKeeperConnectionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* get a data
* @param configuration Configuration
* @param tableName String,Table's name
* */
public static void getData(Configuration configuration,String tableName){
HTable table;
try {
table = new HTable(configuration, tableName);
Get get=new Get(Bytes.toBytes("zhangsan"));
Result result=table.get(get);
for(Cell cell:result.rawCells()){
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))+" ");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* insert all data
* @param configuration Configuration
* @param tableName String,Table's name
* */
public static void getAllData(Configuration configuration,String tableName){
HTable table;
try {
table=new HTable(configuration, tableName);
Scan scan=new Scan();
ResultScanner results=table.getScanner(scan);
for(Result result:results){
for(Cell cell:result.rawCells()){
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))+" ");
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
#2
求助啊,大神呢?
#3
java.lang.NoClassDefFoundError: org/cloudera/htrace/Trace
出现这个问题是因为缺少htrace-core.jar,该包在Hbase的lib文件中
出现这个问题是因为缺少htrace-core.jar,该包在Hbase的lib文件中