本文的主线 项目 => 操作
本文的示例代码参考hbase-phoenix
项目
spring init -b=2.2.10.RELEASE -j=1.8 -l=java -d=web --build=gradle hbase-phoenix && cd hbase-phoenix
vim build.gradle
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'com.baomidou:mybatis-plus-boot-starter:3.4.1'
implementation('org.apache.phoenix:phoenix-core:5.0.0-HBase-2.0') {
exclude(module: 'slf4j-log4j12')
exclude(module: 'log4j')
}
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
vim src/main/resources/application.properties
spring.datasource.url=jdbc:phoenix:localhost
spring.datasource.driver-class-name=org.apache.phoenix.jdbc.PhoenixDriver
操作
vim src/main/java/com/example/hbasephoenix/GPSModel.java
package com.example.hbasephoenix;
public class GPSModel {
public Integer id;
public Double longitude;
public Double latitude;
public String city;
public Integer createdTs;
}
vim src/main/java/com/example/hbasephoenix/GPSMapper.java
package com.example.hbasephoenix;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.*;
@Mapper
public interface GPSMapper {
@Select("SELECT * FROM t_gps WHERE id = #{id}")
public List<GPSModel> findById(Integer id);
}
vim src/main/java/com/example/hbasephoenix/GPSController.java
package com.example.hbasephoenix;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
public class GPSController {
@Autowired
private GPSMapper gpsMapper;
@RequestMapping("/")
public List<GPSModel> index(@RequestParam("id") Integer id) {
return gpsMapper.findById(id);
}
}
vim src/main/java/com/example/hbasephoenix/DemoApplication.java
package com.example.hbasephoenix;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.example.hbasephoenix")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
./gradlew bootrun
curl 'http://localhost:8080/?id=1001' | json
[
{
"id": 1001,
"longitude": 118.77807441,
"latitude": 32.0572355,
"city": "BeiJing",
"createdTs": 1608199850
},
{
"id": 1001,
"longitude": 118.77807441,
"latitude": 32.0572355,
"city": "BeiJing",
"createdTs": 1608219850
}
]
curl 'http://localhost:8080/?id=1002' | json
[
{
"id": 1002,
"longitude": 118.77807441,
"latitude": 32.0572355,
"city": "BeiJing",
"createdTs": 1608229850
}
]
网友评论