首页 > 代码库 > hbase连接linux开发过程

hbase连接linux开发过程

最近近公司被安排做hbase开发,太久没做今天记录下过程

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.client.HBaseAdmin;


public class getHbaseConfiguration {

private static Configuration conf = null;
public getHbaseConfiguration()
{

}

//这里必须预先处理化不然不然可能会出现未初始化conf就开始使用的问题
static
{
if(null == conf)
{
conf = HBaseConfiguration.create();
Path path = new Path("hbase-site.xml");
conf.addResource(path);
}
// return conf;
}
public void createTable(String tableName) throws IOException
{

HBaseAdmin admin = new HBaseAdmin(conf);
if(admin.tableExists(tableName))
{
System.out.println("Exists tables");
}
HTableDescriptor hTabeDescriptor = new HTableDescriptor("tableName");
hTabeDescriptor.addFamily(new HColumnDescriptor("cf1"));
admin.createTable(hTabeDescriptor);

}
public static void main(String[] args) {
//new getHbaseConfiguration().getInstance();
try {
new getHbaseConfiguration().createTable("test_1");
} catch (IOException e) {
e.printStackTrace();
}
}
}

被初始化的conf要求,可以从集群中hbase目录下conf拷贝,记得集群先hbase shell ---list测试集群可用否

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="http://www.mamicode.com/configuration.xsl"?>
<!--
/**
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-->
<configuration>
<property>
<name>hbase.rootdir</name>
<value>hdfs://CDHProject1:9000/hbase</value>
</property>
<property>
<name>hbase.cluster.distributed</name>
<value>true</value>
</property>
<property>
<name>hbase.tmp.dir</name>
<value>/home/hadoop/bigdata/softs/hbase-1.0.0-cdh5.5.5/data/tmp</value>
</property>
<!-- zookeeper- -->
<property>
<name>hbase.zookeeper.quorum</name>
<value>CDHProject1,CDHProject2,CDHProject3</value>
</property>
<property>
<name>hbase.zookeeper.property.clientPort</name>
<value>2181</value>
</property>
</configuration>

 

hbase连接linux开发过程