传送门
hadoop入门系列--hbase基础知识点
hadoop入门系列--从本地把数据导入Hbase
hadoop入门系列--用java代码实现创建hbase表
hadoop入门系列--使用hbase过滤器(一篇全掌握)
传送门
注意:这里的代码完全是由题主亲自验证过的。
1)用java代码实现在hbase建表
package hbase_put_scan;
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.KeyValue;
import org.apache.hadoop.hbase.NamespaceDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
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.client.Table;
import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.util.Bytes;
public class HbaseUtility {
public static Configuration conf;
public static Connection conn;
/**
* 类级别的初始化,只是在类加载的时候做一次 配置zookeeper的端口2181
* 配置zookeeper的仲裁主机名centos,如果有多个机器,主机名间用冒号隔开 配置hbase master
* 还有一种方式是new一个configuration对象,然后用addresource方法去添加xml配置文件 但是像这样显式的配置是会覆盖xml里的配置的
*/
static {
conf = HBaseConfiguration.create();
conf.set("hbase.zookeeper.property", "2181");
conf.set("hbasae.zookeeper.quorum", "centos");
conf.set("hbase.master", "centos:60000");
try {
conn = ConnectionFactory.createConnection(conf);
}catch(IOException e) {
e.printStackTrace();
}
}
public static void createable(String tablename,String... ColumnFamilys) throws IOException{
Admin admin = conn.getAdmin();
HTableDescriptor table = new HTableDescriptor(TableName.valueOf(tablename));
for (String family : ColumnFamilys) {
HColumnDescriptor columnFamily = new HColumnDescriptor(family);
table.addFamily(columnFamily);
}
if (admin.tableExists(TableName.valueOf(tablename))) {
System.out.println("Table Exists");
}else {
admin.createTable(table);
System.out.println("Table Created");
admin.close();
}
}
public static void main(String[] args) throws IOException{
createable("111","imformation");
}
}
网友评论