新建maven工程
打开pom.xml添加hbase需要的依赖
<dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId> <version>2.3.5</version> </dependency> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-server</artifactId> <version>2.3.5</version> </dependency>
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HConstants;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
import java.io.IOException;
public class HBase {
public static void main(String[] args) {
//配置HBase信息,连接HBase数据库
Configuration conf = HBaseConfiguration.create();
conf.set(HConstants.HBASE_DIR, "hdfs://192.168.153.146:9000/hbase");
//给配置类添加配置
conf.set(HConstants.ZOOKEEPER_QUORUM, "192.168.153.146");
conf.set(HConstants.CLIENT_PORT_STR, "2181");
try {
//获取连接
Connection conn = ConnectionFactory.createConnection(conf);
System.out.println(conn);
Table stuTB = conn.getTable(TableName.valueOf("bigdata:student"));
Put put = new Put(Bytes.toBytes("rowkey11"));
put.addColumn("baseinfo".getBytes(), "name".getBytes(), "guo".getBytes());
put.addColumn("baseinfo".getBytes(), "age".getBytes(), "18".getBytes());
put.addColumn("baseinfo".getBytes(), "birthday".getBytes(), "1994-10-06".getBytes());
put.addColumn("schoolinfo".getBytes(), "name".getBytes(), "西华一高".getBytes());
put.addColumn("schoolinfo".getBytes(), "address".getBytes(), "西华".getBytes());
stuTB.put(put);
} catch (IOException e) {
e.printStackTrace();
}
}
}
import static org.junit.Assert.assertTrue;
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.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Unit test for simple App.
*/
public class AppTest {
static Configuration config = null;
private Connection conn = null;
private Admin admin;
@Before//测试之前先加载
public void init() throws IOException {
System.out.println("执行init()");
config = HBaseConfiguration.create();
config.set(HConstants.HBASE_DIR, "hdfs://192.168.153.147:9000/hbase");
config.set(HConstants.ZOOKEEPER_QUORUM, "192.168.153.147");
config.set(HConstants.CLIENT_PORT_STR, "2181");
conn = ConnectionFactory.createConnection(config);
admin = conn.getAdmin();
}
@Test
public void test1() {
System.out.println(conn);
System.out.println("执行test1()");
}
/**
* 创建命名空间
*/
@Test
public void createNameSpace() throws IOException {
NamespaceDescriptor kb21 = NamespaceDescriptor.create("kb21").build();
try {
admin.createNamespace(kb21);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 创建表
*/
@Test
public void createTable() throws IOException {
//创建表的描述类
TableName tableName = TableName.valueOf("bigdata:car2");
HTableDescriptor desc = new HTableDescriptor(tableName);
//创建列族的描述
HColumnDescriptor family1 = new HColumnDescriptor("info");
desc.addFamily(family1);
admin.createTable(desc);
}
/**
* 删除表
*
* @throws IOException
*/
@Test
public void createTable2() throws IOException {
admin.disableTable(TableName.valueOf("bigdata:car"));
admin.deleteTable(TableName.valueOf("bigdata:car"));
}
@Test
public void getAllNamespace() throws IOException {
String[] nps = admin.listNamespaces();
String s = Arrays.toString(nps);
System.out.println(s);
}
/**
*
*/
@Test
public void getAllNamespace2() throws IOException {
List<TableDescriptor> tableDesc = admin.listTableDescriptorsByNamespace("kb21".getBytes());
System.out.println(tableDesc.toString());
}
@After
public void close() throws IOException {
System.out.println("执行close()");
if (admin != null) {
admin.close();
}
if (conn != null) {
conn.close();
}
}
@Test
public void insertData() throws IOException {
Table table = conn.getTable(TableName.valueOf("bigdata:car2"));
Put put2 = new Put(Bytes.toBytes("model3"));
put2.addColumn("info".getBytes(), "brand".getBytes(), "TSLA".getBytes());
put2.addColumn("info".getBytes(), "country".getBytes(), "美国".getBytes());
put2.addColumn("info".getBytes(), "model".getBytes(), "轿车".getBytes());
put2.addColumn("info".getBytes(), "price".getBytes(), "23万".getBytes());
put2.addColumn("info".getBytes(), "data".getBytes(), "1994-10-01".getBytes());
Put put3 = new Put(Bytes.toBytes("modely"));
put3.addColumn("info".getBytes(), "brand".getBytes(), "TSLA".getBytes());
put3.addColumn("info".getBytes(), "country".getBytes(), "美国".getBytes());
put3.addColumn("info".getBytes(), "model".getBytes(), "suv".getBytes());
put3.addColumn("info".getBytes(), "price".getBytes(), "40万".getBytes());
put3.addColumn("info".getBytes(), "data".getBytes(), "1998-10-01".getBytes());
ArrayList<Put> list = new ArrayList<>();
list.add(put2);
list.add(put3);
table.put(list);
}
/**
* get查询
*/
@Test
public void queryData() throws IOException {
Table table = conn.getTable(TableName.valueOf("kb21:student"));
Get get = new Get(Bytes.toBytes("student1"));
Result result = table.get(get);
byte[] value = result.getValue(Bytes.toBytes("info1"), Bytes.toBytes("name"));
System.out.println("姓名:" + Bytes.toString(value));
value = result.getValue(Bytes.toBytes("info2"), Bytes.toBytes("school"));
System.out.println("学校:" + Bytes.toString(value));
}
@Test
public void scanData() throws IOException {
Table table = conn.getTable(TableName.valueOf("kb21:student"));
Scan scan = new Scan();
ResultScanner scanner = table.getScanner(scan);
for (Result result : scanner) {
byte[] value = result.getValue(Bytes.toBytes("info1"), Bytes.toBytes("name"));
System.out.println("姓名:" + Bytes.toString(value));
value = result.getValue(Bytes.toBytes("info2"), Bytes.toBytes("school"));
System.out.println("学校:" + Bytes.toString(value));
System.out.println(Bytes.toString(result.getRow()));
}
}
Hive连接Hbase
修改 /opt/soft/hive312/conf 下的 hive-site.xml
<property>
<name>hive.zookeeper.quorum</name>
<value>192.168.153.147</value>
</property><property>
<name>hbase.zookeeper.quorum</name>
<value>192.168.153.147</value>
</property><property>
<name>hive.aux.jars.path</name>
<value>file:///opt/soft/hive312/lib/hive-hbase-handler-3.1.2.jar,file:///opt/soft/hive312/lib/zookeeper-3.4.6.jar,file:///opt/soft/hive312/lib/hbase-client-2.3.5.jar,file:///opt/soft/hive312/lib/hbase-common-2.3.5-tests.jar,file:///opt/soft/hive312/lib/hbase-server-2.3.5.jar,file:///opt/soft/hive312/lib/hbase-common-2.3.5.jar,file:///opt/soft/hive312/lib/hbase-protocol-2.3.5.jar,file:///opt/soft/hive312/lib/htrace-core-3.2.0-incubating.jar</value>
</property>
把hbase jar包拷贝到hive
[root@guo147 conf]# cp /opt/soft/hbase235/lib/* /opt/soft/hive312/lib/
(会有重复的选 n 不覆盖)
删除hive guava-11.0.2.jar
[root@guo147 conf]# find ../lib/guava*
../lib/guava-11.0.2.jar
../lib/guava-27.0-jre.jar
[root@guo147 conf]# rm -rf ../lib/guava-11.0.2.jar
删除hbase guava-11.0.2.jar
[root@guo147 lib]# pwd
/opt/soft/hbase235/lib[root@guo147 lib]# rm -rf guava-11.0.2.jar
//拷贝文章来源:https://www.toymoban.com/news/detail-411456.html
[root@guo147 lib]# cp /opt/soft/hive312/lib/guava-27.0-jre.jar ./文章来源地址https://www.toymoban.com/news/detail-411456.html
到了这里,关于IDEA连接HBase的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!